更新网关

This commit is contained in:
eson 2023-11-15 14:32:14 +08:00
parent b20cb4b7c3
commit 309b18a2b7
4 changed files with 42 additions and 19 deletions

2
go.mod
View File

@ -47,4 +47,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)
replace fusen-basic v0.0.0 => gitee.com/fusenpack/fusen-basic v0.0.5-0.20231114092248-4bce975c1242
replace fusen-basic v0.0.0 => gitee.com/fusenpack/fusen-basic v0.0.5-0.20231115062855-6f9b810c456b

4
go.sum
View File

@ -1,6 +1,6 @@
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gitee.com/fusenpack/fusen-basic v0.0.5-0.20231114092248-4bce975c1242 h1:/Mnolce7yH1XHhm+KtNGXZksn3I4O5HeUrXzdP/5mKA=
gitee.com/fusenpack/fusen-basic v0.0.5-0.20231114092248-4bce975c1242/go.mod h1:Zw6gt/0VwNmtG2Boqmo8+5H5AVuWsIVgCgp1ilkRyao=
gitee.com/fusenpack/fusen-basic v0.0.5-0.20231115062855-6f9b810c456b h1:CXhj7d67CykbMvTQPGi+iOoDnbhqchEc3i4AYBSovlo=
gitee.com/fusenpack/fusen-basic v0.0.5-0.20231115062855-6f9b810c456b/go.mod h1:Zw6gt/0VwNmtG2Boqmo8+5H5AVuWsIVgCgp1ilkRyao=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=

View File

@ -0,0 +1,12 @@
package logic
import (
"log"
"testing"
)
func TestRe(t *testing.T) {
fsHeader := `{"code": 200 , "msg":"asd"}`
code := codeRE.FindStringSubmatch(string(fsHeader))
log.Println(code)
}

View File

@ -3,8 +3,10 @@ package logic
import (
"bytes"
"context"
"fmt"
"fusen-basic/basic"
"net/http"
"regexp"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/protobuf/encoding/protojson"
@ -28,13 +30,34 @@ var fsProtoJSON = &runtime.JSONPb{
},
}
var codeRE = regexp.MustCompile(`(?i)"code"\s*:\s*(\d+)`)
func WriteFusenResponse(fsHeader string, w http.ResponseWriter, resp proto.Message) error {
w.WriteHeader(200)
var buf = bytes.NewBufferString(fsHeader[:len(fsHeader)-1] + ",")
rdata, err := fsProtoJSON.Marshal(resp)
if err != nil {
return err
}
buf.WriteString(`"data":`)
buf.Write(rdata)
buf.WriteString(`}`)
w.Write(buf.Bytes())
return nil
}
func ResponseHeaderMatcher(ctx context.Context, w http.ResponseWriter, resp proto.Message) error {
headers := w.Header()
key := http.CanonicalHeaderKey(basic.GM_FusenResponse.GrpcMetadataKey())
if grpcResp, ok := headers[key]; ok {
// w.Header().Set("Location", location[0])
// w.WriteHeader(http.StatusFound)
fsHeader := grpcResp[0]
code := codeRE.FindStringSubmatch(string(fsHeader))
if len(code) > 0 && code[1] != "200" {
return WriteFusenResponse(fsHeader, w, resp)
}
// 判断自定返回的内容, 用html的页面返回等...
selfwrite := http.CanonicalHeaderKey(basic.GM_SelfWrite.GrpcMetadataKey())
@ -52,21 +75,9 @@ func ResponseHeaderMatcher(ctx context.Context, w http.ResponseWriter, resp prot
return nil
}
w.WriteHeader(200)
fsHeader := grpcResp[0]
var buf = bytes.NewBufferString(fsHeader[:len(fsHeader)-1] + ",")
rdata, err := fsProtoJSON.Marshal(resp)
if err != nil {
return err
}
buf.WriteString(`"data":`)
buf.Write(rdata)
buf.WriteString(`}`)
w.Write(buf.Bytes())
return nil
return WriteFusenResponse(fsHeader, w, resp)
}
w.Write([]byte("GM_FusenResponse error"))
w.Write([]byte(fmt.Sprintf("%s error", basic.GM_FusenResponse.String())))
return nil
}