|
|
|
@ -2,17 +2,15 @@ package internal |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"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 { |
|
|
|
@ -61,31 +59,13 @@ func Start() error { |
|
|
|
|
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 run(e) |
|
|
|
|
} |
|
|
|
|
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")) |
|
|
|
|
|
|
|
|
|
func run(e *echo.Echo) error { |
|
|
|
|
port := env.String("SERVER_PORT", "1324") |
|
|
|
|
addr := fmt.Sprintf(":%s", port) |
|
|
|
|
// TODO(hupeh): 验证 addr 是否合法
|
|
|
|
|
e.Logger.Fatal(e.Start(addr)) |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|