main.go 6.1 KB

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