main.go 7.2 KB

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