refactor(server): Move server to package
This commit is contained in:
76
main.go
76
main.go
@@ -3,78 +3,31 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.ostiwe.com/ostiwe-com/status/model"
|
|
||||||
"git.ostiwe.com/ostiwe-com/status/modules/db"
|
|
||||||
appLog "git.ostiwe.com/ostiwe-com/status/modules/log"
|
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/router"
|
||||||
"git.ostiwe.com/ostiwe-com/status/service"
|
"git.ostiwe.com/ostiwe-com/status/server"
|
||||||
"git.ostiwe.com/ostiwe-com/status/version"
|
_ "git.ostiwe.com/ostiwe-com/status/settings"
|
||||||
"github.com/alexflint/go-arg"
|
"github.com/alexflint/go-arg"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
_ "git.ostiwe.com/ostiwe-com/status/settings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerCmd struct {
|
var appArgs args.AppArgs
|
||||||
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() {
|
func main() {
|
||||||
arg.MustParse(&args)
|
arg.MustParse(&appArgs)
|
||||||
|
|
||||||
if args.Server != nil {
|
if appArgs.Server != nil {
|
||||||
connect, err := db.Connect()
|
server.Run(appArgs.Server)
|
||||||
if err != nil {
|
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error, failed connect to database: %v", err))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
db.SetGlobal(connect)
|
if appArgs.ServerDocumentation != nil {
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
appLog.Global.Get(appLog.SYSTEM).Info("Start service observer")
|
|
||||||
go service.NewCheck(20).Start(time.Second * 10)
|
|
||||||
|
|
||||||
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")
|
appLog.Global.Get(appLog.SYSTEM).Info("Collect documentation")
|
||||||
|
|
||||||
docs := router.Documentate()
|
docs := router.Documentate()
|
||||||
if !args.ServerDocumentation.Plain {
|
if !appArgs.ServerDocumentation.Plain {
|
||||||
chiRouter := chi.NewRouter()
|
chiRouter := chi.NewRouter()
|
||||||
|
|
||||||
err := docs.SetupRoutes(chiRouter, docs)
|
err := docs.SetupRoutes(chiRouter, docs)
|
||||||
@@ -83,8 +36,8 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
appLog.Global.Get(appLog.SYSTEM).Info(fmt.Sprintf("Start documentation server on port: %s", args.ServerDocumentation.Port))
|
appLog.Global.Get(appLog.SYSTEM).Info(fmt.Sprintf("Start documentation server on port: %s", appArgs.ServerDocumentation.Port))
|
||||||
err = http.ListenAndServe(fmt.Sprintf(":%s", args.ServerDocumentation.Port), chiRouter)
|
err = http.ListenAndServe(fmt.Sprintf(":%s", appArgs.ServerDocumentation.Port), chiRouter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error: %v", err))
|
appLog.Global.Get(appLog.SYSTEM).Error(fmt.Sprintf("Startup server error: %v", err))
|
||||||
}
|
}
|
||||||
@@ -96,10 +49,3 @@ func main() {
|
|||||||
|
|
||||||
appLog.Global.Get(appLog.SYSTEM).Info("Exit from application")
|
appLog.Global.Get(appLog.SYSTEM).Info("Exit from application")
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMigrate() error {
|
|
||||||
return db.Global.AutoMigrate(
|
|
||||||
model.Service{},
|
|
||||||
model.Status{},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|||||||
22
pkg/args/args.go
Normal file
22
pkg/args/args.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package args
|
||||||
|
|
||||||
|
import "git.ostiwe.com/ostiwe-com/status/version"
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (AppArgs) Version() string {
|
||||||
|
return version.AppVersion()
|
||||||
|
}
|
||||||
47
server/server.go
Normal file
47
server/server.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
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/modules/queue"
|
||||||
|
"git.ostiwe.com/ostiwe-com/status/pkg/args"
|
||||||
|
"git.ostiwe.com/ostiwe-com/status/router"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Run(serverArgs *args.ServerCmd) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
appLog.Global.Get(appLog.SYSTEM).Info("Start service observer")
|
||||||
|
go queue.InitQueues()
|
||||||
|
|
||||||
|
appLog.Global.Put(appLog.SERVER, logrus.New())
|
||||||
|
appLog.Global.Get(appLog.SERVER).Info("Startup server on port: ", serverArgs.Port)
|
||||||
|
|
||||||
|
err = http.ListenAndServe(fmt.Sprintf(":%s", serverArgs.Port), router.InitRoutes())
|
||||||
|
if err != nil {
|
||||||
|
appLog.Global.Get(appLog.SERVER).Error(fmt.Sprintf("Startup server error: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runMigrate() error {
|
||||||
|
return db.Global.AutoMigrate(
|
||||||
|
model.Service{},
|
||||||
|
model.Status{},
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user