main.go 6.1 KB

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