gorm wire
This commit is contained in:
29
cmd/cmd.go
29
cmd/cmd.go
@@ -1,29 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type server interface {
|
||||
ListenAndServe() error
|
||||
}
|
||||
|
||||
func mustInit(fn func() error) {
|
||||
err := fn()
|
||||
if err != nil {
|
||||
ptr := reflect.ValueOf(fn).Pointer()
|
||||
fi := runtime.FuncForPC(ptr)
|
||||
log.Fatalf("%s failed: %v", fi.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustInitAny[T any](s T, fn func(s T) error) {
|
||||
err := fn(s)
|
||||
if err != nil {
|
||||
ptr := reflect.ValueOf(fn).Pointer()
|
||||
fi := runtime.FuncForPC(ptr)
|
||||
log.Fatalf("%s(T) failed: %v", fi.Name(), err)
|
||||
}
|
||||
}
|
||||
196
cmd/erp.go
196
cmd/erp.go
@@ -1,156 +1,94 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"management/internal/erpserver/handler"
|
||||
"management/internal/erpserver/repository"
|
||||
systemrepo "management/internal/erpserver/repository/system"
|
||||
commonservice "management/internal/erpserver/service/v1/common"
|
||||
systemservice "management/internal/erpserver/service/v1/system"
|
||||
"management/internal/pkg/binding"
|
||||
"management/internal/erpserver"
|
||||
"management/internal/pkg/config"
|
||||
"management/internal/pkg/database"
|
||||
"management/internal/pkg/middleware"
|
||||
"management/internal/pkg/redis"
|
||||
"management/internal/pkg/session"
|
||||
"management/internal/pkg/tpl"
|
||||
|
||||
"github.com/drhin/logger"
|
||||
"github.com/fvbock/endless"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var configPath string
|
||||
|
||||
// erpCmd 代表erp命令
|
||||
var erpCmd = &cobra.Command{
|
||||
Use: "erp",
|
||||
Short: "Start erp management server",
|
||||
Long: `A Service to erp management`,
|
||||
Short: "启动 ERP 管理服务器",
|
||||
Long: `启动 ERP 管理服务器,可根据指定配置文件加载服务配置。
|
||||
|
||||
示例:
|
||||
# 使用默认配置文件启动服务
|
||||
./management erp
|
||||
|
||||
# 使用自定义配置文件启动服务
|
||||
./management erp -c config.prod.yaml
|
||||
|
||||
该命令会初始化配置、日志系统,然后启动 HTTP 服务器监听指定端口。在 Windows 系统上使用标准的 http.Server,其他系统使用 endless 实现热重启。
|
||||
`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := runErp(cmd.Context())
|
||||
if err != nil {
|
||||
if err := runErp(); err != nil {
|
||||
log.Fatalf("run erp failed: %v", err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
erpCmd.Flags().StringVarP(&configPath, "config", "c", "", "Custom config file path")
|
||||
rootCmd.AddCommand(erpCmd)
|
||||
}
|
||||
|
||||
func runErp(ctx context.Context) error {
|
||||
conf, err := config.New(configPath)
|
||||
checkError(err)
|
||||
|
||||
// 初始化数据
|
||||
// dbinit.InitSeed()
|
||||
|
||||
// mustInit(redis.Init)
|
||||
|
||||
err = binding.SetValidatorTrans("zh")
|
||||
checkError(err)
|
||||
|
||||
contextx, err := newAppContext(conf)
|
||||
checkError(err)
|
||||
|
||||
address := fmt.Sprintf("%s:%d", conf.App.Host, conf.App.Port)
|
||||
log.Printf("Starting erp manage server on %s", address)
|
||||
server := InitServer(address, contextx.Router())
|
||||
return server.ListenAndServe()
|
||||
}
|
||||
|
||||
func newAppContext(conf *config.Config) (*handler.AppContext, error) {
|
||||
// initialize DB
|
||||
dbOptions := &database.PostgreSQLOptions{
|
||||
Addr: conf.DB.Host,
|
||||
Username: conf.DB.Username,
|
||||
Password: conf.DB.Password,
|
||||
Database: conf.DB.DBName,
|
||||
}
|
||||
gdb, err := database.NewPostgreSQL(dbOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
repo := repository.NewStore(gdb)
|
||||
|
||||
// initialize Redis
|
||||
redis, err := redis.New(conf.Redis)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// initialize Session
|
||||
db, err := gdb.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
session := session.New(db, conf.App.Prod)
|
||||
|
||||
// initialize Logger
|
||||
log, err := logger.NewProduction()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx := &handler.AppContext{
|
||||
DB: gdb,
|
||||
Store: repo,
|
||||
Redis: redis,
|
||||
Session: session,
|
||||
Config: conf,
|
||||
Log: log,
|
||||
}
|
||||
|
||||
setRepository(ctx)
|
||||
setService(ctx)
|
||||
setMiddleware(ctx)
|
||||
if err := setRender(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func setRepository(ctx *handler.AppContext) {
|
||||
ctx.UserRepo = systemrepo.NewUserRepository(ctx.Store)
|
||||
ctx.LoginLogRepo = systemrepo.NewLoginLogRepository(ctx.Store)
|
||||
ctx.AuditLogRepo = systemrepo.NewAuditLogRepository(ctx.Store)
|
||||
ctx.RoleRepo = systemrepo.NewRoleRepository(ctx.Store)
|
||||
ctx.DepartmentRepo = systemrepo.NewDepartmentRepository(ctx.Store)
|
||||
ctx.ConfigRepo = systemrepo.NewConfigRepository(ctx.Store)
|
||||
ctx.RoleMenuRepo = systemrepo.NewRoleMenuRepository(ctx.Store)
|
||||
ctx.MenuRepo = systemrepo.NewMenuRepository(ctx.Store)
|
||||
}
|
||||
|
||||
func setService(ctx *handler.AppContext) {
|
||||
ctx.CaptchaService = commonservice.NewCaptchaService()
|
||||
|
||||
ctx.ConfigService = systemservice.NewConfigService(ctx.ConfigRepo, ctx.Redis)
|
||||
ctx.LoginLogService = systemservice.NewLoginLogService(ctx.LoginLogRepo)
|
||||
ctx.AuditLogService = systemservice.NewAuditLogService(ctx.AuditLogRepo)
|
||||
ctx.RoleService = systemservice.NewRoleService(ctx.RoleRepo)
|
||||
ctx.UserService = systemservice.NewUserService(ctx.Session, ctx.Log, ctx.UserRepo, ctx.RoleService, ctx.LoginLogService)
|
||||
ctx.DepartmentService = systemservice.NewDepartmentService(ctx.DepartmentRepo)
|
||||
ctx.RoleMenuService = systemservice.NewRoleMenuService(ctx.RoleMenuRepo)
|
||||
ctx.MenuService = systemservice.NewMenuService(ctx.Redis, ctx.Store, ctx.MenuRepo, ctx.RoleService, ctx.RoleMenuService)
|
||||
}
|
||||
|
||||
func setMiddleware(ctx *handler.AppContext) {
|
||||
ctx.Middleware = middleware.New(ctx.Session, ctx.MenuService, ctx.AuditLogService)
|
||||
}
|
||||
|
||||
func setRender(ctx *handler.AppContext) error {
|
||||
var err error
|
||||
ctx.Render, err = tpl.New(ctx.Session, ctx.MenuService)
|
||||
func runErp() error {
|
||||
config, err := config.New(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkError(err error) {
|
||||
l, err := logger.NewDevelopment()
|
||||
if err != nil {
|
||||
log.Fatalf("init failed: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
mux, fn, err := erpserver.NewWire(config, l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer fn()
|
||||
|
||||
address := fmt.Sprintf("%s:%d", config.App.Host, config.App.Port)
|
||||
log.Printf("Starting manage server on %s", address)
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
s := &http.Server{
|
||||
Addr: address,
|
||||
Handler: mux,
|
||||
ReadTimeout: 20 * time.Second,
|
||||
WriteTimeout: 20 * time.Second,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
return s.ListenAndServe()
|
||||
} else {
|
||||
s := endless.NewServer(address, mux)
|
||||
s.ReadHeaderTimeout = 20 * time.Second
|
||||
s.WriteTimeout = 20 * time.Second
|
||||
s.MaxHeaderBytes = 1 << 20
|
||||
return s.ListenAndServe()
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(erpCmd)
|
||||
|
||||
// 在这里,您将定义您的标志和配置设置。
|
||||
|
||||
// Cobra支持适用于此命令的持久标志
|
||||
// 以及所有子命令,例如:
|
||||
// erpCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra支持仅在执行以下命令时运行的本地标志
|
||||
// 直接调用,例如:
|
||||
// erpCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
erpCmd.Flags().StringVarP(&configPath, "config", "c", "configs/config.dev.yaml", "custom config file")
|
||||
}
|
||||
|
||||
43
cmd/root.go
43
cmd/root.go
@@ -1,25 +1,48 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var configPath string
|
||||
|
||||
// rootCmd 表示在没有任何子命令的情况下调用的基本命令
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "testpaper",
|
||||
Short: "Start testpaper server",
|
||||
Long: `testpaper service`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
},
|
||||
Use: "management",
|
||||
Short: "ERP 管理系统命令行工具",
|
||||
Long: `ERP 管理系统命令行工具,用于管理和控制 ERP 管理服务器。支持多种操作,如启动服务器、根据不同配置文件加载服务等。
|
||||
|
||||
示例:
|
||||
# 使用默认配置文件启动 ERP 管理服务器
|
||||
./management erp
|
||||
|
||||
# 使用自定义配置文件启动 ERP 管理服务器
|
||||
./management erp -c config.prod.yaml
|
||||
|
||||
该工具依赖 Cobra 库构建,在 Windows 系统上使用标准的 http.Server 启动服务,其他系统使用 endless 库实现热重启。
|
||||
`,
|
||||
// 如果您的裸应用程序没有注释以下行
|
||||
// 具有与之关联的操作:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
// Execute 将所有子命令添加到根命令中,并相应地设置标志。
|
||||
// 这是由main.main()调用的。rootCmd只需要发生一次。
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
// 在这里,您将定义您的标志和配置设置。
|
||||
// Cobra支持持久标志,如果在这里定义,
|
||||
// 将为您的应用程序提供全球支持。
|
||||
|
||||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.management.yaml)")
|
||||
|
||||
// Cobra还支持仅运行本地标志
|
||||
// 当直接调用此操作时。
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/fvbock/endless"
|
||||
)
|
||||
|
||||
func InitServer(address string, router http.Handler) server {
|
||||
s := endless.NewServer(address, router)
|
||||
s.ReadHeaderTimeout = 20 * time.Second
|
||||
s.WriteTimeout = 20 * time.Second
|
||||
s.MaxHeaderBytes = 1 << 20
|
||||
return s
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InitServer(address string, router http.Handler) server {
|
||||
return &http.Server{
|
||||
Addr: address,
|
||||
Handler: router,
|
||||
ReadTimeout: 20 * time.Second,
|
||||
WriteTimeout: 20 * time.Second,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user