WIP: Develop #1
72
main.go
72
main.go
@@ -23,44 +23,57 @@ var appArgs args.AppArgs
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
arg.MustParse(&appArgs)
|
arg.MustParse(&appArgs)
|
||||||
|
|
||||||
defer appLog.Global.Get(appLog.SYSTEM).Debug("Exit from application")
|
defer appLog.Global.Get(appLog.SYSTEM).Debug("Exit from application")
|
||||||
|
|
||||||
if appArgs.Migration != nil && appArgs.Migration.Create != nil {
|
if appArgs.Migration != nil && appArgs.Migration.Create != nil {
|
||||||
settings.Init()
|
runMigrationCreateCommand()
|
||||||
|
|
||||||
if err := migration.CreateMigration(appArgs.Migration.Create.Name); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if appArgs.Server != nil {
|
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()
|
migration.RunMigration()
|
||||||
server.Run(appArgs.Server)
|
server.Run(appArgs.Server)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
func runDocumentationCommand() {
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
if appArgs.Docs.Generate != nil {
|
if appArgs.Docs.Generate != nil {
|
||||||
|
runDocsGenerationCommand()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if appArgs.Docs.Serve != nil {
|
||||||
|
runDocsServingCommand()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runDocsGenerationCommand() {
|
||||||
documentate, err := router.Documentate()
|
documentate, err := router.Documentate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var file []byte
|
var file []byte
|
||||||
|
|
||||||
switch appArgs.Docs.Generate.Format {
|
switch appArgs.Docs.Generate.Format {
|
||||||
case "json":
|
case "json":
|
||||||
file, err = documentate.MarshalJSON()
|
file, err = documentate.MarshalJSON()
|
||||||
@@ -70,7 +83,6 @@ func main() {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,41 +90,34 @@ func main() {
|
|||||||
_, err = os.Stdout.Write(file)
|
_, err = os.Stdout.Write(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile(appArgs.Docs.Generate.Out, file, os.ModeAppend)
|
err = os.WriteFile(appArgs.Docs.Generate.Out, file, os.ModeAppend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return
|
func runDocsServingCommand() {
|
||||||
}
|
|
||||||
|
|
||||||
if appArgs.Docs.Serve != nil {
|
|
||||||
documentate, err := router.Documentate()
|
documentate, err := router.Documentate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
docsJson, err := documentate.MarshalJSON()
|
docsJson, err := documentate.MarshalJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
html, err := htmlFolder.ReadFile("html/redoc.html")
|
html, err := htmlFolder.ReadFile("html/redoc.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,26 +127,21 @@ func main() {
|
|||||||
_, err = c.Writer.Write(docsJson)
|
_, err = c.Writer.Write(docsJson)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Writer.WriteHeader(http.StatusInternalServerError)
|
c.Writer.WriteHeader(http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
g.Handle("GET", "/docs/index.html", func(c *gin.Context) {
|
g.Handle("GET", "/docs/index.html", func(c *gin.Context) {
|
||||||
c.Writer.Header().Add("Content-Type", "text/html")
|
c.Writer.Header().Add("Content-Type", "text/html")
|
||||||
_, err = c.Writer.Write(html)
|
_, err = c.Writer.Write(html)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Writer.WriteHeader(http.StatusInternalServerError)
|
c.Writer.WriteHeader(http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if err = g.Run(fmt.Sprintf(":%s", appArgs.Docs.Serve.Port)); err != nil {
|
if err = g.Run(fmt.Sprintf(":%s", appArgs.Docs.Serve.Port)); err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,12 @@ func NewAppScheduler() (AppScheduler, error) {
|
|||||||
}, nil
|
}, 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...)
|
job, err := s.scheduler.NewJob(jd, t, opt...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -19,7 +19,11 @@ func (c *PlainAuthController) Handler() gin.HandlerFunc {
|
|||||||
var payload dto.LoginRequest
|
var payload dto.LoginRequest
|
||||||
|
|
||||||
if err := ginCtx.ShouldBindJSON(&payload); err != nil {
|
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 {
|
if sendErr != nil {
|
||||||
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
|
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -30,7 +34,11 @@ func (c *PlainAuthController) Handler() gin.HandlerFunc {
|
|||||||
|
|
||||||
jwtString, err := c.authModule.Proceed(ginCtx.Request.Context(), payload.Login, payload.Password)
|
jwtString, err := c.authModule.Proceed(ginCtx.Request.Context(), payload.Login, payload.Password)
|
||||||
if err != nil {
|
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 {
|
if sendErr != nil {
|
||||||
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
|
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|||||||
30
router/controller/service/common.go
Normal file
30
router/controller/service/common.go
Normal 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...))
|
||||||
|
}
|
||||||
@@ -4,12 +4,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.ostiwe.com/ostiwe-com/status/modules/jwt"
|
"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/repository"
|
||||||
"git.ostiwe.com/ostiwe-com/status/router/controller"
|
"git.ostiwe.com/ostiwe-com/status/router/controller"
|
||||||
"git.ostiwe.com/ostiwe-com/status/router/midlleware"
|
"git.ostiwe.com/ostiwe-com/status/router/midlleware"
|
||||||
"git.ostiwe.com/ostiwe-com/status/transform"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,22 +22,7 @@ func (g *GetServices) New() controller.Controller {
|
|||||||
|
|
||||||
func (g *GetServices) Handler() gin.HandlerFunc {
|
func (g *GetServices) Handler() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
items, err := g.serviceRepository.All(c.Request.Context(), -1, 0, true)
|
processGetServicesHandler(c, false, g.serviceRepository)
|
||||||
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...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,8 @@ package service
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"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/repository"
|
||||||
"git.ostiwe.com/ostiwe-com/status/router/controller"
|
"git.ostiwe.com/ostiwe-com/status/router/controller"
|
||||||
"git.ostiwe.com/ostiwe-com/status/transform"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,22 +20,7 @@ func (p *PublicGetServicesController) New() controller.Controller {
|
|||||||
|
|
||||||
func (p *PublicGetServicesController) Handler() gin.HandlerFunc {
|
func (p *PublicGetServicesController) Handler() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
items, err := p.serviceRepository.All(c.Request.Context(), -1, 0, true)
|
processGetServicesHandler(c, true, p.serviceRepository)
|
||||||
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...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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("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
|
return r
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
httpObserveFailed = errors.New("http observe fail")
|
errHttpObserveFailed = errors.New("http observe fail")
|
||||||
httpObserveMaxTries = errors.New("http observe max tries")
|
|
||||||
|
|
||||||
GlobalCheckService Check
|
GlobalCheckService Check
|
||||||
)
|
)
|
||||||
@@ -111,7 +110,12 @@ func (c *check) Observe(ctx context.Context, srv *model.Service) error {
|
|||||||
return nil
|
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{
|
return c.statusRepository.Add(ctx, model.Status{
|
||||||
ServiceID: serviceID,
|
ServiceID: serviceID,
|
||||||
Status: status,
|
Status: status,
|
||||||
@@ -163,7 +167,7 @@ func (c *check) ObserveHttp(ctx context.Context, service *model.Service) (*Respo
|
|||||||
}
|
}
|
||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
if response.StatusCode != http.StatusOK {
|
||||||
return meta, httpObserveFailed
|
return meta, errHttpObserveFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
return meta, nil
|
return meta, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user