main.go 6.4 KB

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