refactor: Fix linter errors
This commit is contained in:
202
main.go
202
main.go
@@ -23,125 +23,125 @@ 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)
|
||||
}
|
||||
|
||||
runMigrationCreateCommand()
|
||||
return
|
||||
}
|
||||
|
||||
if appArgs.Server != nil {
|
||||
settings.Init()
|
||||
|
||||
migration.RunMigration()
|
||||
server.Run(appArgs.Server)
|
||||
|
||||
runServerCommand()
|
||||
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 {
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
runDocsGenerationCommand()
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user