|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
echoSwagger "github.com/swaggo/echo-swagger"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"net/http"
|
|
|
|
"sorbet/internal/entities"
|
|
|
|
"sorbet/internal/middleware"
|
|
|
|
"sorbet/internal/repositories"
|
|
|
|
"sorbet/internal/util"
|
|
|
|
"sorbet/pkg/db"
|
|
|
|
"sorbet/pkg/env"
|
|
|
|
"sorbet/pkg/ioc"
|
|
|
|
"sorbet/pkg/log"
|
|
|
|
"sorbet/pkg/rsp"
|
|
|
|
"sorbet/pkg/ticket"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Init() error {
|
|
|
|
ioc.Bind(db.DB()) // 注入数据库操作
|
|
|
|
ioc.Bind(log.Default()) // 注入日志操作
|
|
|
|
repositories.Init() // 注入数据仓库操作
|
|
|
|
|
|
|
|
// 同步数据库结构
|
|
|
|
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 = func(err error, c echo.Context) {
|
|
|
|
if !c.Response().Committed {
|
|
|
|
http.Error(c.Response(), err.Error(), 500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.Logger = util.NewLogger()
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
e.Use(middleware.CORS())
|
|
|
|
e.Use(middleware.Logger)
|
|
|
|
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
db := ioc.MustGet[gorm.DB]().WithContext(c.Request().Context())
|
|
|
|
ci := ioc.Fork()
|
|
|
|
ci.Bind(db)
|
|
|
|
c.Set("db", db)
|
|
|
|
c.Set("ioc", ci)
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
e.GET("/swagger/*", echoSwagger.WrapHandler)
|
|
|
|
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 := util.CreateTicket(&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("", middleware.Ticket(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
|
|
|
|
}
|