main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "golang-fave/assets"
  9. "golang-fave/consts"
  10. "golang-fave/engine"
  11. "golang-fave/utils"
  12. "github.com/vladimirok5959/golang-server-bootstrap/bootstrap"
  13. "github.com/vladimirok5959/golang-server-resources/resource"
  14. "github.com/vladimirok5959/golang-server-sessions/session"
  15. "github.com/vladimirok5959/golang-server-static/static"
  16. )
  17. var ParamHost string
  18. var ParamPort int
  19. var ParamWwwDir string
  20. func init() {
  21. flag.StringVar(&ParamHost, "host", "0.0.0.0", "server host")
  22. flag.IntVar(&ParamPort, "port", 8080, "server port")
  23. flag.StringVar(&ParamWwwDir, "dir", "", "virtual hosts directory")
  24. flag.Parse()
  25. }
  26. func main() {
  27. ParamWwwDir = utils.FixPath(ParamWwwDir)
  28. if !utils.IsHostDirExists(ParamWwwDir) {
  29. fmt.Println("Virtual hosts directory is not exists")
  30. fmt.Println("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts")
  31. return
  32. }
  33. // Init mounted resources
  34. res := resource.New()
  35. res.Add(consts.AssetsCpScriptsJs, "application/javascript; charset=utf-8", assets.CpScriptsJs)
  36. res.Add(consts.AssetsCpStylesCss, "text/css", assets.CpStylesCss)
  37. res.Add(consts.AssetsSysBgPng, "image/png", assets.SysBgPng)
  38. res.Add(consts.AssetsSysFaveIco, "image/x-icon", assets.SysFaveIco)
  39. res.Add(consts.AssetsSysLogoPng, "image/png", assets.SysLogoPng)
  40. res.Add(consts.AssetsSysLogoSvg, "image/svg+xml", assets.SysLogoSvg)
  41. res.Add(consts.AssetsSysStylesCss, "text/css", assets.SysStylesCss)
  42. // Init static files helper
  43. stat := static.New(consts.DirIndexFile)
  44. // Init and start web server
  45. bootstrap.Start(nil, fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
  46. w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
  47. }, func(w http.ResponseWriter, r *http.Request) {
  48. // Mounted assets
  49. if res.Response(w, r, func(w http.ResponseWriter, r *http.Request, i *resource.Resource) {
  50. w.Header().Set("Cache-Control", "public, max-age=31536000")
  51. }, nil) {
  52. return
  53. }
  54. // Host and port
  55. host, port := utils.ExtractHostPort(r.Host, false)
  56. vhost_dir := ParamWwwDir + string(os.PathSeparator) + host
  57. if !utils.IsHostDirExists(vhost_dir) {
  58. vhost_dir = ParamWwwDir + string(os.PathSeparator) + "localhost"
  59. }
  60. // Check for minimal dir structure
  61. vhost_dir_config := vhost_dir + string(os.PathSeparator) + "config"
  62. vhost_dir_htdocs := vhost_dir + string(os.PathSeparator) + "htdocs"
  63. vhost_dir_logs := vhost_dir + string(os.PathSeparator) + "logs"
  64. vhost_dir_template := vhost_dir + string(os.PathSeparator) + "template"
  65. vhost_dir_tmp := vhost_dir + string(os.PathSeparator) + "tmp"
  66. if !utils.IsHostDirExists(vhost_dir_config) {
  67. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_config+" is not found"))
  68. return
  69. }
  70. if !utils.IsHostDirExists(vhost_dir_htdocs) {
  71. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
  72. return
  73. }
  74. if !utils.IsHostDirExists(vhost_dir_logs) {
  75. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
  76. return
  77. }
  78. if !utils.IsHostDirExists(vhost_dir_template) {
  79. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_template+" is not found"))
  80. return
  81. }
  82. if !utils.IsHostDirExists(vhost_dir_tmp) {
  83. utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
  84. return
  85. }
  86. // Static files
  87. if stat.Response(vhost_dir_htdocs, w, r, nil, nil) {
  88. return
  89. }
  90. // Session
  91. sess := session.New(w, r, vhost_dir_tmp)
  92. defer sess.Close()
  93. // Logic
  94. if engine.New(w, r, sess, host, port, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp).Response() {
  95. return
  96. }
  97. // Error 404
  98. utils.SystemErrorPage404(w)
  99. })
  100. // TODO: call it in background time by time
  101. // Delete expired session files
  102. // session.Clean("./tmp")
  103. }