main.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/0xJacky/Nginx-UI/internal/cmd"
  8. "github.com/0xJacky/Nginx-UI/internal/kernel"
  9. "github.com/0xJacky/Nginx-UI/model"
  10. "github.com/0xJacky/Nginx-UI/router"
  11. "github.com/0xJacky/Nginx-UI/settings"
  12. "github.com/gin-gonic/gin"
  13. "github.com/jpillora/overseer"
  14. "github.com/uozi-tech/cosy"
  15. cKernel "github.com/uozi-tech/cosy/kernel"
  16. "github.com/uozi-tech/cosy/logger"
  17. cRouter "github.com/uozi-tech/cosy/router"
  18. cSettings "github.com/uozi-tech/cosy/settings"
  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. appCmd := cmd.NewAppCmd()
  52. confPath := appCmd.String("config")
  53. settings.Init(confPath)
  54. overseer.Run(overseer.Config{
  55. Program: Program(confPath),
  56. Address: fmt.Sprintf("%s:%d", cSettings.ServerSettings.Host, cSettings.ServerSettings.Port),
  57. TerminateTimeout: 5 * time.Second,
  58. })
  59. }