main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "os"
  9. "os/signal"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "golang-fave/engine/actions"
  14. "golang-fave/engine/wrapper"
  15. )
  16. const C_Debug = !false
  17. var ParamHost string
  18. var ParamPort int
  19. var ParamWwwDir string
  20. var VhostHomeDir string
  21. func init() {
  22. flag.StringVar(&ParamHost, "host", "0.0.0.0", "server host")
  23. flag.IntVar(&ParamPort, "port", 8080, "server port")
  24. flag.StringVar(&ParamWwwDir, "dir", "", "virtual hosts directory")
  25. flag.Parse()
  26. }
  27. func main() {
  28. if _, err := os.Stat(ParamWwwDir); os.IsNotExist(err) {
  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. if ParamWwwDir[len(ParamWwwDir)-1] != '/' {
  34. ParamWwwDir = ParamWwwDir + "/"
  35. }
  36. // Handle
  37. mux := http.NewServeMux()
  38. mux.HandleFunc("/", handler)
  39. srv := &http.Server{
  40. Addr: fmt.Sprintf("%s:%d", ParamHost, ParamPort),
  41. Handler: mux,
  42. }
  43. stop := make(chan os.Signal)
  44. signal.Notify(stop, os.Interrupt)
  45. go func() {
  46. log.Printf("Starting server at %s:%d", ParamHost, ParamPort)
  47. if err := srv.ListenAndServe(); err != nil {
  48. if err != http.ErrServerClosed {
  49. log.Fatal(err)
  50. }
  51. }
  52. }()
  53. // Wait for signal
  54. <-stop
  55. log.Printf("Shutting down server...\n")
  56. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  57. defer cancel()
  58. if err := srv.Shutdown(ctx); err != nil {
  59. log.Fatal(err)
  60. } else {
  61. log.Printf("Server is off!")
  62. }
  63. }
  64. func vhExists(vhosthome string) bool {
  65. if st, err := os.Stat(vhosthome); !os.IsNotExist(err) {
  66. if err == nil {
  67. fmode := st.Mode()
  68. if fmode.IsDir() {
  69. return true
  70. }
  71. }
  72. }
  73. return false
  74. }
  75. func handler(w http.ResponseWriter, r *http.Request) {
  76. // Build vhost home dir
  77. host := r.Host
  78. port := strconv.Itoa(ParamPort)
  79. index := strings.Index(host, ":")
  80. if index > -1 {
  81. port = host[index+1:]
  82. host = host[0:index]
  83. }
  84. // Cut "www" if exists
  85. if strings.HasPrefix(host, "www.") {
  86. host = host[4:]
  87. }
  88. VhostHomeDir = ParamWwwDir + host
  89. // Check if virtual host exists
  90. if !vhExists(VhostHomeDir) {
  91. host = "localhost"
  92. VhostHomeDir = ParamWwwDir + host
  93. }
  94. // Set protocol
  95. r.URL.Scheme = "http"
  96. // Set server name
  97. w.Header().Set("Server", "fave.pro")
  98. // Create and start engine
  99. wrapper.New(&w, r, host, port, ParamWwwDir, VhostHomeDir, C_Debug).
  100. Run(func(wrapper *wrapper.Wrapper) bool {
  101. // Actions
  102. action := actions.New(wrapper)
  103. if action.Call() {
  104. wrapper.Session.Save()
  105. return true
  106. }
  107. // Pages
  108. if !(wrapper.R.URL.Path == "/cp" || strings.HasPrefix(wrapper.R.URL.Path, "/cp/")) {
  109. return handleFrontEnd(wrapper)
  110. } else {
  111. return handleBackEnd(wrapper)
  112. }
  113. })
  114. }