From 68c98bb038244a7c32539785e25aa88614127c91 Mon Sep 17 00:00:00 2001 From: ostiwe Date: Sun, 10 Aug 2025 22:13:42 +0300 Subject: [PATCH] refactor(server): Move server to package --- main.go | 76 +++++++----------------------------------------- pkg/args/args.go | 22 ++++++++++++++ server/server.go | 47 ++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 65 deletions(-) create mode 100644 pkg/args/args.go create mode 100644 server/server.go diff --git a/main.go b/main.go index 705e64c..e6cc9d3 100644 --- a/main.go +++ b/main.go @@ -3,78 +3,31 @@ package main import ( "fmt" "net/http" - "time" - "git.ostiwe.com/ostiwe-com/status/model" - "git.ostiwe.com/ostiwe-com/status/modules/db" appLog "git.ostiwe.com/ostiwe-com/status/modules/log" + "git.ostiwe.com/ostiwe-com/status/pkg/args" "git.ostiwe.com/ostiwe-com/status/router" - "git.ostiwe.com/ostiwe-com/status/service" - "git.ostiwe.com/ostiwe-com/status/version" + "git.ostiwe.com/ostiwe-com/status/server" + _ "git.ostiwe.com/ostiwe-com/status/settings" "github.com/alexflint/go-arg" "github.com/go-chi/chi/v5" - "github.com/sirupsen/logrus" - - _ "git.ostiwe.com/ostiwe-com/status/settings" ) -type ServerCmd struct { - Port string `arg:"-p,--port" help:"Port to listen on" default:"8080"` -} - -type ServerDocumentationCmd struct { - Port string `arg:"-p,--port" help:"Port to listen on" default:"8081"` - Plain bool `arg:"--plain" help:"Enable plain text output" default:"true"` - PlainFormat string `arg:"--plain-format" help:"Set format for output (json, yaml)" default:"yaml"` -} - -type appArgs struct { - Server *ServerCmd `arg:"subcommand:server" help:"Start the api server"` - ServerDocumentation *ServerDocumentationCmd `arg:"subcommand:server-docs" help:"Generate documentation for api server"` -} - -var args appArgs - -func (appArgs) Version() string { - return version.AppVersion() -} +var appArgs args.AppArgs func main() { - arg.MustParse(&args) - - if args.Server != nil { - connect, err := db.Connect() - if err != nil { - appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed connect to database: %v", err)) - return - } - - db.SetGlobal(connect) - appLog.Global.Get(appLog.SYSTEM).Info("Run db migration") - if err = runMigrate(); err != nil { - appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Migration failed, error: %v", err)) - return - } - - appLog.Global.Get(appLog.SYSTEM).Info("Start service observer") - go service.NewCheck(20).Start(time.Second * 10) - - appLog.Global.Put(appLog.SERVER, logrus.New()) - appLog.Global.Get(appLog.SERVER).Info("Startup server on port: ", args.Server.Port) - - err = http.ListenAndServe(fmt.Sprintf(":%s", args.Server.Port), router.InitRoutes()) - if err != nil { - appLog.Global.Get(appLog.SERVER).Error(fmt.Sprintf("Startup server error: %v", err)) - } + arg.MustParse(&appArgs) + if appArgs.Server != nil { + server.Run(appArgs.Server) return } - if args.ServerDocumentation != nil { + if appArgs.ServerDocumentation != nil { appLog.Global.Get(appLog.SYSTEM).Info("Collect documentation") docs := router.Documentate() - if !args.ServerDocumentation.Plain { + if !appArgs.ServerDocumentation.Plain { chiRouter := chi.NewRouter() err := docs.SetupRoutes(chiRouter, docs) @@ -83,8 +36,8 @@ func main() { return } - appLog.Global.Get(appLog.SYSTEM).Info(fmt.Sprintf("Start documentation server on port: %s", args.ServerDocumentation.Port)) - err = http.ListenAndServe(fmt.Sprintf(":%s", args.ServerDocumentation.Port), chiRouter) + appLog.Global.Get(appLog.SYSTEM).Info(fmt.Sprintf("Start documentation server on port: %s", appArgs.ServerDocumentation.Port)) + err = http.ListenAndServe(fmt.Sprintf(":%s", appArgs.ServerDocumentation.Port), chiRouter) if err != nil { appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error: %v", err)) } @@ -96,10 +49,3 @@ func main() { appLog.Global.Get(appLog.SYSTEM).Info("Exit from application") } - -func runMigrate() error { - return db.Global.AutoMigrate( - model.Service{}, - model.Status{}, - ) -} diff --git a/pkg/args/args.go b/pkg/args/args.go new file mode 100644 index 0000000..8775d4f --- /dev/null +++ b/pkg/args/args.go @@ -0,0 +1,22 @@ +package args + +import "git.ostiwe.com/ostiwe-com/status/version" + +type ServerCmd struct { + Port string `arg:"-p,--port" help:"Port to listen on" default:"8080"` +} + +type ServerDocumentationCmd struct { + Port string `arg:"-p,--port" help:"Port to listen on" default:"8081"` + Plain bool `arg:"--plain" help:"Enable plain text output" default:"true"` + PlainFormat string `arg:"--plain-format" help:"Set format for output (json, yaml)" default:"yaml"` +} + +type AppArgs struct { + Server *ServerCmd `arg:"subcommand:server" help:"Start the api server"` + ServerDocumentation *ServerDocumentationCmd `arg:"subcommand:server-docs" help:"Generate documentation for api server"` +} + +func (AppArgs) Version() string { + return version.AppVersion() +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..243c8ae --- /dev/null +++ b/server/server.go @@ -0,0 +1,47 @@ +package server + +import ( + "fmt" + "net/http" + + "git.ostiwe.com/ostiwe-com/status/model" + "git.ostiwe.com/ostiwe-com/status/modules/db" + appLog "git.ostiwe.com/ostiwe-com/status/modules/log" + "git.ostiwe.com/ostiwe-com/status/modules/queue" + "git.ostiwe.com/ostiwe-com/status/pkg/args" + "git.ostiwe.com/ostiwe-com/status/router" + "github.com/sirupsen/logrus" +) + +func Run(serverArgs *args.ServerCmd) { + connect, err := db.Connect() + if err != nil { + appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed connect to database: %v", err)) + return + } + + db.SetGlobal(connect) + appLog.Global.Get(appLog.SYSTEM).Info("Run db migration") + if err = runMigrate(); err != nil { + appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Migration failed, error: %v", err)) + return + } + + appLog.Global.Get(appLog.SYSTEM).Info("Start service observer") + go queue.InitQueues() + + appLog.Global.Put(appLog.SERVER, logrus.New()) + appLog.Global.Get(appLog.SERVER).Info("Startup server on port: ", serverArgs.Port) + + err = http.ListenAndServe(fmt.Sprintf(":%s", serverArgs.Port), router.InitRoutes()) + if err != nil { + appLog.Global.Get(appLog.SERVER).Error(fmt.Sprintf("Startup server error: %v", err)) + } +} + +func runMigrate() error { + return db.Global.AutoMigrate( + model.Service{}, + model.Status{}, + ) +}