|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"sorbet/internal/entities"
|
|
|
|
"sorbet/internal/middleware"
|
|
|
|
"sorbet/internal/repositories"
|
|
|
|
"sorbet/internal/util"
|
|
|
|
"sorbet/pkg/db"
|
|
|
|
"sorbet/pkg/env"
|
|
|
|
"sorbet/pkg/log"
|
|
|
|
"sorbet/pkg/rsp"
|
|
|
|
"sorbet/pkg/ticket"
|
|
|
|
)
|
|
|
|
|
|
|
|
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],在生产模式下请务必关闭。")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 Start() error {
|
|
|
|
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)
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
|
|
repo := repositories.NewCompanyRepository(c.Get("db").(*gorm.DB))
|
|
|
|
company, err := repo.GetByID(c.Request().Context(), 1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
token, err := ticket.Create(&ticket.Claims{
|
|
|
|
UID: company.ID,
|
|
|
|
Role: "system",
|
|
|
|
Issuer: "chshs",
|
|
|
|
Subject: "subject",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return rsp.Ok(c, echo.Map{
|
|
|
|
"token": token,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
e.Group("", ticket.Middleware(false, "system")).GET("/u", func(c echo.Context) error {
|
|
|
|
return rsp.Ok(c, echo.Map{
|
|
|
|
"ticket": c.Get("ticket"),
|
|
|
|
"claims": c.Get("ticket_claims").(*ticket.Claims),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
e.Logger.Fatal(e.Start(":1323"))
|
|
|
|
return nil
|
|
|
|
}
|