feat(plugin): Extracting the main function to answercmd pkg

This commit is contained in:
LinkinStar 2023-01-18 15:37:22 +08:00
parent 406ee34fa8
commit 3ccdc8670e
5 changed files with 78 additions and 65 deletions

View File

@ -1,71 +1,12 @@
package main
import (
"os"
"time"
"github.com/answerdev/answer/internal/base/conf"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/cron"
"github.com/answerdev/answer/internal/cli"
"github.com/answerdev/answer/internal/schema"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman"
"github.com/segmentfault/pacman/contrib/log/zap"
"github.com/segmentfault/pacman/contrib/server/http"
"github.com/segmentfault/pacman/log"
)
// go build -ldflags "-X main.Version=x.y.z"
var (
// Name is the name of the project
Name = "answer"
// Version is the version of the project
Version = "0.0.0"
// Revision is the git short commit revision number
Revision = ""
// Time is the build time of the project
Time = ""
// log level
logLevel = os.Getenv("LOG_LEVEL")
// log path
logPath = os.Getenv("LOG_PATH")
answercmd "github.com/answerdev/answer/cmd"
)
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
func main() {
log.SetLogger(zap.NewLogger(
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
Execute()
}
func runApp() {
c, err := conf.ReadConfig(cli.GetConfigFilePath())
if err != nil {
panic(err)
}
conf.GetPathIgnoreList()
app, cleanup, err := initApplication(
c.Debug, c.Server, c.Data.Database, c.Data.Cache, c.I18n, c.Swaggerui, c.ServiceConfig, log.GetLogger())
if err != nil {
panic(err)
}
constant.Version = Version
schema.AppStartTime = time.Now()
defer cleanup()
if err := app.Run(); err != nil {
panic(err)
}
}
func newApplication(serverConf *conf.Server, server *gin.Engine, manager *cron.ScheduledTaskManager) *pacman.Application {
manager.Run()
return pacman.NewApp(
pacman.WithName(Name),
pacman.WithVersion(Version),
pacman.WithServer(http.NewServer(server, serverConf.HTTP.Addr)),
)
answercmd.Main()
}

View File

@ -1,4 +1,4 @@
package main
package answercmd
import (
"fmt"
@ -163,7 +163,7 @@ To run answer, use:
)
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
// This is called by main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {

72
cmd/main.go Normal file
View File

@ -0,0 +1,72 @@
package answercmd
import (
"os"
"time"
"github.com/answerdev/answer/internal/base/conf"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/cron"
"github.com/answerdev/answer/internal/cli"
"github.com/answerdev/answer/internal/schema"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman"
"github.com/segmentfault/pacman/contrib/log/zap"
"github.com/segmentfault/pacman/contrib/server/http"
"github.com/segmentfault/pacman/log"
)
// go build -ldflags "-X answercmd.Version=x.y.z"
var (
// Name is the name of the project
Name = "answer"
// Version is the version of the project
Version = "0.0.0"
// Revision is the git short commit revision number
Revision = ""
// Time is the build time of the project
Time = ""
// log level
logLevel = os.Getenv("LOG_LEVEL")
// log path
logPath = os.Getenv("LOG_PATH")
)
// Main
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
func Main() {
log.SetLogger(zap.NewLogger(
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
Execute()
}
func runApp() {
c, err := conf.ReadConfig(cli.GetConfigFilePath())
if err != nil {
panic(err)
}
conf.GetPathIgnoreList()
app, cleanup, err := initApplication(
c.Debug, c.Server, c.Data.Database, c.Data.Cache, c.I18n, c.Swaggerui, c.ServiceConfig, log.GetLogger())
if err != nil {
panic(err)
}
constant.Version = Version
schema.AppStartTime = time.Now()
defer cleanup()
if err := app.Run(); err != nil {
panic(err)
}
}
func newApplication(serverConf *conf.Server, server *gin.Engine, manager *cron.ScheduledTaskManager) *pacman.Application {
manager.Run()
return pacman.NewApp(
pacman.WithName(Name),
pacman.WithVersion(Version),
pacman.WithServer(http.NewServer(server, serverConf.HTTP.Addr)),
)
}

View File

@ -3,7 +3,7 @@
// The build tag makes sure the stub is not built in the final build.
package main
package answercmd
import (
"github.com/answerdev/answer/internal/base/conf"

View File

@ -4,7 +4,7 @@
//go:build !wireinject
// +build !wireinject
package main
package answercmd
import (
"github.com/answerdev/answer/internal/base/conf"