main.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.Println(err)
  67. return
  68. }
  69. // Init logger
  70. lg := logger.New()
  71. defer lg.Close()
  72. // Attach www dir to logger
  73. lg.SetWwwDir(consts.ParamWwwDir)
  74. // Session cleaner
  75. sess_cl_ch, sess_cl_stop := session_clean_start(consts.ParamWwwDir)
  76. defer session_clean_stop(sess_cl_ch, sess_cl_stop)
  77. // Init mounted resources
  78. res := resource.New()
  79. assets.PopulateResources(res)
  80. // Init static files helper
  81. stat := static.New(consts.DirIndexFile)
  82. // Init modules
  83. mods := modules.New()
  84. // MySQL connections pool
  85. mpool := mysqlpool.New()
  86. // Init and start web server
  87. bootstrap.Start(lg.Handler, fmt.Sprintf("%s:%d", consts.ParamHost, consts.ParamPort), 9, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request, o interface{}) {
  88. w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
  89. }, func(w http.ResponseWriter, r *http.Request, o interface{}) {
  90. // Schema
  91. r.URL.Scheme = "http"
  92. // Mounted assets
  93. if res.Response(w, r, func(w http.ResponseWriter, r *http.Request, i *resource.OneResource) {
  94. w.Header().Set("Cache-Control", "public, max-age=31536000")
  95. if consts.ParamDebug && i.Path == "assets/cp/scripts.js" {
  96. w.Write([]byte("window.fave_debug=true;"))
  97. }
  98. }, nil) {
  99. return
  100. }
  101. // Host and port
  102. host, port := utils.ExtractHostPort(r.Host, false)
  103. curr_host := host
  104. // Domain bindings
  105. doms := domains.New(consts.ParamWwwDir)
  106. if mhost := doms.GetHost(host); mhost != "" {
  107. curr_host = mhost
  108. }
  109. vhost_dir := consts.ParamWwwDir + string(os.PathSeparator) + curr_host
  110. if !utils.IsDirExists(vhost_dir) {
  111. curr_host = "localhost"
  112. vhost_dir = consts.ParamWwwDir + string(os.PathSeparator) + "localhost"
  113. }
  114. // Check for minimal dir structure
  115. vhost_dir_config := vhost_dir + string(os.PathSeparator) + "config"
  116. vhost_dir_htdocs := vhost_dir + string(os.PathSeparator) + "htdocs"
  117. vhost_dir_logs := vhost_dir + string(os.PathSeparator) + "logs"
  118. vhost_dir_template := vhost_dir + string(os.PathSeparator) + "template"
  119. vhost_dir_tmp := vhost_dir + string(os.PathSeparator) + "tmp"
  120. if !utils.IsDirExists(vhost_dir_config) {
  121. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_config+" is not found"))
  122. return
  123. }
  124. if !utils.IsDirExists(vhost_dir_htdocs) {
  125. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
  126. return
  127. }
  128. if !utils.IsDirExists(vhost_dir_logs) {
  129. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
  130. return
  131. }
  132. if !utils.IsDirExists(vhost_dir_template) {
  133. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_template+" is not found"))
  134. return
  135. }
  136. if !utils.IsDirExists(vhost_dir_tmp) {
  137. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
  138. return
  139. }
  140. // Static files
  141. if stat.Response(vhost_dir_htdocs, w, r, nil, nil) {
  142. return
  143. }
  144. // Robots.txt and styles.css from template dir
  145. if ServeTemplateFile(w, r, "robots.txt", "", vhost_dir_template) {
  146. return
  147. }
  148. if ServeTemplateFile(w, r, "styles.css", "assets/theme/", vhost_dir_template) {
  149. return
  150. }
  151. if ServeTemplateFile(w, r, "scripts.js", "assets/theme/", vhost_dir_template) {
  152. return
  153. }
  154. // Session
  155. sess := session.New(w, r, vhost_dir_tmp)
  156. defer sess.Close()
  157. // Convert
  158. var mp *mysqlpool.MySqlPool
  159. if mpool, ok := o.(*mysqlpool.MySqlPool); ok {
  160. mp = mpool
  161. }
  162. // Logic
  163. if mp != nil {
  164. 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) {
  165. return
  166. }
  167. }
  168. // Error 404
  169. utils.SystemErrorPage404(w)
  170. }, func(s *http.Server) {
  171. s.SetKeepAlivesEnabled(consts.ParamKeepAlive)
  172. }, mpool)
  173. // Close MySQL
  174. mpool.CloseAll()
  175. }
  176. func ServeTemplateFile(w http.ResponseWriter, r *http.Request, file string, path string, dir string) bool {
  177. if r.URL.Path == "/"+path+file {
  178. f, err := os.Open(dir + string(os.PathSeparator) + file)
  179. if err == nil {
  180. defer f.Close()
  181. st, err := os.Stat(dir + string(os.PathSeparator) + file)
  182. if err == nil {
  183. if !st.Mode().IsDir() {
  184. http.ServeFile(w, r, dir+string(os.PathSeparator)+file)
  185. return true
  186. }
  187. }
  188. }
  189. }
  190. return false
  191. }