main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/static"
  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. )
  16. var ParamHost string
  17. var ParamPort int
  18. var ParamWwwDir string
  19. func init() {
  20. flag.StringVar(&ParamHost, "host", "0.0.0.0", "server host")
  21. flag.IntVar(&ParamPort, "port", 8080, "server port")
  22. flag.StringVar(&ParamWwwDir, "dir", "", "virtual hosts directory")
  23. flag.Parse()
  24. }
  25. func main() {
  26. ParamWwwDir = utils.FixPath(ParamWwwDir)
  27. if !utils.IsHostDirExists(ParamWwwDir) {
  28. fmt.Println("Virtual hosts directory is not exists")
  29. fmt.Println("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts")
  30. return
  31. }
  32. res := resource.New()
  33. res.Add(consts.AssetsCpScriptsJs, "application/javascript; charset=utf-8", assets.CpScriptsJs)
  34. res.Add(consts.AssetsCpStylesCss, "text/css", assets.CpStylesCss)
  35. res.Add(consts.AssetsSysBgPng, "image/png", assets.SysBgPng)
  36. res.Add(consts.AssetsSysFaveIco, "image/x-icon", assets.SysFaveIco)
  37. res.Add(consts.AssetsSysLogoPng, "image/png", assets.SysLogoPng)
  38. res.Add(consts.AssetsSysLogoSvg, "image/svg+xml", assets.SysLogoSvg)
  39. res.Add(consts.AssetsSysStylesCss, "text/css", assets.SysStylesCss)
  40. // TODO: Move this to `"github.com/vladimirok5959/golang-server-static/static"`
  41. stat := static.New(consts.DirIndexFile)
  42. // TODO: Logic as object here
  43. bootstrap.Start(fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
  44. w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
  45. }, func(w http.ResponseWriter, r *http.Request) {
  46. // Mounted assets
  47. if res.Response(w, r, func(w http.ResponseWriter, r *http.Request, i *resource.Resource) {
  48. w.Header().Set("Cache-Control", "public, max-age=31536000")
  49. }, nil) {
  50. return
  51. }
  52. // Host and port
  53. host, port := utils.ExtractHostPort(r.Host, false)
  54. vhost_dir := ParamWwwDir + string(os.PathSeparator) + host
  55. if !utils.IsHostDirExists(vhost_dir) {
  56. vhost_dir = ParamWwwDir + string(os.PathSeparator) + "localhost"
  57. }
  58. // Check for minimal dir structure
  59. vhost_dir_config := vhost_dir + string(os.PathSeparator) + "config"
  60. vhost_dir_htdocs := vhost_dir + string(os.PathSeparator) + "htdocs"
  61. vhost_dir_logs := vhost_dir + string(os.PathSeparator) + "logs"
  62. vhost_dir_template := vhost_dir + string(os.PathSeparator) + "template"
  63. vhost_dir_tmp := vhost_dir + string(os.PathSeparator) + "tmp"
  64. if !utils.IsHostDirExists(vhost_dir_config) {
  65. utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_config+" is not found"))
  66. return
  67. }
  68. if !utils.IsHostDirExists(vhost_dir_htdocs) {
  69. utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
  70. return
  71. }
  72. if !utils.IsHostDirExists(vhost_dir_logs) {
  73. utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
  74. return
  75. }
  76. if !utils.IsHostDirExists(vhost_dir_template) {
  77. utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_template+" is not found"))
  78. return
  79. }
  80. if !utils.IsHostDirExists(vhost_dir_tmp) {
  81. utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
  82. return
  83. }
  84. // Static files
  85. if stat.Response(vhost_dir_htdocs, w, r, nil, nil) {
  86. return
  87. }
  88. // Session
  89. //sess := session.New(w, r, vhost_dir_tmp)
  90. //defer sess.Close()
  91. // Session struct need to make public!
  92. sess := session.New(w, r, vhost_dir_tmp)
  93. defer sess.Close()
  94. // Logic
  95. // TODO: call from `logic.Response()`
  96. // TODO: create logic object here???
  97. if logic(host, port, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp, w, r) {
  98. return
  99. }
  100. // Error 404
  101. // TODO: display default template
  102. w.WriteHeader(http.StatusNotFound)
  103. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  104. w.Header().Set("Content-Type", "text/html")
  105. w.Write([]byte(`404`))
  106. })
  107. // Delete expired session files
  108. // session.Clean("./tmp")
  109. }
  110. func logic(host, port, dir_config, dir_htdocs, dir_logs, dir_template, dir_tmp string, w http.ResponseWriter, r *http.Request) bool {
  111. if r.URL.Path == "/" {
  112. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  113. w.Header().Set("Content-Type", "text/html")
  114. //counter := sess.GetInt("counter", 0)
  115. //w.Write([]byte(`Logic -> (` + fmt.Sprintf("%d", counter) + `)`))
  116. w.Write([]byte(`Logic`))
  117. //counter++
  118. //sess.SetInt("counter", counter)
  119. return true
  120. }
  121. return false
  122. }