WIP: Develop #1
16
main.go
16
main.go
@@ -27,16 +27,19 @@ func main() {
|
|||||||
|
|
||||||
if appArgs.Migration != nil && appArgs.Migration.Create != nil {
|
if appArgs.Migration != nil && appArgs.Migration.Create != nil {
|
||||||
runMigrationCreateCommand()
|
runMigrationCreateCommand()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if appArgs.Server != nil {
|
if appArgs.Server != nil {
|
||||||
runServerCommand()
|
runServerCommand()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if appArgs.Docs != nil {
|
if appArgs.Docs != nil {
|
||||||
runDocumentationCommand()
|
runDocumentationCommand()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,11 +60,13 @@ func runServerCommand() {
|
|||||||
func runDocumentationCommand() {
|
func runDocumentationCommand() {
|
||||||
if appArgs.Docs.Generate != nil {
|
if appArgs.Docs.Generate != nil {
|
||||||
runDocsGenerationCommand()
|
runDocsGenerationCommand()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if appArgs.Docs.Serve != nil {
|
if appArgs.Docs.Serve != nil {
|
||||||
runDocsServingCommand()
|
runDocsServingCommand()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,6 +75,7 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,6 +89,7 @@ func runDocsGenerationCommand() {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
appLog.Global.Get(appLog.SYSTEM).Error(err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,14 +97,17 @@ func runDocsGenerationCommand() {
|
|||||||
_, 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,18 +116,21 @@ func runDocsServingCommand() {
|
|||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,6 +140,7 @@ func runDocsServingCommand() {
|
|||||||
_, 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
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -136,12 +150,14 @@ func runDocsServingCommand() {
|
|||||||
_, 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,32 +7,33 @@ type HTTPConfig struct {
|
|||||||
Headers map[string]string `json:"headers"`
|
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 {
|
type ServiceTypeCheckConfig struct {
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
HTTPConfig *HTTPConfig `json:"httpConfig"`
|
HTTPConfig *HTTPConfig `json:"httpConfig"`
|
||||||
// MaxFails - max "ping" fails, after with the service marked as unavailable
|
|
||||||
MaxFails uint8 `json:"maxFails"`
|
MaxFails uint8 `json:"maxFails"`
|
||||||
// Interval - interval between "ping" in seconds
|
|
||||||
Interval uint64 `json:"interval"`
|
Interval uint64 `json:"interval"`
|
||||||
// Timeout - interval after which the task will be canceled
|
|
||||||
Timeout uint64 `json:"timeout"`
|
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 {
|
type Service struct {
|
||||||
// Unique ID for entity
|
|
||||||
ID uint64 `gorm:"primary_key;auto_increment" json:"id"`
|
ID uint64 `gorm:"primary_key;auto_increment" json:"id"`
|
||||||
// Human-readable service name
|
|
||||||
Name string `gorm:"size:255;not null" json:"name"`
|
Name string `gorm:"size:255;not null" json:"name"`
|
||||||
// Human-readable service description
|
|
||||||
Description string `gorm:"size:255" json:"description"`
|
Description string `gorm:"size:255" json:"description"`
|
||||||
PublicDescription string `gorm:"size:255" json:"publicDescription"`
|
PublicDescription string `gorm:"size:255" json:"publicDescription"`
|
||||||
Public *bool `gorm:"default:false" json:"public"`
|
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"`
|
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"`
|
Type string `gorm:"size:255;not null" json:"type"`
|
||||||
Config *ServiceTypeCheckConfig `gorm:"serializer:json;column:type_config" json:"typeConfig"`
|
Config *ServiceTypeCheckConfig `gorm:"serializer:json;column:type_config" json:"typeConfig"`
|
||||||
|
|
||||||
Statuses []Status `gorm:"foreignkey:ServiceID" json:"statuses"`
|
Statuses []Status `gorm:"foreignkey:ServiceID" json:"statuses"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package args
|
|||||||
import "git.ostiwe.com/ostiwe-com/status/version"
|
import "git.ostiwe.com/ostiwe-com/status/version"
|
||||||
|
|
||||||
type ServerCmd struct {
|
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 {
|
type MigrationCreate struct {
|
||||||
@@ -15,12 +15,12 @@ type Migration struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ServerDocumentationCmd 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 {
|
type GenerateDocumentationCmd struct {
|
||||||
Format string `arg:"--format,-f" help:"Set output format (json, yaml)" default:"yaml"`
|
Format string `arg:"--format,-f" default:"yaml" help:"Set output format (json, yaml)"`
|
||||||
Out string `arg:"--out,-o" help:"Output file name (or stdout)" default:"stdout"`
|
Out string `arg:"--out,-o" default:"stdout" help:"Output file name (or stdout)"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DocsCmd struct {
|
type DocsCmd struct {
|
||||||
|
|||||||
@@ -58,11 +58,13 @@ func (r readyResponseErr) Send(response http.ResponseWriter) error {
|
|||||||
|
|
||||||
func (r responseErrBuilder) WithTrace(s string) ResponseErrBuilder {
|
func (r responseErrBuilder) WithTrace(s string) ResponseErrBuilder {
|
||||||
r.trace = s
|
r.trace = s
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r responseErrBuilder) WithStatusCode(i int) ResponseErrBuilder {
|
func (r responseErrBuilder) WithStatusCode(i int) ResponseErrBuilder {
|
||||||
r.status = i
|
r.status = i
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,11 +74,13 @@ func NewResponseErrBuilder() ResponseErrBuilder {
|
|||||||
|
|
||||||
func (r responseErrBuilder) WithDetails(m map[string]any) ResponseErrBuilder {
|
func (r responseErrBuilder) WithDetails(m map[string]any) ResponseErrBuilder {
|
||||||
r.details = m
|
r.details = m
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r responseErrBuilder) WithMessage(s string) ResponseErrBuilder {
|
func (r responseErrBuilder) WithMessage(s string) ResponseErrBuilder {
|
||||||
r.message = s
|
r.message = s
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ func (c *PlainAuthController) Handler() gin.HandlerFunc {
|
|||||||
Send(ginCtx)
|
Send(ginCtx)
|
||||||
if sendErr != nil {
|
if sendErr != nil {
|
||||||
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
|
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,6 +42,7 @@ func (c *PlainAuthController) Handler() gin.HandlerFunc {
|
|||||||
Send(ginCtx)
|
Send(ginCtx)
|
||||||
if sendErr != nil {
|
if sendErr != nil {
|
||||||
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
|
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ func (c *Controller) Handler() gin.HandlerFunc {
|
|||||||
_, err := ginCtx.Writer.Write([]byte("pong"))
|
_, err := ginCtx.Writer.Write([]byte("pong"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Global.Get(log.SERVER).Error(err)
|
log.Global.Get(log.SERVER).Error(err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ func SetUserFromJWT() gin.HandlerFunc {
|
|||||||
subject, err := token.Claims.GetSubject()
|
subject, err := token.Claims.GetSubject()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Writer.WriteHeader(http.StatusUnauthorized)
|
c.Writer.WriteHeader(http.StatusUnauthorized)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,6 @@ func Documentate() (*openapi3.Spec, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return reflector.Spec, nil
|
return reflector.Spec, nil
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ func Run(serverArgs *args.ServerCmd) {
|
|||||||
connect, err := db.Connect()
|
connect, err := db.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed connect to database: %v", err))
|
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed connect to database: %v", err))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,6 +27,7 @@ func Run(serverArgs *args.ServerCmd) {
|
|||||||
scheduler.GlobalAppScheduler, err = scheduler.NewAppScheduler()
|
scheduler.GlobalAppScheduler, err = scheduler.NewAppScheduler()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed create scheduler: %v", err))
|
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed create scheduler: %v", err))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,11 +36,13 @@ func Run(serverArgs *args.ServerCmd) {
|
|||||||
|
|
||||||
if err = service.GlobalCheckService.RegisterTasks(context.Background()); err != nil {
|
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))
|
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed create observe jobs: %v", err))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = scheduler.GlobalAppScheduler.StartJobs(); err != nil {
|
if err = scheduler.GlobalAppScheduler.StartJobs(); err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed start jobs: %v", err))
|
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed start jobs: %v", err))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ func PublicServices(items ...model.Service) []dto.PublicService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
@@ -57,6 +56,7 @@ func PublicService(item model.Service) dto.PublicService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
slices.Reverse(statuses)
|
slices.Reverse(statuses)
|
||||||
|
|
||||||
return dto.PublicService{
|
return dto.PublicService{
|
||||||
ID: int(item.ID),
|
ID: int(item.ID),
|
||||||
Name: item.Name,
|
Name: item.Name,
|
||||||
|
|||||||
Reference in New Issue
Block a user