diff --git a/main.go b/main.go index cfe77a5..94932b6 100644 --- a/main.go +++ b/main.go @@ -27,16 +27,19 @@ func main() { if appArgs.Migration != nil && appArgs.Migration.Create != nil { runMigrationCreateCommand() + return } if appArgs.Server != nil { runServerCommand() + return } if appArgs.Docs != nil { runDocumentationCommand() + return } } @@ -57,11 +60,13 @@ func runServerCommand() { func runDocumentationCommand() { if appArgs.Docs.Generate != nil { runDocsGenerationCommand() + return } if appArgs.Docs.Serve != nil { runDocsServingCommand() + return } } @@ -70,6 +75,7 @@ func runDocsGenerationCommand() { documentate, err := router.Documentate() if err != nil { appLog.Global.Get(appLog.SYSTEM).Error(err) + return } @@ -83,6 +89,7 @@ func runDocsGenerationCommand() { if err != nil { appLog.Global.Get(appLog.SYSTEM).Error(err) + return } @@ -90,14 +97,17 @@ func runDocsGenerationCommand() { _, 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 } } @@ -106,18 +116,21 @@ 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 } @@ -127,6 +140,7 @@ func runDocsServingCommand() { _, err = c.Writer.Write(docsJson) if err != nil { c.Writer.WriteHeader(http.StatusInternalServerError) + return } }) @@ -136,12 +150,14 @@ func runDocsServingCommand() { _, 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/model/service.go b/model/service.go index c8b1ada..0433c51 100644 --- a/model/service.go +++ b/model/service.go @@ -7,33 +7,34 @@ type HTTPConfig struct { Headers map[string]string `json:"headers"` } +// ServiceTypeCheckConfig +// MaxFails - max "ping" fails, after with the service marked as unavailable +// Interval - interval between "ping" in seconds +// Timeout - interval after which the task will be canceled type ServiceTypeCheckConfig struct { Version string `json:"version"` HTTPConfig *HTTPConfig `json:"httpConfig"` - // MaxFails - max "ping" fails, after with the service marked as unavailable - MaxFails uint8 `json:"maxFails"` - // Interval - interval between "ping" in seconds - Interval uint64 `json:"interval"` - // Timeout - interval after which the task will be canceled - Timeout uint64 `json:"timeout"` + MaxFails uint8 `json:"maxFails"` + Interval uint64 `json:"interval"` + Timeout uint64 `json:"timeout"` } +// Service +// ID Unique ID for entity +// Name Human-readable service name +// Description Human-readable service description +// Host to check, for example 192.168.1.44 or https://google.com +// Type for check, for now is TCP or HTTP or something else type Service struct { - // Unique ID for entity - ID uint64 `gorm:"primary_key;auto_increment" json:"id"` - // Human-readable service name - Name string `gorm:"size:255;not null" json:"name"` - // Human-readable service description - Description string `gorm:"size:255" json:"description"` - PublicDescription string `gorm:"size:255" json:"publicDescription"` - Public *bool `gorm:"default:false" json:"public"` - // Host to check, for example 192.168.1.44 or https://google.com - Host string `gorm:"size:255;not null" json:"host"` - // Type for check, for now is TCP or HTTP or something else - Type string `gorm:"size:255;not null" json:"type"` - Config *ServiceTypeCheckConfig `gorm:"serializer:json;column:type_config" json:"typeConfig"` - - Statuses []Status `gorm:"foreignkey:ServiceID" json:"statuses"` + ID uint64 `gorm:"primary_key;auto_increment" json:"id"` + Name string `gorm:"size:255;not null" json:"name"` + Description string `gorm:"size:255" json:"description"` + PublicDescription string `gorm:"size:255" json:"publicDescription"` + Public *bool `gorm:"default:false" json:"public"` + Host string `gorm:"size:255;not null" json:"host"` + Type string `gorm:"size:255;not null" json:"type"` + Config *ServiceTypeCheckConfig `gorm:"serializer:json;column:type_config" json:"typeConfig"` + Statuses []Status `gorm:"foreignkey:ServiceID" json:"statuses"` } func (Service) TableName() string { diff --git a/model/status.go b/model/status.go index 8d3b3c6..3136d0a 100644 --- a/model/status.go +++ b/model/status.go @@ -18,8 +18,8 @@ const ( type Status struct { ID uint64 `gorm:"primary_key;auto_increment" json:"-"` ServiceID uint64 `json:"-"` - Status StatusCode `gorm:"size:255;not null" json:"status"` - Description *string `gorm:"size:255" json:"description"` + Status StatusCode `gorm:"size:255;not null" json:"status"` + Description *string `gorm:"size:255" json:"description"` CreatedAt time.Time `json:"createdAt"` ResponseTime uint64 `json:"responseTime"` } diff --git a/pkg/args/args.go b/pkg/args/args.go index 6ea16fa..ad2051d 100644 --- a/pkg/args/args.go +++ b/pkg/args/args.go @@ -3,7 +3,7 @@ package args import "git.ostiwe.com/ostiwe-com/status/version" type ServerCmd struct { - Port string `arg:"-p,--port,env:APP_PORT" help:"Port to listen on" default:"8080"` + Port string `arg:"-p,--port,env:APP_PORT" default:"8080" help:"Port to listen on"` } type MigrationCreate struct { @@ -15,22 +15,22 @@ type Migration struct { } type ServerDocumentationCmd struct { - Port string `arg:"-p,--port,env:APP_PORT_DOCS" help:"Port to listen on" default:"8081"` + Port string `arg:"-p,--port,env:APP_PORT_DOCS" default:"8081" help:"Port to listen on"` } type GenerateDocumentationCmd struct { - Format string `arg:"--format,-f" help:"Set output format (json, yaml)" default:"yaml"` - Out string `arg:"--out,-o" help:"Output file name (or stdout)" default:"stdout"` + Format string `arg:"--format,-f" default:"yaml" help:"Set output format (json, yaml)"` + Out string `arg:"--out,-o" default:"stdout" help:"Output file name (or stdout)"` } type DocsCmd struct { - Serve *ServerDocumentationCmd `arg:"subcommand:serve" help:"Generate and serve the documentation server"` + Serve *ServerDocumentationCmd `arg:"subcommand:serve" help:"Generate and serve the documentation server"` Generate *GenerateDocumentationCmd `arg:"subcommand:generate" help:"Generate documentation to file"` } type AppArgs struct { - Server *ServerCmd `arg:"subcommand:server" help:"Start the api server"` - Docs *DocsCmd `arg:"subcommand:docs" help:"Generate documentation to file or run documentation server"` + Server *ServerCmd `arg:"subcommand:server" help:"Start the api server"` + Docs *DocsCmd `arg:"subcommand:docs" help:"Generate documentation to file or run documentation server"` Migration *Migration `arg:"subcommand:migration" help:"Migration utils"` } diff --git a/pkg/http/errors.go b/pkg/http/errors.go index e5ba4c0..94dd044 100644 --- a/pkg/http/errors.go +++ b/pkg/http/errors.go @@ -58,11 +58,13 @@ func (r readyResponseErr) Send(response http.ResponseWriter) error { func (r responseErrBuilder) WithTrace(s string) ResponseErrBuilder { r.trace = s + return r } func (r responseErrBuilder) WithStatusCode(i int) ResponseErrBuilder { r.status = i + return r } @@ -72,11 +74,13 @@ func NewResponseErrBuilder() ResponseErrBuilder { func (r responseErrBuilder) WithDetails(m map[string]any) ResponseErrBuilder { r.details = m + return r } func (r responseErrBuilder) WithMessage(s string) ResponseErrBuilder { r.message = s + return r } diff --git a/router/controller/auth/plain.go b/router/controller/auth/plain.go index 4b0c1ef..d8c29ed 100644 --- a/router/controller/auth/plain.go +++ b/router/controller/auth/plain.go @@ -26,6 +26,7 @@ func (c *PlainAuthController) Handler() gin.HandlerFunc { Send(ginCtx) if sendErr != nil { ginCtx.Writer.WriteHeader(http.StatusInternalServerError) + return } @@ -41,6 +42,7 @@ func (c *PlainAuthController) Handler() gin.HandlerFunc { Send(ginCtx) if sendErr != nil { ginCtx.Writer.WriteHeader(http.StatusInternalServerError) + return } diff --git a/router/controller/ping/controller.go b/router/controller/ping/controller.go index 084c712..b68f0c5 100644 --- a/router/controller/ping/controller.go +++ b/router/controller/ping/controller.go @@ -30,6 +30,7 @@ func (c *Controller) Handler() gin.HandlerFunc { _, err := ginCtx.Writer.Write([]byte("pong")) if err != nil { log.Global.Get(log.SERVER).Error(err) + return } } diff --git a/router/midlleware/user.go b/router/midlleware/user.go index ba3bea3..b41575e 100644 --- a/router/midlleware/user.go +++ b/router/midlleware/user.go @@ -23,6 +23,7 @@ func SetUserFromJWT() gin.HandlerFunc { subject, err := token.Claims.GetSubject() if err != nil { c.Writer.WriteHeader(http.StatusUnauthorized) + return } diff --git a/router/server.go b/router/server.go index aee3f4f..1d93bd4 100644 --- a/router/server.go +++ b/router/server.go @@ -89,7 +89,6 @@ func Documentate() (*openapi3.Spec, error) { if err != nil { return nil, err } - } return reflector.Spec, nil diff --git a/server/server.go b/server/server.go index 307b0a4..db9582c 100644 --- a/server/server.go +++ b/server/server.go @@ -18,6 +18,7 @@ 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 } @@ -26,6 +27,7 @@ func Run(serverArgs *args.ServerCmd) { scheduler.GlobalAppScheduler, err = scheduler.NewAppScheduler() if err != nil { appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed create scheduler: %v", err)) + return } @@ -34,11 +36,13 @@ func Run(serverArgs *args.ServerCmd) { if err = service.GlobalCheckService.RegisterTasks(context.Background()); err != nil { appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed create observe jobs: %v", err)) + return } if err = scheduler.GlobalAppScheduler.StartJobs(); err != nil { appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed start jobs: %v", err)) + return } diff --git a/transform/service.go b/transform/service.go index 41cf4fa..1ad7985 100644 --- a/transform/service.go +++ b/transform/service.go @@ -33,7 +33,6 @@ func PublicServices(items ...model.Service) []dto.PublicService { } wg.Wait() - } return result @@ -57,6 +56,7 @@ func PublicService(item model.Service) dto.PublicService { } slices.Reverse(statuses) + return dto.PublicService{ ID: int(item.ID), Name: item.Name,