main.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/0xJacky/Nginx-UI/internal/kernel"
  6. "github.com/0xJacky/Nginx-UI/model"
  7. "github.com/0xJacky/Nginx-UI/router"
  8. "github.com/0xJacky/Nginx-UI/settings"
  9. "github.com/gin-gonic/gin"
  10. "github.com/jpillora/overseer"
  11. "github.com/pkg/errors"
  12. "github.com/uozi-tech/cosy"
  13. cKernel "github.com/uozi-tech/cosy/kernel"
  14. "github.com/uozi-tech/cosy/logger"
  15. cRouter "github.com/uozi-tech/cosy/router"
  16. cSettings "github.com/uozi-tech/cosy/settings"
  17. "net/http"
  18. "time"
  19. )
  20. func Program(confPath string) func(state overseer.State) {
  21. return func(state overseer.State) {
  22. defer logger.Sync()
  23. defer logger.Info("Server exited")
  24. cosy.RegisterModels(model.GenerateAllModel()...)
  25. cosy.RegisterInitFunc(kernel.Boot, router.InitRouter)
  26. // Initialize settings package
  27. settings.Init(confPath)
  28. // Set gin mode
  29. gin.SetMode(cSettings.ServerSettings.RunMode)
  30. // Initialize logger package
  31. logger.Init(cSettings.ServerSettings.RunMode)
  32. defer logger.Sync()
  33. if state.Listener == nil {
  34. return
  35. }
  36. // Gin router initialization
  37. cRouter.Init()
  38. // Kernel boot
  39. cKernel.Boot()
  40. addr := fmt.Sprintf("%s:%d", cSettings.ServerSettings.Host, cSettings.ServerSettings.Port)
  41. srv := &http.Server{
  42. Addr: addr,
  43. Handler: cRouter.GetEngine(),
  44. }
  45. if err := srv.Serve(state.Listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
  46. logger.Fatalf("listen: %s\n", err)
  47. }
  48. }
  49. }
  50. func main() {
  51. var confPath string
  52. flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file")
  53. flag.Parse()
  54. settings.Init(confPath)
  55. overseer.Run(overseer.Config{
  56. Program: Program(confPath),
  57. Address: fmt.Sprintf("%s:%d", cSettings.ServerSettings.Host, cSettings.ServerSettings.Port),
  58. TerminateTimeout: 5 * time.Second,
  59. })
  60. }