diff --git a/main.go b/main.go index 86da5c5..cfe77a5 100644 --- a/main.go +++ b/main.go @@ -23,125 +23,125 @@ var appArgs args.AppArgs func main() { arg.MustParse(&appArgs) - defer appLog.Global.Get(appLog.SYSTEM).Debug("Exit from application") if appArgs.Migration != nil && appArgs.Migration.Create != nil { - settings.Init() - - if err := migration.CreateMigration(appArgs.Migration.Create.Name); err != nil { - panic(err) - } - + runMigrationCreateCommand() return } if appArgs.Server != nil { - settings.Init() - - migration.RunMigration() - server.Run(appArgs.Server) - + runServerCommand() return } - // TODO: Decompose document generation logic into separate methods - // Current code block handles both generation and serving logic - should be separated - if appArgs.Docs == nil { + if appArgs.Docs != nil { + runDocumentationCommand() return } +} +func runMigrationCreateCommand() { + settings.Init() + if err := migration.CreateMigration(appArgs.Migration.Create.Name); err != nil { + panic(err) + } +} + +func runServerCommand() { + settings.Init() + migration.RunMigration() + server.Run(appArgs.Server) +} + +func runDocumentationCommand() { if appArgs.Docs.Generate != nil { - documentate, err := router.Documentate() - if err != nil { - appLog.Global.Get(appLog.SYSTEM).Error(err) - - return - } - - var file []byte - - switch appArgs.Docs.Generate.Format { - case "json": - file, err = documentate.MarshalJSON() - case "yaml": - file, err = documentate.MarshalYAML() - } - - if err != nil { - appLog.Global.Get(appLog.SYSTEM).Error(err) - - return - } - - if appArgs.Docs.Generate.Out == "stdout" { - _, err = os.Stdout.Write(file) - if err != nil { - appLog.Global.Get(appLog.SYSTEM).Error(err) - - return - } - - return - } - - err = os.WriteFile(appArgs.Docs.Generate.Out, file, os.ModeAppend) - if err != nil { - appLog.Global.Get(appLog.SYSTEM).Error(err) - - return - } - + runDocsGenerationCommand() return } if appArgs.Docs.Serve != nil { - documentate, err := router.Documentate() - if err != nil { - appLog.Global.Get(appLog.SYSTEM).Error(err) - - return - } - docsJson, err := documentate.MarshalJSON() - if err != nil { - appLog.Global.Get(appLog.SYSTEM).Error(err) - - return - } - - html, err := htmlFolder.ReadFile("html/redoc.html") - if err != nil { - appLog.Global.Get(appLog.SYSTEM).Error(err) - - return - } - - g := gin.New() - g.Handle("GET", "/static-doc", func(c *gin.Context) { - c.Writer.Header().Add("Content-type", "application/json") - _, err = c.Writer.Write(docsJson) - if err != nil { - c.Writer.WriteHeader(http.StatusInternalServerError) - - return - } - }) - g.Handle("GET", "/docs/index.html", func(c *gin.Context) { - c.Writer.Header().Add("Content-Type", "text/html") - _, err = c.Writer.Write(html) - if err != nil { - c.Writer.WriteHeader(http.StatusInternalServerError) - - return - } - }) - - if err = g.Run(fmt.Sprintf(":%s", appArgs.Docs.Serve.Port)); err != nil { - appLog.Global.Get(appLog.SYSTEM).Error(err) - - return - } - + runDocsServingCommand() + return + } +} + +func runDocsGenerationCommand() { + documentate, err := router.Documentate() + if err != nil { + appLog.Global.Get(appLog.SYSTEM).Error(err) + return + } + + var file []byte + switch appArgs.Docs.Generate.Format { + case "json": + file, err = documentate.MarshalJSON() + case "yaml": + file, err = documentate.MarshalYAML() + } + + if err != nil { + appLog.Global.Get(appLog.SYSTEM).Error(err) + return + } + + if appArgs.Docs.Generate.Out == "stdout" { + _, err = os.Stdout.Write(file) + if err != nil { + appLog.Global.Get(appLog.SYSTEM).Error(err) + return + } + return + } + + err = os.WriteFile(appArgs.Docs.Generate.Out, file, os.ModeAppend) + if err != nil { + appLog.Global.Get(appLog.SYSTEM).Error(err) + return + } +} + +func runDocsServingCommand() { + documentate, err := router.Documentate() + if err != nil { + appLog.Global.Get(appLog.SYSTEM).Error(err) + return + } + + docsJson, err := documentate.MarshalJSON() + if err != nil { + appLog.Global.Get(appLog.SYSTEM).Error(err) + return + } + + html, err := htmlFolder.ReadFile("html/redoc.html") + if err != nil { + appLog.Global.Get(appLog.SYSTEM).Error(err) + return + } + + g := gin.New() + g.Handle("GET", "/static-doc", func(c *gin.Context) { + c.Writer.Header().Add("Content-type", "application/json") + _, err = c.Writer.Write(docsJson) + if err != nil { + c.Writer.WriteHeader(http.StatusInternalServerError) + return + } + }) + + g.Handle("GET", "/docs/index.html", func(c *gin.Context) { + c.Writer.Header().Add("Content-Type", "text/html") + _, err = c.Writer.Write(html) + if err != nil { + c.Writer.WriteHeader(http.StatusInternalServerError) + return + } + }) + + if err = g.Run(fmt.Sprintf(":%s", appArgs.Docs.Serve.Port)); err != nil { + appLog.Global.Get(appLog.SYSTEM).Error(err) return } } diff --git a/modules/scheduler/scheduler.go b/modules/scheduler/scheduler.go index f362e25..99a3d73 100644 --- a/modules/scheduler/scheduler.go +++ b/modules/scheduler/scheduler.go @@ -44,7 +44,12 @@ func NewAppScheduler() (AppScheduler, error) { }, nil } -func (s *appScheduler) NewServiceJob(serviceID uint64, jd gocron.JobDefinition, t gocron.Task, opt ...gocron.JobOption) (gocron.Job, error) { +func (s *appScheduler) NewServiceJob( + serviceID uint64, + jd gocron.JobDefinition, + t gocron.Task, + opt ...gocron.JobOption, +) (gocron.Job, error) { job, err := s.scheduler.NewJob(jd, t, opt...) if err != nil { return nil, err diff --git a/router/controller/auth/plain.go b/router/controller/auth/plain.go index f3acde0..4b0c1ef 100644 --- a/router/controller/auth/plain.go +++ b/router/controller/auth/plain.go @@ -19,7 +19,11 @@ func (c *PlainAuthController) Handler() gin.HandlerFunc { var payload dto.LoginRequest if err := ginCtx.ShouldBindJSON(&payload); err != nil { - sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(ginCtx) + sendErr := httpApp. + NewResponseErrBuilder(). + WithStatusCode(http.StatusBadRequest). + WithMessage(err.Error()). + Send(ginCtx) if sendErr != nil { ginCtx.Writer.WriteHeader(http.StatusInternalServerError) return @@ -30,7 +34,11 @@ func (c *PlainAuthController) Handler() gin.HandlerFunc { jwtString, err := c.authModule.Proceed(ginCtx.Request.Context(), payload.Login, payload.Password) if err != nil { - sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(ginCtx) + sendErr := httpApp. + NewResponseErrBuilder(). + WithStatusCode(http.StatusBadRequest). + WithMessage(err.Error()). + Send(ginCtx) if sendErr != nil { ginCtx.Writer.WriteHeader(http.StatusInternalServerError) return diff --git a/router/controller/service/common.go b/router/controller/service/common.go new file mode 100644 index 0000000..6975af1 --- /dev/null +++ b/router/controller/service/common.go @@ -0,0 +1,30 @@ +package service + +import ( + "net/http" + + "git.ostiwe.com/ostiwe-com/status/modules/log" + http2 "git.ostiwe.com/ostiwe-com/status/pkg/http" + "git.ostiwe.com/ostiwe-com/status/repository" + "git.ostiwe.com/ostiwe-com/status/transform" + "github.com/gin-gonic/gin" +) + +func processGetServicesHandler(c *gin.Context, publicOnly bool, serviceRepository repository.Service) { + items, err := serviceRepository.All(c.Request.Context(), -1, 0, publicOnly) + if err != nil { + log.Global.Get(log.SERVER).Error(err) + + writeErr := http2.NewResponseErrBuilder(). + WithMessage("Fetch service error"). + WithStatusCode(http.StatusInternalServerError). + Send(c) + if writeErr != nil { + log.Global.Get(log.SERVER).Error(writeErr) + } + + return + } + + c.JSON(http.StatusOK, transform.PublicServices(items...)) +} diff --git a/router/controller/service/get.go b/router/controller/service/get.go index e0d7080..57a5180 100644 --- a/router/controller/service/get.go +++ b/router/controller/service/get.go @@ -4,12 +4,9 @@ import ( "net/http" "git.ostiwe.com/ostiwe-com/status/modules/jwt" - "git.ostiwe.com/ostiwe-com/status/modules/log" - http2 "git.ostiwe.com/ostiwe-com/status/pkg/http" "git.ostiwe.com/ostiwe-com/status/repository" "git.ostiwe.com/ostiwe-com/status/router/controller" "git.ostiwe.com/ostiwe-com/status/router/midlleware" - "git.ostiwe.com/ostiwe-com/status/transform" "github.com/gin-gonic/gin" ) @@ -25,22 +22,7 @@ func (g *GetServices) New() controller.Controller { func (g *GetServices) Handler() gin.HandlerFunc { return func(c *gin.Context) { - items, err := g.serviceRepository.All(c.Request.Context(), -1, 0, true) - if err != nil { - log.Global.Get(log.SERVER).Error(err) - - writeErr := http2.NewResponseErrBuilder(). - WithMessage("Fetch service error"). - WithStatusCode(http.StatusInternalServerError). - Send(c) - if writeErr != nil { - log.Global.Get(log.SERVER).Error(writeErr) - } - - return - } - - c.JSON(http.StatusOK, transform.PublicServices(items...)) + processGetServicesHandler(c, false, g.serviceRepository) } } diff --git a/router/controller/service/public_get.go b/router/controller/service/public_get.go index cdc54e5..3f83ea8 100644 --- a/router/controller/service/public_get.go +++ b/router/controller/service/public_get.go @@ -3,11 +3,8 @@ package service import ( "net/http" - "git.ostiwe.com/ostiwe-com/status/modules/log" - http2 "git.ostiwe.com/ostiwe-com/status/pkg/http" "git.ostiwe.com/ostiwe-com/status/repository" "git.ostiwe.com/ostiwe-com/status/router/controller" - "git.ostiwe.com/ostiwe-com/status/transform" "github.com/gin-gonic/gin" ) @@ -23,22 +20,7 @@ func (p *PublicGetServicesController) New() controller.Controller { func (p *PublicGetServicesController) Handler() gin.HandlerFunc { return func(c *gin.Context) { - items, err := p.serviceRepository.All(c.Request.Context(), -1, 0, true) - if err != nil { - log.Global.Get(log.SERVER).Error(err) - - writeErr := http2.NewResponseErrBuilder(). - WithMessage("Fetch service error"). - WithStatusCode(http.StatusInternalServerError). - Send(c) - if writeErr != nil { - log.Global.Get(log.SERVER).Error(writeErr) - } - - return - } - - c.JSON(http.StatusOK, transform.PublicServices(items...)) + processGetServicesHandler(c, true, p.serviceRepository) } } diff --git a/router/server.go b/router/server.go index 8a657a2..aee3f4f 100644 --- a/router/server.go +++ b/router/server.go @@ -58,7 +58,10 @@ func InitRoutes() *gin.Engine { } log.Global.Get(log.SERVER).Info(fmt.Sprintf("Initialized %d routers", len(ctrlList))) - log.Global.Get(log.SERVER).Info(fmt.Sprintf("Setting up routers is done for %dms, start server", time.Since(startTime).Milliseconds())) + log.Global.Get(log.SERVER).Info(fmt.Sprintf( + "Setting up routers is done for %dms, start server", + time.Since(startTime).Milliseconds(), + )) return r } diff --git a/service/check.go b/service/check.go index f93eae7..28583e3 100644 --- a/service/check.go +++ b/service/check.go @@ -17,8 +17,7 @@ import ( ) var ( - httpObserveFailed = errors.New("http observe fail") - httpObserveMaxTries = errors.New("http observe max tries") + errHttpObserveFailed = errors.New("http observe fail") GlobalCheckService Check ) @@ -111,7 +110,12 @@ func (c *check) Observe(ctx context.Context, srv *model.Service) error { return nil } -func (c *check) RegisterStatus(ctx context.Context, serviceID uint64, status model.StatusCode, meta ResponseMeta) error { +func (c *check) RegisterStatus( + ctx context.Context, + serviceID uint64, + status model.StatusCode, + meta ResponseMeta, +) error { return c.statusRepository.Add(ctx, model.Status{ ServiceID: serviceID, Status: status, @@ -163,7 +167,7 @@ func (c *check) ObserveHttp(ctx context.Context, service *model.Service) (*Respo } if response.StatusCode != http.StatusOK { - return meta, httpObserveFailed + return meta, errHttpObserveFailed } return meta, nil