main.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.BoolVar(&consts.ParamKeepAlive, "keepalive", false, "enable/disable server keep alive")
  25. flag.Parse()
  26. }
  27. func main() {
  28. // Universal, params by env vars
  29. if consts.ParamHost == "0.0.0.0" {
  30. if os.Getenv("FAVE_HOST") != "" {
  31. consts.ParamHost = os.Getenv("FAVE_HOST")
  32. }
  33. }
  34. if consts.ParamPort == 8080 {
  35. if os.Getenv("FAVE_PORT") != "" {
  36. consts.ParamPort = utils.StrToInt(os.Getenv("FAVE_PORT"))
  37. }
  38. }
  39. if consts.ParamWwwDir == "" {
  40. if os.Getenv("FAVE_DIR") != "" {
  41. consts.ParamWwwDir = os.Getenv("FAVE_DIR")
  42. }
  43. }
  44. if consts.ParamDebug == false {
  45. if os.Getenv("FAVE_DEBUG") == "true" {
  46. consts.ParamDebug = true
  47. }
  48. }
  49. if consts.ParamKeepAlive == false {
  50. if os.Getenv("FAVE_KEEPALIVE") == "true" {
  51. consts.ParamKeepAlive = true
  52. }
  53. }
  54. // Check www dir
  55. consts.ParamWwwDir = utils.FixPath(consts.ParamWwwDir)
  56. if !utils.IsDirExists(consts.ParamWwwDir) {
  57. fmt.Printf("Virtual hosts directory is not exists\n")
  58. fmt.Printf("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts\n")
  59. return
  60. }
  61. // Init logger
  62. lg := logger.New()
  63. defer lg.Close()
  64. // Attach www dir to logger
  65. lg.SetWwwDir(consts.ParamWwwDir)
  66. // Session cleaner
  67. sess_cl_ch, sess_cl_stop := session_clean_start(consts.ParamWwwDir)
  68. defer session_clean_stop(sess_cl_ch, sess_cl_stop)
  69. // Init mounted resources
  70. res := resource.New()
  71. assets.PopulateResources(res)
  72. // Init static files helper
  73. stat := static.New(consts.DirIndexFile)
  74. // Init modules
  75. mods := modules.New()
  76. // Init and start web server
  77. bootstrap.Start(lg.Handler, fmt.Sprintf("%s:%d", consts.ParamHost, consts.ParamPort), 9, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
  78. w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
  79. }, func(w http.ResponseWriter, r *http.Request) {
  80. // Schema
  81. r.URL.Scheme = "http"
  82. // Mounted assets
  83. if res.Response(w, r, func(w http.ResponseWriter, r *http.Request, i *resource.OneResource) {
  84. w.Header().Set("Cache-Control", "public, max-age=31536000")
  85. }, nil) {
  86. return
  87. }
  88. // Host and port
  89. host, port := utils.ExtractHostPort(r.Host, false)
  90. curr_host := host
  91. vhost_dir := consts.ParamWwwDir + string(os.PathSeparator) + host
  92. if !utils.IsDirExists(vhost_dir) {
  93. curr_host = "localhost"
  94. vhost_dir = consts.ParamWwwDir + string(os.PathSeparator) + "localhost"
  95. }
  96. // Check for minimal dir structure
  97. vhost_dir_config := vhost_dir + string(os.PathSeparator) + "config"
  98. vhost_dir_htdocs := vhost_dir + string(os.PathSeparator) + "htdocs"
  99. vhost_dir_logs := vhost_dir + string(os.PathSeparator) + "logs"
  100. vhost_dir_template := vhost_dir + string(os.PathSeparator) + "template"
  101. vhost_dir_tmp := vhost_dir + string(os.PathSeparator) + "tmp"
  102. if !utils.IsDirExists(vhost_dir_config) {
  103. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_config+" is not found"))
  104. return
  105. }
  106. if !utils.IsDirExists(vhost_dir_htdocs) {
  107. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
  108. return
  109. }
  110. if !utils.IsDirExists(vhost_dir_logs) {
  111. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
  112. return
  113. }
  114. if !utils.IsDirExists(vhost_dir_template) {
  115. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_template+" is not found"))
  116. return
  117. }
  118. if !utils.IsDirExists(vhost_dir_tmp) {
  119. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
  120. return
  121. }
  122. // Static files
  123. if stat.Response(vhost_dir_htdocs, w, r, nil, nil) {
  124. return
  125. }
  126. // Robots.txt and styles.css from template dir
  127. if ServeTemplateFile(w, r, "robots.txt", "", vhost_dir_template) {
  128. return
  129. }
  130. if ServeTemplateFile(w, r, "styles.css", "assets/theme/", vhost_dir_template) {
  131. return
  132. }
  133. if ServeTemplateFile(w, r, "scripts.js", "assets/theme/", vhost_dir_template) {
  134. return
  135. }
  136. // Session
  137. sess := session.New(w, r, vhost_dir_tmp)
  138. defer sess.Close()
  139. // Logic
  140. 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) {
  141. return
  142. }
  143. // Error 404
  144. utils.SystemErrorPage404(w)
  145. }, func(s *http.Server) {
  146. s.SetKeepAlivesEnabled(consts.ParamKeepAlive)
  147. })
  148. }
  149. func ServeTemplateFile(w http.ResponseWriter, r *http.Request, file string, path string, dir string) bool {
  150. if r.URL.Path == "/"+path+file {
  151. f, err := os.Open(dir + string(os.PathSeparator) + file)
  152. if err == nil {
  153. defer f.Close()
  154. st, err := os.Stat(dir + string(os.PathSeparator) + file)
  155. if err == nil {
  156. if !st.Mode().IsDir() {
  157. http.ServeFile(w, r, dir+string(os.PathSeparator)+file)
  158. return true
  159. }
  160. }
  161. }
  162. }
  163. return false
  164. }