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.

160 lines
3.5 KiB

2 years ago
package user
import (
11 months ago
"fmt"
"git.diulo.com/mogfee/kit/middleware/jwt"
11 months ago
"git.diulo.com/mogfee/kit/rest"
"git.diulo.com/mogfee/kit/rest/httpx"
11 months ago
"net/http"
2 years ago
)
11 months ago
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),
},
})
2 years ago
}
11 months ago
// /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)
11 months ago
var req struct {
Name string `query:"name"`
UserId int `query:"user_id"`
}
fmt.Println("xxx")
11 months ago
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(ctx, w, err)
return
2 years ago
}
11 months ago
//resp, err := srv.List(ctx, &req)
//if err != nil {
// httpx.ErrorCtx(ctx, w, err)
//} else {
httpx.OkJsonCtx(ctx, w, req)
//}
2 years ago
}
}
11 months ago
// /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
2 years ago
}
11 months ago
resp, err := srv.All(ctx, &req)
1 year ago
if err != nil {
11 months ago
httpx.ErrorCtx(ctx, w, err)
} else {
httpx.OkJsonCtx(ctx, w, resp)
1 year ago
}
}
}
11 months ago
// /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
1 year ago
}
11 months ago
resp, err := srv.Auto(ctx, &req)
1 year ago
if err != nil {
11 months ago
httpx.ErrorCtx(ctx, w, err)
} else {
httpx.OkJsonCtx(ctx, w, resp)
1 year ago
}
}
}
11 months ago
// /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
1 year ago
}
11 months ago
resp, err := srv.LoginWithList(ctx, &req)
1 year ago
if err != nil {
11 months ago
httpx.ErrorCtx(ctx, w, err)
} else {
httpx.OkJsonCtx(ctx, w, resp)
1 year ago
}
}
}
11 months ago
// /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
1 year ago
}
11 months ago
resp, err := srv.Login(ctx, &req)
1 year ago
if err != nil {
11 months ago
httpx.ErrorCtx(ctx, w, err)
} else {
httpx.OkJsonCtx(ctx, w, resp)
1 year ago
}
}
}
11 months ago
// /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
1 year ago
}
11 months ago
resp, err := srv.Login1(ctx, &req)
1 year ago
if err != nil {
11 months ago
httpx.ErrorCtx(ctx, w, err)
} else {
httpx.OkJsonCtx(ctx, w, resp)
1 year ago
}
}
}