wip: 登录和认证

This commit is contained in:
loveuer
2025-07-13 22:57:57 +08:00
parent 48af538f98
commit b48fa05d9f
33 changed files with 1961 additions and 33 deletions

View File

@ -2,20 +2,33 @@ package api
import (
"context"
"github.com/go-playground/validator/v10"
"loveuer/utodo/internal/handler"
"loveuer/utodo/internal/opt"
g_handler "loveuer/utodo/pkg/handler"
"loveuer/utodo/pkg/middleware/logger"
"loveuer/utodo/pkg/middleware/trace"
"github.com/gofiber/fiber/v3"
l3 "github.com/gofiber/fiber/v3/middleware/logger"
r3 "github.com/gofiber/fiber/v3/middleware/recover"
)
type structValidator struct {
validate *validator.Validate
}
func (v *structValidator) Validate(out any) error {
return v.validate.Struct(out)
}
func New(ctx context.Context) *fiber.App {
app := fiber.New()
app := fiber.New(fiber.Config{
BodyLimit: 5 * 1024 * 1024,
StructValidator: &structValidator{validate: validator.New()},
})
app.Use(trace.New())
app.Use(l3.New())
// app.Use(l3.New())
app.Use(logger.New())
app.Use(r3.New())
app.Get("/healthz", g_handler.Healthz(opt.Cfg.Name, opt.Cfg.Version))

View File

@ -6,9 +6,10 @@ import (
"loveuer/utodo/internal/model"
"loveuer/utodo/internal/opt"
g_api "loveuer/utodo/pkg/api"
"loveuer/utodo/pkg/database/cache"
"loveuer/utodo/pkg/tool"
"gitea.loveuer.com/yizhisec/packages/database/db"
"gitea.loveuer.com/yizhisec/packages/tool"
"github.com/spf13/cobra"
)
@ -21,6 +22,7 @@ func svcCmd() *cobra.Command {
cmd.Flags().StringVar(&opt.Cfg.Svc.Address, "address", ":9119", "address to listen on")
cmd.Flags().StringVar(&opt.Cfg.Svc.DBFile, "db", "/data/data.db", "database file")
cmd.Flags().StringVar(&opt.Cfg.Svc.Cache, "cache", "", "cache uri: empty for memory cache; for redis(rdp://user:password@host:port)")
return cmd
}
@ -31,7 +33,14 @@ func svcRun(cmd *cobra.Command, args []string) error {
stopApi func(context.Context) error
)
if err = db.Init(db.WithSqlite(opt.Cfg.Svc.DBFile)); err != nil {
if err = db.Init(db.WithCtx(cmd.Context()), db.WithSqlite(opt.Cfg.Svc.DBFile)); err != nil {
return err
}
if err = cache.Init(
cache.WithCtx(cmd.Context()),
tool.If(opt.Cfg.Svc.Cache == "", cache.WithMemory(), cache.WithRedisURI(opt.Cfg.Svc.Cache)),
); err != nil {
return err
}

View File

@ -1,14 +1,30 @@
package handler
import (
"loveuer/utodo/pkg/logger"
"loveuer/utodo/pkg/resp"
"github.com/gofiber/fiber/v3"
)
func Login() fiber.Handler {
return func(c fiber.Ctx) error {
logger.InfoCtx(c.Context(), "login")
return c.SendString("Hello, World!")
type Req struct {
Username string `json:"username"`
Phone string `json:"phone"`
Email string `json:"email"`
Password string `json:"password" validate:"required,min=8"`
}
var (
err error
req Req
)
if err = c.Bind().JSON(&req); err != nil {
return resp.R400(c, "", nil, err.Error())
}
// TODO: 完成登录
return nil
}
}

View File

@ -7,6 +7,7 @@ type config struct {
Svc struct {
Address string
DBFile string
Cache string
}
}