refactor: Fix linter errors

This commit is contained in:
2025-11-04 19:52:26 +03:00
parent 8ca762cb1f
commit 180a5c2a90
8 changed files with 161 additions and 147 deletions

72
main.go
View File

@@ -23,44 +23,57 @@ 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()
runServerCommand()
return
}
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)
}
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 {
return
}
func runDocumentationCommand() {
if appArgs.Docs.Generate != nil {
runDocsGenerationCommand()
return
}
if appArgs.Docs.Serve != nil {
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()
@@ -70,7 +83,6 @@ func main() {
if err != nil {
appLog.Global.Get(appLog.SYSTEM).Error(err)
return
}
@@ -78,41 +90,34 @@ func main() {
_, 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
}
}
return
}
if appArgs.Docs.Serve != nil {
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
}
@@ -122,26 +127,21 @@ func main() {
_, 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
}
return
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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...))
}

View File

@@ -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)
}
}

View File

@@ -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)
}
}

View File

@@ -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
}

View File

@@ -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