This commit is contained in:
eson
2023-11-02 18:40:22 +08:00
commit 5785fea6b8
12 changed files with 772 additions and 0 deletions

7
server/config/config.go Normal file
View File

@@ -0,0 +1,7 @@
package config
type Config struct {
Name string `yaml:"Name"`
Host string `yaml:"Host"`
Port uint `yaml:"Port"`
}

View File

@@ -0,0 +1,25 @@
package logic
import (
"context"
"fusen-gateway/gen/go/service"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)
func AutoRegisterHandler(ctx context.Context, mux *runtime.ServeMux, opts ...grpc.DialOption) error {
var err error
err = service.RegisterAuthHandlerClientNacos(ctx, mux, opts...)
if err != nil {
return err
}
err = service.RegisterInfoHandlerClientNacos(ctx, mux, opts...)
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,56 @@
package logic
import (
"context"
"encoding/json"
"fusen-gateway/proto/goutils/basic"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type EmptyMarshaler struct {
runtime.JSONPb
}
func (m *EmptyMarshaler) Marshal(v interface{}) ([]byte, error) {
return nil, nil
}
var fsProtoJSON = &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
EmitUnpopulated: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
}
func ResponseHeaderMatcher(ctx context.Context, w http.ResponseWriter, resp proto.Message) error {
headers := w.Header()
key := http.CanonicalHeaderKey("Grpc-Metadata-" + string(basic.M_FusenResponse))
w.WriteHeader(200)
if location, ok := headers[key]; ok {
// w.Header().Set("Location", location[0])
// w.WriteHeader(http.StatusFound)
jbuf := []byte(location[0])
gresp := &basic.GatewayResponse{}
err := json.Unmarshal(jbuf, gresp)
if err != nil {
return err
}
gresp.Data = resp
rdata, err := fsProtoJSON.Marshal(resp)
if err != nil {
return err
}
w.Write(rdata)
}
// w.WriteHeader(200)
// w.Write([]byte("error"))
return nil
}

44
server/main.go Normal file
View File

@@ -0,0 +1,44 @@
package main
import (
"context"
"fusen-gateway/gen/go/service"
"fusen-gateway/proto/goutils"
"fusen-gateway/server/config"
"fusen-gateway/server/logic"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
fusen := goutils.NewFusen[config.Config]()
fusen.StartNacos(nil)
service.AutoGrpcInit(fusen)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux(
runtime.WithForwardResponseOption(logic.ResponseHeaderMatcher),
runtime.WithMarshalerOption("*", &logic.EmptyMarshaler{}),
)
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
err := logic.AutoRegisterHandler(ctx, mux, opts...)
if err != nil {
panic(err)
}
err = http.ListenAndServe(":9090", mux)
if err != nil {
panic(err)
}
}

11
server/main_test.go Normal file
View File

@@ -0,0 +1,11 @@
package main
import (
"log"
"testing"
)
func TestMain(t *testing.T) {
log.SetFlags(log.Llongfile)
main()
}

View File

@@ -0,0 +1,2 @@
SERVICE_NAME=gateway #需要修改当前项目的名字
PROJECT_NAME=gitee.com/fusenpack/fusen-gateway #需要修改项目的仓库