main.go 4.0 KB

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