Files
status/main.go
ostiwe fc8b723114 refactor: Fix linter errors by auto-apply
- Apply automated linter fixes
- Manually reformat fields in model/service.go
- Resolve code complexity issues
2025-11-04 19:59:17 +03:00

164 lines
2.9 KiB
Go

package main
import (
"embed"
"fmt"
"net/http"
"os"
"git.ostiwe.com/ostiwe-com/status/migration"
appLog "git.ostiwe.com/ostiwe-com/status/modules/log"
"git.ostiwe.com/ostiwe-com/status/pkg/args"
"git.ostiwe.com/ostiwe-com/status/router"
"git.ostiwe.com/ostiwe-com/status/server"
"git.ostiwe.com/ostiwe-com/status/settings"
"github.com/alexflint/go-arg"
"github.com/gin-gonic/gin"
)
//go:embed html
var htmlFolder embed.FS
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 {
runMigrationCreateCommand()
return
}
if appArgs.Server != nil {
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)
}
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()
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
}
}