main.go 6.6 KB

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