feat: Add documentation generation and serve functionality
- Implement documentation generation - Replace chi router with gin for documentation server - Fix issues with docs command execution
This commit is contained in:
142
main.go
142
main.go
@@ -1,20 +1,34 @@
|
||||
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"
|
||||
"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)
|
||||
}
|
||||
@@ -23,35 +37,111 @@ func main() {
|
||||
}
|
||||
|
||||
if appArgs.Server != nil {
|
||||
migration.RunMigration()
|
||||
settings.Init()
|
||||
|
||||
migration.RunMigration()
|
||||
server.Run(appArgs.Server)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Rewrite to use gin router, instead of chi router
|
||||
// if appArgs.ServerDocumentation != nil {
|
||||
// appLog.Global.Get(appLog.SYSTEM).Info("Collect documentation")
|
||||
//
|
||||
// docs := router.Documentate()
|
||||
// if !appArgs.ServerDocumentation.Plain {
|
||||
// chiRouter := chi.NewRouter()
|
||||
//
|
||||
// err := docs.SetupRoutes(chiRouter, docs)
|
||||
// if err != nil {
|
||||
// appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Setup docs routes error: %v", err))
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// appLog.Global.Get(appLog.SYSTEM).Info(fmt.Sprintf("Start documentation server on port: %s", appArgs.ServerDocumentation.Port))
|
||||
// err = http.ListenAndServe(fmt.Sprintf(":%s", appArgs.ServerDocumentation.Port), chiRouter)
|
||||
// if err != nil {
|
||||
// appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error: %v", err))
|
||||
// }
|
||||
//
|
||||
// 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
|
||||
}
|
||||
|
||||
appLog.Global.Get(appLog.SYSTEM).Info("Exit from application")
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user