You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

159 lines
3.5 KiB

package user
import (
"fmt"
"git.diulo.com/mogfee/kit/middleware/jwt"
"git.diulo.com/mogfee/kit/rest"
"git.diulo.com/mogfee/kit/rest/httpx"
"net/http"
)
func RegisterUserHTTPServer(server *rest.Server, srv UserServer) {
server.AddRoutes([]rest.Route{
{
Method: "GET",
Path: "/api/v1/user/list",
Handler: UserListHandler(srv),
},
{
Method: "GET",
Path: "/api/v1/user/all",
Handler: UserAllHandler(srv),
},
{
Method: "GET",
Path: "/api/v1/user/auto",
Handler: UserAutoHandler(srv),
},
{
Method: "GET",
Path: "/api/v1/user/login_list",
Handler: UserLoginWithListHandler(srv),
},
{
Method: "GET",
Path: "/api/v1/user/login",
Handler: UserLoginHandler(srv),
},
{
Method: "POST",
Path: "/api/v1/user/login1",
Handler: UserLogin1Handler(srv),
},
})
}
// /api/v1/user/list
func UserListHandler(srv UserServer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var ctx = r.Context()
ctx = jwt.SetAuthKeyContext(ctx, "user:list")
ctx = jwt.SetNeedAuthContext(ctx, true)
var req struct {
Name string `query:"name"`
UserId int `query:"user_id"`
}
fmt.Println("xxx")
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(ctx, w, err)
return
}
//resp, err := srv.List(ctx, &req)
//if err != nil {
// httpx.ErrorCtx(ctx, w, err)
//} else {
httpx.OkJsonCtx(ctx, w, req)
//}
}
}
// /api/v1/user/all
func UserAllHandler(srv UserServer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var ctx = r.Context()
ctx = jwt.SetNeedAuthContext(ctx, true)
var req Request
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(ctx, w, err)
return
}
resp, err := srv.All(ctx, &req)
if err != nil {
httpx.ErrorCtx(ctx, w, err)
} else {
httpx.OkJsonCtx(ctx, w, resp)
}
}
}
// /api/v1/user/auto
func UserAutoHandler(srv UserServer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var ctx = r.Context()
var req Request
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(ctx, w, err)
return
}
resp, err := srv.Auto(ctx, &req)
if err != nil {
httpx.ErrorCtx(ctx, w, err)
} else {
httpx.OkJsonCtx(ctx, w, resp)
}
}
}
// /api/v1/user/login_list
func UserLoginWithListHandler(srv UserServer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var ctx = r.Context()
var req Request
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(ctx, w, err)
return
}
resp, err := srv.LoginWithList(ctx, &req)
if err != nil {
httpx.ErrorCtx(ctx, w, err)
} else {
httpx.OkJsonCtx(ctx, w, resp)
}
}
}
// /api/v1/user/login
func UserLoginHandler(srv UserServer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var ctx = r.Context()
var req Request
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(ctx, w, err)
return
}
resp, err := srv.Login(ctx, &req)
if err != nil {
httpx.ErrorCtx(ctx, w, err)
} else {
httpx.OkJsonCtx(ctx, w, resp)
}
}
}
// /api/v1/user/login1
func UserLogin1Handler(srv UserServer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var ctx = r.Context()
var req Request
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(ctx, w, err)
return
}
resp, err := srv.Login1(ctx, &req)
if err != nil {
httpx.ErrorCtx(ctx, w, err)
} else {
httpx.OkJsonCtx(ctx, w, resp)
}
}
}