main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net/http"
  6. "golang-fave/consts"
  7. "golang-fave/utils"
  8. "github.com/vladimirok5959/golang-server-bootstrap/bootstrap"
  9. /*
  10. "context"
  11. "errors"
  12. "flag"
  13. "fmt"
  14. "log"
  15. "net/http"
  16. "os"
  17. "os/signal"
  18. "strconv"
  19. "strings"
  20. "time"
  21. "golang-fave/constants"
  22. "golang-fave/engine/actions"
  23. "golang-fave/engine/wrapper"
  24. */)
  25. var ParamHost string
  26. var ParamPort int
  27. var ParamWwwDir string
  28. //var VhostHomeDir string
  29. func init() {
  30. flag.StringVar(&ParamHost, "host", "0.0.0.0", "server host")
  31. flag.IntVar(&ParamPort, "port", 8080, "server port")
  32. flag.StringVar(&ParamWwwDir, "dir", "", "virtual hosts directory")
  33. flag.Parse()
  34. }
  35. func main() {
  36. ParamWwwDir = utils.FixPath(ParamWwwDir)
  37. if !(utils.IsFileExists(ParamWwwDir) && utils.IsDir(ParamWwwDir)) {
  38. fmt.Println("Virtual hosts directory is not exists")
  39. fmt.Println("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts")
  40. return
  41. }
  42. bootstrap.Start("127.0.0.1:8080", 30, "assets", func(w http.ResponseWriter, r *http.Request) {
  43. w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
  44. }, func(w http.ResponseWriter, r *http.Request) {
  45. /*
  46. // After callback
  47. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  48. w.Header().Set("Content-Type", "text/html")
  49. w.Write([]byte(`
  50. <div>Hello World!</div>
  51. <div><a href="/assets/bootstrap.css">/assets/bootstrap.css</a></div>
  52. <div><a href="/assets/bootstrap.js">/assets/bootstrap.js</a></div>
  53. <div><a href="/assets/jquery.js">/assets/jquery.js</a></div>
  54. <div><a href="/assets/popper.js">/assets/popper.js</a></div>
  55. `))
  56. */
  57. })
  58. /*
  59. if _, err := os.Stat(ParamWwwDir); os.IsNotExist(err) {
  60. fmt.Println("Virtual hosts directory is not exists")
  61. fmt.Println("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts")
  62. return
  63. }
  64. if ParamWwwDir[len(ParamWwwDir)-1] != '/' {
  65. ParamWwwDir = ParamWwwDir + "/"
  66. }
  67. // Handle
  68. mux := http.NewServeMux()
  69. mux.HandleFunc("/", handler)
  70. srv := &http.Server{
  71. Addr: fmt.Sprintf("%s:%d", ParamHost, ParamPort),
  72. Handler: mux,
  73. }
  74. stop := make(chan os.Signal)
  75. signal.Notify(stop, os.Interrupt)
  76. go func() {
  77. log.Printf("Starting server at %s:%d", ParamHost, ParamPort)
  78. if err := srv.ListenAndServe(); err != nil {
  79. if err != http.ErrServerClosed {
  80. log.Fatal(err)
  81. }
  82. }
  83. }()
  84. // Wait for signal
  85. <-stop
  86. log.Printf("Shutting down server...\n")
  87. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  88. defer cancel()
  89. if err := srv.Shutdown(ctx); err != nil {
  90. log.Fatal(err)
  91. } else {
  92. log.Printf("Server is off!")
  93. }
  94. */
  95. }
  96. /*
  97. func vhExists(vhosthome string) bool {
  98. if st, err := os.Stat(vhosthome); !os.IsNotExist(err) {
  99. if err == nil {
  100. fmode := st.Mode()
  101. if fmode.IsDir() {
  102. return true
  103. }
  104. }
  105. }
  106. return false
  107. }
  108. func handler(w http.ResponseWriter, r *http.Request) {
  109. // Build vhost home dir
  110. host := r.Host
  111. port := strconv.Itoa(ParamPort)
  112. index := strings.Index(host, ":")
  113. if index > -1 {
  114. port = host[index+1:]
  115. host = host[0:index]
  116. }
  117. // Cut "www" if exists
  118. if strings.HasPrefix(host, "www.") {
  119. host = host[4:]
  120. }
  121. VhostHomeDir = ParamWwwDir + host
  122. // Check if virtual host exists
  123. if !vhExists(VhostHomeDir) {
  124. host = "localhost"
  125. VhostHomeDir = ParamWwwDir + host
  126. }
  127. // Set protocol
  128. r.URL.Scheme = "http"
  129. // Set server name
  130. w.Header().Set("Server", "fave.pro/"+constants.ServerVersion)
  131. wrap := wrapper.New(&w, r, host, port, ParamWwwDir, VhostHomeDir)
  132. // Check if localhost exists
  133. if !vhExists(VhostHomeDir) && !strings.HasPrefix(r.URL.Path, "/assets/") {
  134. wrap.PrintEnginePageError(errors.New("Folder " + VhostHomeDir + " is not found"))
  135. return
  136. }
  137. // Create and start engine
  138. wrap.Run(func(wrapper *wrapper.Wrapper) bool {
  139. // Actions
  140. action := actions.New(wrapper)
  141. if action.Run() {
  142. wrapper.Session.Save()
  143. return true
  144. }
  145. // Pages
  146. return handlerPage(wrapper)
  147. })
  148. }
  149. */