main.go 4.0 KB

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