main.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // Init and start web server
  58. bootstrap.Start(lg.Handler, fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
  59. w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
  60. }, func(w http.ResponseWriter, r *http.Request) {
  61. // Schema
  62. r.URL.Scheme = "http"
  63. // Mounted assets
  64. if res.Response(w, r, func(w http.ResponseWriter, r *http.Request, i *resource.Resource) {
  65. w.Header().Set("Cache-Control", "public, max-age=31536000")
  66. }, nil) {
  67. return
  68. }
  69. // Host and port
  70. host, port := utils.ExtractHostPort(r.Host, false)
  71. vhost_dir := ParamWwwDir + string(os.PathSeparator) + host
  72. if !utils.IsDirExists(vhost_dir) {
  73. vhost_dir = ParamWwwDir + string(os.PathSeparator) + "localhost"
  74. }
  75. // Check for minimal dir structure
  76. vhost_dir_config := vhost_dir + string(os.PathSeparator) + "config"
  77. vhost_dir_htdocs := vhost_dir + string(os.PathSeparator) + "htdocs"
  78. vhost_dir_logs := vhost_dir + string(os.PathSeparator) + "logs"
  79. vhost_dir_template := vhost_dir + string(os.PathSeparator) + "template"
  80. vhost_dir_tmp := vhost_dir + string(os.PathSeparator) + "tmp"
  81. if !utils.IsDirExists(vhost_dir_config) {
  82. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_config+" is not found"))
  83. return
  84. }
  85. if !utils.IsDirExists(vhost_dir_htdocs) {
  86. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
  87. return
  88. }
  89. if !utils.IsDirExists(vhost_dir_logs) {
  90. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
  91. return
  92. }
  93. if !utils.IsDirExists(vhost_dir_template) {
  94. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_template+" is not found"))
  95. return
  96. }
  97. if !utils.IsDirExists(vhost_dir_tmp) {
  98. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
  99. return
  100. }
  101. // Static files
  102. if stat.Response(vhost_dir_htdocs, w, r, nil, nil) {
  103. return
  104. }
  105. // Session
  106. sess := session.New(w, r, vhost_dir_tmp)
  107. defer sess.Close()
  108. // Logic
  109. 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) {
  110. return
  111. }
  112. // Error 404
  113. utils.SystemErrorPage404(w)
  114. })
  115. }