moto/api.go

54 lines
1.5 KiB
Go
Raw Normal View History

2024-12-12 11:45:03 +08:00
package moto
2024-12-12 16:24:50 +08:00
import (
"git.nobla.cn/golang/kos"
"git.nobla.cn/golang/kos/entry/http"
"git.nobla.cn/golang/moto/internal/user"
"git.nobla.cn/golang/moto/internal/user/passport"
"git.nobla.cn/golang/moto/internal/user/types"
)
func (svr *Server) handleLogin(ctx *http.Context) (err error) {
var (
tk *types.Tokenize
req *passport.LoginRequest
)
req = &passport.LoginRequest{}
if err = ctx.Bind(req); err != nil {
return ctx.Error(http.ErrInvalidPayload, err.Error())
}
if tk, err = passport.Login(ctx.Context(), req); err != nil {
return ctx.Error(http.ErrPermissionDenied, err.Error())
}
return ctx.Success(tk)
}
func (svr *Server) handleLogout(ctx *http.Context) (err error) {
passport.Logout(ctx.Context(), ctx.User().Get("token"))
return ctx.Success("logout")
}
func (svr *Server) handleProfile(ctx *http.Context) (err error) {
var (
profile *types.Profile
)
if profile, err = user.Profile(ctx.Context(), ctx.User().ID); err != nil {
return ctx.Error(http.ErrTemporaryUnavailable, err.Error())
}
return ctx.Success(profile)
}
func (svr *Server) handleGetConfigure(ctx *http.Context) (err error) {
return ctx.Success(map[string]string{})
}
2024-12-12 11:45:03 +08:00
func (svr *Server) routes() {
2024-12-12 16:24:50 +08:00
kos.Http().Use(user.AuthMiddleware)
user.AllowUri("/passport/login")
2024-12-12 11:45:03 +08:00
2024-12-12 16:24:50 +08:00
kos.Http().Handle(http.MethodPost, "/passport/login", svr.handleLogin)
kos.Http().Handle(http.MethodDelete, "/passport/logout", svr.handleLogout)
kos.Http().Handle(http.MethodGet, "/user/profile", svr.handleProfile)
kos.Http().Handle(http.MethodGet, "/user/configures", svr.handleGetConfigure)
2024-12-12 11:45:03 +08:00
}