moto/api.go

54 lines
1.5 KiB
Go

package moto
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{})
}
func (svr *Server) routes() {
kos.Http().Use(user.AuthMiddleware)
user.AllowUri("/passport/login")
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)
}