main.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "github.com/0xJacky/Nginx-UI/server/analytic"
  6. "github.com/0xJacky/Nginx-UI/server/model"
  7. "github.com/0xJacky/Nginx-UI/server/router"
  8. "github.com/0xJacky/Nginx-UI/server/settings"
  9. "github.com/0xJacky/Nginx-UI/server/tool"
  10. "github.com/gin-gonic/gin"
  11. "log"
  12. "mime"
  13. "net/http"
  14. "os/signal"
  15. "syscall"
  16. "time"
  17. )
  18. func main() {
  19. // Create context that listens for the interrupt signal from the OS.
  20. ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
  21. defer stop()
  22. // Hack: fix wrong Content Type of .js file on some OS platforms
  23. // See https://github.com/golang/go/issues/32350
  24. _ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8")
  25. var confPath string
  26. flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file")
  27. flag.Parse()
  28. gin.SetMode(settings.ServerSettings.RunMode)
  29. settings.Init(confPath)
  30. log.Printf("nginx config dir path: %s", tool.GetNginxConfPath(""))
  31. if "" != settings.ServerSettings.JwtSecret {
  32. model.Init()
  33. go tool.AutoCert()
  34. go analytic.RecordServerAnalytic()
  35. }
  36. srv := &http.Server{
  37. Addr: ":" + settings.ServerSettings.HttpPort,
  38. Handler: router.InitRouter(),
  39. }
  40. // Initializing the server in a goroutine so that
  41. // it won't block the graceful shutdown handling below
  42. go func() {
  43. if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  44. log.Fatalf("listen: %s\n", err)
  45. }
  46. }()
  47. // Listen for the interrupt signal.
  48. <-ctx.Done()
  49. // Restore default behavior on the interrupt signal and notify user of shutdown.
  50. stop()
  51. log.Println("shutting down gracefully, press Ctrl+C again to force")
  52. // The context is used to inform the server it has 5 seconds to finish
  53. // the request it is currently handling
  54. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  55. defer cancel()
  56. if err := srv.Shutdown(ctx); err != nil {
  57. log.Fatal("Server forced to shutdown: ", err)
  58. }
  59. log.Println("Server exiting")
  60. }