This commit is contained in:
zhaoxiaorong
2025-02-07 17:08:55 +08:00
parent fd62c5cddb
commit 58139791f3
8 changed files with 220 additions and 6 deletions

44
infra/fts.go Normal file
View File

@@ -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
}

7
infra/handler.go Normal file
View File

@@ -0,0 +1,7 @@
package infra
import "github.com/gofiber/fiber/v2"
func Health(ctx *fiber.Ctx) error {
return ctx.SendStatus(200)
}

16
infra/new.go Normal file
View File

@@ -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
}

20
infra/parser.go Normal file
View File

@@ -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}
}

69
infra/response.go Normal file
View File

@@ -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
}

54
infra/service.go Normal file
View File

@@ -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
}