main.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // Shop basket
  97. sb := basket.New()
  98. sb_cl_ch, sb_cl_stop := basket_clean_start(sb)
  99. defer basket_clean_stop(sb_cl_ch, sb_cl_stop)
  100. // Init cache blocks
  101. cbs := cblocks.New()
  102. // Init and start web server
  103. server_address := fmt.Sprintf("%s:%d", consts.ParamHost, consts.ParamPort)
  104. bootstrap.Start(lg.Handler, server_address, 9, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request, o interface{}) {
  105. w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
  106. }, func(w http.ResponseWriter, r *http.Request, o interface{}) {
  107. // Schema
  108. r.URL.Scheme = "http"
  109. // Mounted assets
  110. if res.Response(w, r, func(w http.ResponseWriter, r *http.Request, i *resource.OneResource) {
  111. w.Header().Set("Cache-Control", "public, max-age=31536000")
  112. if consts.ParamDebug && i.Path == "assets/cp/scripts.js" {
  113. w.Write([]byte("window.fave_debug=true;"))
  114. }
  115. }, nil) {
  116. return
  117. }
  118. // Host and port
  119. host, port := utils.ExtractHostPort(r.Host, false)
  120. curr_host := host
  121. // Domain bindings
  122. doms := domains.New(consts.ParamWwwDir)
  123. if mhost := doms.GetHost(host); mhost != "" {
  124. curr_host = mhost
  125. }
  126. vhost_dir := consts.ParamWwwDir + string(os.PathSeparator) + curr_host
  127. if !utils.IsDirExists(vhost_dir) {
  128. curr_host = "localhost"
  129. vhost_dir = consts.ParamWwwDir + string(os.PathSeparator) + "localhost"
  130. }
  131. // Check for minimal dir structure
  132. vhost_dir_config := vhost_dir + string(os.PathSeparator) + "config"
  133. vhost_dir_htdocs := vhost_dir + string(os.PathSeparator) + "htdocs"
  134. vhost_dir_logs := vhost_dir + string(os.PathSeparator) + "logs"
  135. vhost_dir_template := vhost_dir + string(os.PathSeparator) + "template"
  136. vhost_dir_tmp := vhost_dir + string(os.PathSeparator) + "tmp"
  137. if !utils.IsDirExists(vhost_dir_config) {
  138. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_config+" is not found"))
  139. return
  140. }
  141. if !utils.IsDirExists(vhost_dir_htdocs) {
  142. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
  143. return
  144. }
  145. if !utils.IsDirExists(vhost_dir_logs) {
  146. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
  147. return
  148. }
  149. if !utils.IsDirExists(vhost_dir_template) {
  150. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_template+" is not found"))
  151. return
  152. }
  153. if !utils.IsDirExists(vhost_dir_tmp) {
  154. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
  155. return
  156. }
  157. // Static files
  158. if stat.Response(vhost_dir_htdocs, w, r, nil, nil) {
  159. return
  160. }
  161. // Robots.txt and styles.css from template dir
  162. if ServeTemplateFile(w, r, "robots.txt", "", vhost_dir_template) {
  163. return
  164. }
  165. if ServeTemplateFile(w, r, "styles.css", "assets/theme/", vhost_dir_template) {
  166. return
  167. }
  168. if ServeTemplateFile(w, r, "scripts.js", "assets/theme/", vhost_dir_template) {
  169. return
  170. }
  171. // Session
  172. sess := session.New(w, r, vhost_dir_tmp)
  173. defer sess.Close()
  174. // Convert
  175. var mp *mysqlpool.MySqlPool
  176. if mpool, ok := o.(*mysqlpool.MySqlPool); ok {
  177. mp = mpool
  178. }
  179. // Logic
  180. if mp != nil {
  181. if engine.Response(
  182. mp,
  183. sb,
  184. lg,
  185. mods,
  186. w,
  187. r,
  188. sess,
  189. cbs,
  190. host,
  191. port,
  192. curr_host,
  193. vhost_dir_config,
  194. vhost_dir_htdocs,
  195. vhost_dir_logs,
  196. vhost_dir_template,
  197. vhost_dir_tmp,
  198. ) {
  199. return
  200. }
  201. }
  202. // Error 404
  203. utils.SystemErrorPage404(w)
  204. }, func(s *http.Server) {
  205. s.SetKeepAlivesEnabled(consts.ParamKeepAlive)
  206. }, mpool)
  207. // Close MySQL
  208. mpool.CloseAll()
  209. }
  210. func ServeTemplateFile(w http.ResponseWriter, r *http.Request, file string, path string, dir string) bool {
  211. if r.URL.Path == "/"+path+file {
  212. f, err := os.Open(dir + string(os.PathSeparator) + file)
  213. if err == nil {
  214. defer f.Close()
  215. st, err := os.Stat(dir + string(os.PathSeparator) + file)
  216. if err == nil {
  217. if !st.Mode().IsDir() {
  218. http.ServeFile(w, r, dir+string(os.PathSeparator)+file)
  219. return true
  220. }
  221. }
  222. }
  223. }
  224. return false
  225. }