main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package main
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "time"
  10. "golang-fave/assets"
  11. "golang-fave/consts"
  12. "golang-fave/engine"
  13. "golang-fave/logger"
  14. "golang-fave/utils"
  15. "github.com/vladimirok5959/golang-server-bootstrap/bootstrap"
  16. "github.com/vladimirok5959/golang-server-resources/resource"
  17. "github.com/vladimirok5959/golang-server-sessions/session"
  18. "github.com/vladimirok5959/golang-server-static/static"
  19. )
  20. var ParamHost string
  21. var ParamPort int
  22. var ParamWwwDir string
  23. func init() {
  24. flag.StringVar(&ParamHost, "host", "0.0.0.0", "server host")
  25. flag.IntVar(&ParamPort, "port", 8080, "server port")
  26. flag.StringVar(&ParamWwwDir, "dir", "", "virtual hosts directory")
  27. flag.Parse()
  28. }
  29. func main() {
  30. // Init logger
  31. lg := logger.New()
  32. defer lg.Close()
  33. // Check www dir
  34. ParamWwwDir = utils.FixPath(ParamWwwDir)
  35. if !utils.IsDirExists(ParamWwwDir) {
  36. lg.Log("Virtual hosts directory is not exists", nil, true)
  37. lg.Log("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts", nil, true)
  38. return
  39. }
  40. // Attach www dir to logger
  41. lg.SetWwwDir(ParamWwwDir)
  42. // Session cleaner
  43. sess_cleaner_chan := make(chan bool)
  44. go func() {
  45. for {
  46. select {
  47. case <-time.After(1 * time.Hour):
  48. files, err := ioutil.ReadDir(ParamWwwDir)
  49. if err == nil {
  50. for _, file := range files {
  51. tmpdir := ParamWwwDir + string(os.PathSeparator) + file.Name() + string(os.PathSeparator) + "tmp"
  52. if utils.IsDirExists(tmpdir) {
  53. session.Clean(tmpdir)
  54. }
  55. }
  56. }
  57. case <-sess_cleaner_chan:
  58. sess_cleaner_chan <- true
  59. return
  60. }
  61. }
  62. }()
  63. defer func() {
  64. sess_cleaner_chan <- true
  65. <-sess_cleaner_chan
  66. }()
  67. // Init mounted resources
  68. res := resource.New()
  69. res.Add(consts.AssetsCpScriptsJs, "application/javascript; charset=utf-8", assets.CpScriptsJs)
  70. res.Add(consts.AssetsCpStylesCss, "text/css", assets.CpStylesCss)
  71. res.Add(consts.AssetsSysBgPng, "image/png", assets.SysBgPng)
  72. res.Add(consts.AssetsSysFaveIco, "image/x-icon", assets.SysFaveIco)
  73. res.Add(consts.AssetsSysLogoPng, "image/png", assets.SysLogoPng)
  74. res.Add(consts.AssetsSysLogoSvg, "image/svg+xml", assets.SysLogoSvg)
  75. res.Add(consts.AssetsSysStylesCss, "text/css", assets.SysStylesCss)
  76. // Init static files helper
  77. stat := static.New(consts.DirIndexFile)
  78. // Init and start web server
  79. bootstrap.Start(lg.Handler, fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
  80. w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
  81. }, func(w http.ResponseWriter, r *http.Request) {
  82. // Mounted assets
  83. if res.Response(w, r, func(w http.ResponseWriter, r *http.Request, i *resource.Resource) {
  84. w.Header().Set("Cache-Control", "public, max-age=31536000")
  85. }, nil) {
  86. return
  87. }
  88. // Host and port
  89. host, port := utils.ExtractHostPort(r.Host, false)
  90. vhost_dir := ParamWwwDir + string(os.PathSeparator) + host
  91. if !utils.IsDirExists(vhost_dir) {
  92. vhost_dir = ParamWwwDir + string(os.PathSeparator) + "localhost"
  93. }
  94. // Check for minimal dir structure
  95. vhost_dir_config := vhost_dir + string(os.PathSeparator) + "config"
  96. vhost_dir_htdocs := vhost_dir + string(os.PathSeparator) + "htdocs"
  97. vhost_dir_logs := vhost_dir + string(os.PathSeparator) + "logs"
  98. vhost_dir_template := vhost_dir + string(os.PathSeparator) + "template"
  99. vhost_dir_tmp := vhost_dir + string(os.PathSeparator) + "tmp"
  100. if !utils.IsDirExists(vhost_dir_config) {
  101. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_config+" is not found"))
  102. return
  103. }
  104. if !utils.IsDirExists(vhost_dir_htdocs) {
  105. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
  106. return
  107. }
  108. if !utils.IsDirExists(vhost_dir_logs) {
  109. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
  110. return
  111. }
  112. if !utils.IsDirExists(vhost_dir_template) {
  113. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_template+" is not found"))
  114. return
  115. }
  116. if !utils.IsDirExists(vhost_dir_tmp) {
  117. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
  118. return
  119. }
  120. // Static files
  121. if stat.Response(vhost_dir_htdocs, w, r, nil, nil) {
  122. return
  123. }
  124. // Session
  125. sess := session.New(w, r, vhost_dir_tmp)
  126. defer sess.Close()
  127. // Logic
  128. if engine.Response(lg, w, r, sess, host, port, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp) {
  129. return
  130. }
  131. // Error 404
  132. utils.SystemErrorPage404(w)
  133. })
  134. // TODO: call it in background time by time
  135. // Delete expired session files
  136. // session.Clean("./tmp")
  137. }