init: first steps
This commit is contained in:
100
main.go
Normal file
100
main.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/model"
|
||||
"git.ostiwe.com/ostiwe-com/status/modules/db"
|
||||
appLog "git.ostiwe.com/ostiwe-com/status/modules/log"
|
||||
"git.ostiwe.com/ostiwe-com/status/router"
|
||||
"git.ostiwe.com/ostiwe-com/status/version"
|
||||
"github.com/alexflint/go-arg"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
_ "git.ostiwe.com/ostiwe-com/status/settings"
|
||||
)
|
||||
|
||||
type ServerCmd struct {
|
||||
Port string `arg:"-p,--port" help:"Port to listen on" default:"8080"`
|
||||
}
|
||||
|
||||
type ServerDocumentationCmd struct {
|
||||
Port string `arg:"-p,--port" help:"Port to listen on" default:"8081"`
|
||||
Plain bool `arg:"--plain" help:"Enable plain text output" default:"true"`
|
||||
PlainFormat string `arg:"--plain-format" help:"Set format for output (json, yaml)" default:"yaml"`
|
||||
}
|
||||
|
||||
type appArgs struct {
|
||||
Server *ServerCmd `arg:"subcommand:server" help:"Start the api server"`
|
||||
ServerDocumentation *ServerDocumentationCmd `arg:"subcommand:server-docs" help:"Generate documentation for api server"`
|
||||
}
|
||||
|
||||
var args appArgs
|
||||
|
||||
func (appArgs) Version() string {
|
||||
return version.AppVersion()
|
||||
}
|
||||
|
||||
func main() {
|
||||
arg.MustParse(&args)
|
||||
|
||||
connect, err := db.Connect()
|
||||
if err != nil {
|
||||
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed connect to database: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
db.SetGlobal(connect)
|
||||
appLog.Global.Get(appLog.SYSTEM).Info("Run db migration")
|
||||
if err = runMigrate(); err != nil {
|
||||
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Migration failed, error: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
if args.Server != nil {
|
||||
appLog.Global.Put(appLog.SERVER, logrus.New())
|
||||
appLog.Global.Get(appLog.SERVER).Info("Startup server on port: ", args.Server.Port)
|
||||
|
||||
err := http.ListenAndServe(fmt.Sprintf(":%s", args.Server.Port), router.InitRoutes())
|
||||
if err != nil {
|
||||
appLog.Global.Get(appLog.SERVER).Error(fmt.Sprintf("Startup server error: %v", err))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if args.ServerDocumentation != nil {
|
||||
appLog.Global.Get(appLog.SYSTEM).Info("Collect documentation")
|
||||
|
||||
docs := router.Documentate()
|
||||
if !args.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", args.ServerDocumentation.Port))
|
||||
err = http.ListenAndServe(fmt.Sprintf(":%s", args.ServerDocumentation.Port), chiRouter)
|
||||
if err != nil {
|
||||
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error: %v", err))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
appLog.Global.Get(appLog.SYSTEM).Info("Exit from application")
|
||||
}
|
||||
|
||||
func runMigrate() error {
|
||||
return db.Global.AutoMigrate(
|
||||
model.Service{},
|
||||
model.Status{},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user