main.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. func init() {
  20. flag.StringVar(&consts.ParamHost, "host", "0.0.0.0", "server host")
  21. flag.IntVar(&consts.ParamPort, "port", 8080, "server port")
  22. flag.StringVar(&consts.ParamWwwDir, "dir", "", "virtual hosts directory")
  23. flag.BoolVar(&consts.ParamDebug, "debug", false, "debug mode with ignoring log files")
  24. flag.Parse()
  25. }
  26. func main() {
  27. // Universal, params by env vars
  28. if consts.ParamHost == "0.0.0.0" {
  29. if os.Getenv("FAVE_HOST") != "" {
  30. consts.ParamHost = os.Getenv("FAVE_HOST")
  31. }
  32. }
  33. if consts.ParamPort == 8080 {
  34. if os.Getenv("FAVE_PORT") != "" {
  35. consts.ParamPort = utils.StrToInt(os.Getenv("FAVE_PORT"))
  36. }
  37. }
  38. if consts.ParamWwwDir == "" {
  39. if os.Getenv("FAVE_DIR") != "" {
  40. consts.ParamWwwDir = os.Getenv("FAVE_DIR")
  41. }
  42. }
  43. if consts.ParamDebug == false {
  44. if os.Getenv("FAVE_DEBUG") == "true" {
  45. consts.ParamDebug = true
  46. }
  47. }
  48. // Init logger
  49. lg := logger.New()
  50. defer lg.Close()
  51. // Check www dir
  52. consts.ParamWwwDir = utils.FixPath(consts.ParamWwwDir)
  53. if !utils.IsDirExists(consts.ParamWwwDir) {
  54. lg.Log("Virtual hosts directory is not exists", nil, true)
  55. lg.Log("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts", nil, true)
  56. return
  57. }
  58. // Attach www dir to logger
  59. lg.SetWwwDir(consts.ParamWwwDir)
  60. // Session cleaner
  61. sess_cl_ch, sess_cl_stop := session_clean_start(consts.ParamWwwDir)
  62. defer session_clean_stop(sess_cl_ch, sess_cl_stop)
  63. // Init mounted resources
  64. res := resource.New()
  65. assets.PopulateResources(res)
  66. // Init static files helper
  67. stat := static.New(consts.DirIndexFile)
  68. // Init modules
  69. mods := modules.New()
  70. // Init and start web server
  71. bootstrap.Start(lg.Handler, fmt.Sprintf("%s:%d", consts.ParamHost, consts.ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
  72. w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
  73. }, func(w http.ResponseWriter, r *http.Request) {
  74. // Schema
  75. r.URL.Scheme = "http"
  76. // Mounted assets
  77. if res.Response(w, r, func(w http.ResponseWriter, r *http.Request, i *resource.OneResource) {
  78. w.Header().Set("Cache-Control", "public, max-age=31536000")
  79. }, nil) {
  80. return
  81. }
  82. // Host and port
  83. host, port := utils.ExtractHostPort(r.Host, false)
  84. curr_host := host
  85. vhost_dir := consts.ParamWwwDir + string(os.PathSeparator) + host
  86. if !utils.IsDirExists(vhost_dir) {
  87. curr_host = "localhost"
  88. vhost_dir = consts.ParamWwwDir + string(os.PathSeparator) + "localhost"
  89. }
  90. // Check for minimal dir structure
  91. vhost_dir_config := vhost_dir + string(os.PathSeparator) + "config"
  92. vhost_dir_htdocs := vhost_dir + string(os.PathSeparator) + "htdocs"
  93. vhost_dir_logs := vhost_dir + string(os.PathSeparator) + "logs"
  94. vhost_dir_template := vhost_dir + string(os.PathSeparator) + "template"
  95. vhost_dir_tmp := vhost_dir + string(os.PathSeparator) + "tmp"
  96. if !utils.IsDirExists(vhost_dir_config) {
  97. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_config+" is not found"))
  98. return
  99. }
  100. if !utils.IsDirExists(vhost_dir_htdocs) {
  101. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
  102. return
  103. }
  104. if !utils.IsDirExists(vhost_dir_logs) {
  105. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
  106. return
  107. }
  108. if !utils.IsDirExists(vhost_dir_template) {
  109. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_template+" is not found"))
  110. return
  111. }
  112. if !utils.IsDirExists(vhost_dir_tmp) {
  113. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
  114. return
  115. }
  116. // Static files
  117. if stat.Response(vhost_dir_htdocs, w, r, nil, nil) {
  118. return
  119. }
  120. // Session
  121. sess := session.New(w, r, vhost_dir_tmp)
  122. defer sess.Close()
  123. // Logic
  124. if engine.Response(lg, mods, w, r, sess, host, port, curr_host, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp) {
  125. return
  126. }
  127. // Error 404
  128. utils.SystemErrorPage404(w)
  129. })
  130. }