main.go 5.6 KB

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