Files
status/main.go
ostiwe 5a8b53b49d feat: Add documentation generation and serve functionality
- Implement documentation generation
- Replace chi router with gin for documentation server
- Fix issues with docs command execution
2025-11-04 19:20:10 +03:00

148 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 {
settings.Init()
if err := migration.CreateMigration(appArgs.Migration.Create.Name); err != nil {
panic(err)
}
return
}
if appArgs.Server != nil {
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
}
if appArgs.Docs.Generate != nil {
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
}
return
}
if appArgs.Docs.Serve != nil {
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
}
return
}
}