errcode
This commit is contained in:
parent
fd62c5cddb
commit
58139791f3
|
@ -1,7 +1,8 @@
|
||||||
package errcode
|
package errcode
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gofiber/fiber/v2"
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
// header error code ,start:100
|
// header error code ,start:100
|
||||||
|
@ -88,14 +89,14 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewError(code int, msg string) error {
|
func NewError(code int, msg string) error {
|
||||||
return fiber.NewError(code, msg)
|
return status.New(codes.Code(code), msg).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// custom error,status code:500
|
// custom error,status code:500
|
||||||
func ErrFatal(msg string) error {
|
func ErrFatal(code int, msg string) error {
|
||||||
return fiber.NewError(500, msg)
|
return status.New(codes.Code(code), msg).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func ErrNotFound(msg string) error {
|
func ErrNotFound(code int, msg string) error {
|
||||||
return fiber.NewError(404, msg+" Not Found")
|
return status.New(codes.Code(code), msg).Err()
|
||||||
}
|
}
|
||||||
|
|
3
go.sum
3
go.sum
|
@ -673,12 +673,15 @@ google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6D
|
||||||
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
|
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
|
||||||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY=
|
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY=
|
||||||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4=
|
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4=
|
||||||
|
google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a h1:fwgW9j3vHirt4ObdHoYNwuO24BEZjSzbh+zPaNWoiY8=
|
||||||
google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a/go.mod h1:EMfReVxb80Dq1hhioy0sOsY9jCE46YDgHlJ7fWVUWRE=
|
google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a/go.mod h1:EMfReVxb80Dq1hhioy0sOsY9jCE46YDgHlJ7fWVUWRE=
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q=
|
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q=
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
|
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
|
||||||
|
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 h1:W18sezcAYs+3tDZX4F80yctqa12jcP1PUS2gQu1zTPU=
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97/go.mod h1:iargEX0SFPm3xcfMI0d1domjg0ZF4Aa0p2awqyxhvF0=
|
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97/go.mod h1:iargEX0SFPm3xcfMI0d1domjg0ZF4Aa0p2awqyxhvF0=
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
|
||||||
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b h1:ZlWIi1wSK56/8hn4QcBp/j9M7Gt3U/3hZw3mC7vDICo=
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b/go.mod h1:swOH3j0KzcDDgGUWr+SNpyTen5YrXjS3eyPzFYKc6lc=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b/go.mod h1:swOH3j0KzcDDgGUWr+SNpyTen5YrXjS3eyPzFYKc6lc=
|
||||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||||
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
|
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package infra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.apinb.com/bsm-sdk/core/errcode"
|
||||||
|
)
|
||||||
|
|
||||||
|
var FTS fts
|
||||||
|
|
||||||
|
type fts struct {
|
||||||
|
Endpoint string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *fts) SaveTo(buf *bytes.Buffer, haeder map[string]string) (string, error) {
|
||||||
|
// 创建一个 POST 请求
|
||||||
|
req, err := http.NewRequest("POST", o.Endpoint, buf)
|
||||||
|
if err != nil {
|
||||||
|
return "", errcode.NewError(110, "Request to the URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置请求头为二进制流
|
||||||
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
|
for key, val := range haeder {
|
||||||
|
req.Header.Set(key, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送请求
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", errcode.NewError(111, "Client Do")
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// 读取响应体
|
||||||
|
respBody, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", errcode.NewError(112, "Read response body")
|
||||||
|
}
|
||||||
|
return string(respBody), nil
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package infra
|
||||||
|
|
||||||
|
import "github.com/gofiber/fiber/v2"
|
||||||
|
|
||||||
|
func Health(ctx *fiber.Ctx) error {
|
||||||
|
return ctx.SendStatus(200)
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package infra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DB *gorm.DB
|
||||||
|
RedisCache *redis.RedisClient
|
||||||
|
)
|
||||||
|
|
||||||
|
func New(db *gorm.DB, redisCache *redis.RedisClient) {
|
||||||
|
DB = db
|
||||||
|
RedisCache = redisCache
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package infra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Auth struct {
|
||||||
|
CustomerID int64 // 企业编号
|
||||||
|
CustomerCode string // 企业标识
|
||||||
|
UserID int64 // 用户编号
|
||||||
|
UserIdentity string // 用户标识
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParserCtx(ctx *fiber.Ctx) *Auth {
|
||||||
|
customerID := ctx.Locals("customerID").(int64)
|
||||||
|
customerCode := ctx.Locals("customerCode").(string)
|
||||||
|
userID := ctx.Locals("userID").(int64)
|
||||||
|
userIdentity := ctx.Locals("userIdentity").(string)
|
||||||
|
return &Auth{CustomerID: customerID, CustomerCode: customerCode, UserID: userID, UserIdentity: userIdentity}
|
||||||
|
}
|
|
@ -0,0 +1,69 @@
|
||||||
|
package infra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.apinb.com/bsm-sdk/core/vars"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Response Reply
|
||||||
|
|
||||||
|
type Reply struct {
|
||||||
|
Ret int `json:"ret"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Data interface{} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReplyIdent struct {
|
||||||
|
ID uint `json:"id"`
|
||||||
|
Identiy string `json:"identity"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reply) Error(ctx *fiber.Ctx, err error) error {
|
||||||
|
// Status code defaults to 500
|
||||||
|
reply := Reply{
|
||||||
|
Ret: http.StatusInternalServerError,
|
||||||
|
}
|
||||||
|
if e, ok := err.(*fiber.Error); ok {
|
||||||
|
reply.Ret = e.Code
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取执行信息
|
||||||
|
ExcuteTime(ctx)
|
||||||
|
|
||||||
|
reply.Msg = err.Error()
|
||||||
|
|
||||||
|
// Send error
|
||||||
|
return ctx.Status(fiber.StatusInternalServerError).JSON(reply)
|
||||||
|
}
|
||||||
|
func (r *Reply) Success(ctx *fiber.Ctx, data interface{}) error {
|
||||||
|
reply := Reply{
|
||||||
|
Ret: http.StatusOK,
|
||||||
|
}
|
||||||
|
|
||||||
|
if data == nil {
|
||||||
|
reply.Data = ""
|
||||||
|
} else {
|
||||||
|
reply.Data = data
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取执行信息
|
||||||
|
ExcuteTime(ctx)
|
||||||
|
|
||||||
|
return ctx.Status(fiber.StatusOK).JSON(reply)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExcuteTime(ctx *fiber.Ctx) error {
|
||||||
|
// 从context中获取开始时间
|
||||||
|
startTime, ok := ctx.Locals("startTime").(time.Time)
|
||||||
|
if !ok {
|
||||||
|
// 如果转换失败,则默认为当前时间
|
||||||
|
startTime = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
exectime := time.Since(startTime)
|
||||||
|
ctx.Response().Header.Add("Execute-Node", vars.HostName+","+exectime.String())
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package infra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.apinb.com/bsm-sdk/core/print"
|
||||||
|
"git.apinb.com/bsm-sdk/core/utils"
|
||||||
|
clientv3 "go.etcd.io/etcd/client/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Service service
|
||||||
|
RootPrefix string = "/services/"
|
||||||
|
)
|
||||||
|
|
||||||
|
type service struct{}
|
||||||
|
|
||||||
|
func (s *service) Register(cli *clientv3.Client, serviceName string, port int) error {
|
||||||
|
lease := clientv3.NewLease(cli)
|
||||||
|
grantResp, err := lease.Grant(context.TODO(), 5)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
serviceAddr := utils.GetLocationIP() + ":" + utils.Int2String(port)
|
||||||
|
|
||||||
|
key := RootPrefix + serviceName + "/" + utils.Int642String(time.Now().UnixNano())
|
||||||
|
_, err = cli.KV.Put(context.TODO(), key, serviceAddr, clientv3.WithLease(grantResp.ID))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
keepAliveChan, err := lease.KeepAlive(context.TODO(), grantResp.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
print.Info("[Traingo Register] Service Key: %s", key)
|
||||||
|
print.Info("[Traingo Register] Service Val: %s", serviceAddr)
|
||||||
|
|
||||||
|
print.Success("[Traingo Register] Service Register Complete.")
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for keepAliveResp := range keepAliveChan {
|
||||||
|
if keepAliveResp == nil {
|
||||||
|
log.Println("LeaseKeepAlive", "Failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue