main.go 4.1 KB

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