|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"sorbet/internal/entities"
|
|
|
|
"sorbet/internal/middleware"
|
|
|
|
"sorbet/internal/util"
|
|
|
|
"sorbet/pkg/app"
|
|
|
|
"sorbet/pkg/db"
|
|
|
|
"sorbet/pkg/env"
|
|
|
|
"sorbet/pkg/log"
|
|
|
|
"sorbet/pkg/rsp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Init() error {
|
|
|
|
// 同步数据库结构
|
|
|
|
if err := syncEntities(); err != nil {
|
|
|
|
if !errors.Is(err, db.ErrNoCodeFirst) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !env.IsEnv("prod") {
|
|
|
|
log.Error("同步数据表结构需要开启 [DB_CODE_FIRST],在生产模式下请务必关闭。")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.OnStart(startServer)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func syncEntities() error {
|
|
|
|
return db.Sync(
|
|
|
|
&entities.Company{},
|
|
|
|
&entities.CompanyDepartment{},
|
|
|
|
&entities.CompanyStaff{},
|
|
|
|
&entities.Config{},
|
|
|
|
&entities.ConfigGroup{},
|
|
|
|
&entities.Feature{},
|
|
|
|
&entities.FeatureCategory{},
|
|
|
|
&entities.FeatureConfig{},
|
|
|
|
&entities.FeatureContent{},
|
|
|
|
&entities.FeatureContentChapter{},
|
|
|
|
&entities.FeatureContentDetail{},
|
|
|
|
&entities.Resource{},
|
|
|
|
&entities.ResourceCategory{},
|
|
|
|
&entities.SystemLog{},
|
|
|
|
&entities.SystemMenu{},
|
|
|
|
&entities.SystemPermission{},
|
|
|
|
&entities.SystemRole{},
|
|
|
|
&entities.SystemRolePower{},
|
|
|
|
&entities.SystemUser{},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func startServer() {
|
|
|
|
e := echo.New()
|
|
|
|
e.HideBanner = true
|
|
|
|
e.HidePort = true
|
|
|
|
e.HTTPErrorHandler = rsp.HTTPErrorHandler
|
|
|
|
e.Logger = util.NewLogger()
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
e.Use(middleware.CORS())
|
|
|
|
e.Use(middleware.Logger)
|
|
|
|
|
|
|
|
port := env.String("SERVER_PORT", "1324")
|
|
|
|
addr := fmt.Sprintf(":%s", port)
|
|
|
|
// TODO(hupeh): 验证 addr 是否合法
|
|
|
|
e.Logger.Error(e.Start(addr))
|
|
|
|
|
|
|
|
// 通知应用退出
|
|
|
|
app.Stop()
|
|
|
|
}
|