generated from pkg/go-template
feat :
This commit is contained in:
@@ -4,11 +4,24 @@ import (
|
||||
"fmt"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/model"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/pkg"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/service"
|
||||
"git.icechen.cn/monorepo/backend/pkg/api"
|
||||
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
ctxLogger "github.com/luizsuper/ctxLoggers"
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/grpc"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
pageIsNotInt = api.WarpFError(errors.New("page 参数错误"))
|
||||
limitIsNotInt = api.WarpFError(errors.New("limit 参数错误"))
|
||||
)
|
||||
|
||||
func GetScriptsH(ctx *fiber.Ctx) error {
|
||||
@@ -76,23 +89,56 @@ func DeleteScriptsH(ctx *fiber.Ctx) error {
|
||||
return ctx.JSON(success(nil))
|
||||
}
|
||||
|
||||
//func HelloWorld() error {
|
||||
// // Set up a connection to the server.
|
||||
// conn, err := grpc.Dial("localhost:3000", grpc.WithInsecure(), grpc.WithBlock())
|
||||
// if err != nil {
|
||||
// log.Fatalf("did not connect: %v", err)
|
||||
// }
|
||||
// defer conn.Close()
|
||||
// c := murder.NewHelloWorldClient(conn)
|
||||
//
|
||||
// // Contact the server and print out its response.
|
||||
// ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
// defer cancel()
|
||||
// r, err := c.Login(ctx, &murder.HelloRequest{Code: "lht"})
|
||||
//
|
||||
// if err != nil {
|
||||
// log.Fatalf("could not greet: %v", err)
|
||||
// }
|
||||
// log.Printf("Greeting: %s", r.Token)
|
||||
// return nil
|
||||
//}
|
||||
func GetScriptApi(ctx *fiber.Ctx) error {
|
||||
//todo:调通中间层
|
||||
//todo:换成服务
|
||||
conn, err := grpc.Dial("localhost:3000", grpc.WithInsecure(), grpc.WithBlock())
|
||||
if err != nil {
|
||||
log.Fatalf("did not connect: %v", err)
|
||||
}
|
||||
|
||||
outgoingContext, cancelFunc := pkg.TransFiberCtx2NewOutgoingContext(ctx, 3*time.Second)
|
||||
defer conn.Close()
|
||||
defer cancelFunc()
|
||||
c := murders.NewMurdersClient(conn)
|
||||
|
||||
page := ctx.Query("page", "1")
|
||||
limit := ctx.Query("size", "10")
|
||||
p, err := strconv.Atoi(page)
|
||||
if err != nil {
|
||||
ctxLogger.Error(ctx.UserContext(), pageIsNotInt.Error(), zap.String("page", page))
|
||||
return pageIsNotInt
|
||||
}
|
||||
l, err := strconv.Atoi(limit)
|
||||
if err != nil {
|
||||
ctxLogger.Error(ctx.UserContext(), limitIsNotInt.Error(), zap.String("limit", limit))
|
||||
return pageIsNotInt
|
||||
}
|
||||
|
||||
validate := validator.New()
|
||||
err = validate.Var(p, "gte=0")
|
||||
if err != nil {
|
||||
ctxLogger.Error(ctx.UserContext(), limitIsNotInt.Error(), zap.String("limit", limit))
|
||||
return api.WarpFError(err)
|
||||
}
|
||||
err = validate.Var(l, "gte=-1")
|
||||
if err != nil {
|
||||
ctxLogger.Error(ctx.UserContext(), limitIsNotInt.Error(), zap.String("limit", limit))
|
||||
return api.WarpFError(err)
|
||||
}
|
||||
|
||||
p64 := int64(p)
|
||||
l64 := int64(l)
|
||||
condition := murders.QueryCondition{
|
||||
Page: &p64,
|
||||
Size: &l64,
|
||||
QueryMap: nil,
|
||||
}
|
||||
|
||||
scripts, err := c.GetScripts(outgoingContext, &condition)
|
||||
if err != nil {
|
||||
return api.WarpFError(err)
|
||||
}
|
||||
|
||||
return ctx.JSON(scripts)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TransFiberCtx2NewOutgoingContext(fCtx *fiber.Ctx, time time.Duration) (context.Context, context.CancelFunc) {
|
||||
ctx := fCtx.UserContext()
|
||||
md := metadata.MD{}
|
||||
if value, ok := ctx.Value(fiber.HeaderXRequestID).(string); ok {
|
||||
md = metadata.Pairs(fiber.HeaderXRequestID, value)
|
||||
} else {
|
||||
md = metadata.Pairs(fiber.HeaderXRequestID, utils.UUIDv4())
|
||||
}
|
||||
//3s 过期超时
|
||||
return context.WithTimeout(metadata.NewOutgoingContext(ctx, md), time)
|
||||
}
|
||||
@@ -19,7 +19,7 @@ const (
|
||||
tag = "/tag"
|
||||
category = "/category"
|
||||
scripts = "/scripts"
|
||||
hello = "/hello"
|
||||
script = "/script"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -110,6 +110,10 @@ func routerInit() {
|
||||
scriptsGroup.Delete("", func(ctx *fiber.Ctx) error {
|
||||
return handler.DeleteScriptsH(ctx)
|
||||
})
|
||||
scriptGroup := app.Group(script)
|
||||
scriptGroup.Get("", func(ctx *fiber.Ctx) error {
|
||||
return handler.GetScriptApi(ctx)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user