refactor: add unified all service gateway
This commit is contained in:
48
all/internal/config/config.go
Normal file
48
all/internal/config/config.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
)
|
||||
|
||||
const ServiceKey = "all"
|
||||
|
||||
type ServiceConfig map[string]any
|
||||
|
||||
type SrvConfig struct {
|
||||
conf.Base `yaml:",inline"`
|
||||
Databases *conf.DBConf `yaml:"Databases"`
|
||||
Etcd *conf.EtcdConf `yaml:"Etcd"`
|
||||
Services map[string]ServiceConfig `yaml:"Services"`
|
||||
}
|
||||
|
||||
var Spec SrvConfig
|
||||
|
||||
func New() {
|
||||
conf.New(ServiceKey, &Spec)
|
||||
Spec.Port = conf.CheckPort(Spec.Port)
|
||||
Spec.BindIP = conf.CheckIP(Spec.BindIP)
|
||||
Spec.Addr = net.JoinHostPort(Spec.BindIP, Spec.Port)
|
||||
conf.NotNil(Spec.Service, Spec.Cache)
|
||||
conf.PrintInfo(Spec.Addr)
|
||||
}
|
||||
|
||||
func Enabled(name string) bool {
|
||||
if raw := strings.TrimSpace(os.Getenv("BSM_SERVICES")); raw != "" {
|
||||
for _, selected := range strings.Split(raw, ",") {
|
||||
if strings.EqualFold(strings.TrimSpace(selected), name) || strings.EqualFold(strings.TrimSpace(selected), "all") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
service, ok := Spec.Services[name]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
enabled, ok := service["Enable"].(bool)
|
||||
return !ok || enabled
|
||||
}
|
||||
21
all/internal/impl/impl.go
Normal file
21
all/internal/impl/impl.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"bsm/full/all/internal/config"
|
||||
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||
"git.apinb.com/bsm-sdk/core/with"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
RedisService *redis.RedisClient
|
||||
EtcdService *clientv3.Client
|
||||
DBService *gorm.DB
|
||||
)
|
||||
|
||||
func NewImpl() {
|
||||
RedisService = with.RedisCache(config.Spec.Cache)
|
||||
DBService = with.Databases(config.Spec.Databases, nil)
|
||||
EtcdService = with.Etcd(config.Spec.Etcd)
|
||||
}
|
||||
90
all/internal/server/server.go
Normal file
90
all/internal/server/server.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"golang.org/x/net/http2"
|
||||
"golang.org/x/net/http2/h2c"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
GRPC *grpc.Server
|
||||
Gateway *gwRuntime.ServeMux
|
||||
HTTP *gin.Engine
|
||||
server *http.Server
|
||||
}
|
||||
|
||||
func New() *Server {
|
||||
grpcServer := grpc.NewServer()
|
||||
reflection.Register(grpcServer)
|
||||
engine := gin.New()
|
||||
engine.Use(gin.Logger(), gin.Recovery())
|
||||
return &Server{
|
||||
GRPC: grpcServer,
|
||||
Gateway: gwRuntime.NewServeMux(),
|
||||
HTTP: engine,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Start(addr string) error {
|
||||
handler := h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.ProtoMajor == 2 && strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") {
|
||||
s.GRPC.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
recorder := newBufferedResponse()
|
||||
s.Gateway.ServeHTTP(recorder, r)
|
||||
if recorder.status != http.StatusNotFound {
|
||||
recorder.flush(w)
|
||||
return
|
||||
}
|
||||
s.HTTP.ServeHTTP(w, r)
|
||||
}), &http2.Server{})
|
||||
s.server = &http.Server{Addr: addr, Handler: handler}
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("all services listening on %s (gRPC + HTTP)\n", addr)
|
||||
return s.server.Serve(listener)
|
||||
}
|
||||
|
||||
func (s *Server) Stop(ctx context.Context) error {
|
||||
s.GRPC.GracefulStop()
|
||||
if s.server == nil {
|
||||
return nil
|
||||
}
|
||||
return s.server.Shutdown(ctx)
|
||||
}
|
||||
|
||||
type bufferedResponse struct {
|
||||
header http.Header
|
||||
body bytes.Buffer
|
||||
status int
|
||||
}
|
||||
|
||||
func newBufferedResponse() *bufferedResponse {
|
||||
return &bufferedResponse{header: make(http.Header), status: http.StatusOK}
|
||||
}
|
||||
|
||||
func (r *bufferedResponse) Header() http.Header { return r.header }
|
||||
func (r *bufferedResponse) WriteHeader(status int) { r.status = status }
|
||||
func (r *bufferedResponse) Write(data []byte) (int, error) { return r.body.Write(data) }
|
||||
func (r *bufferedResponse) flush(w http.ResponseWriter) {
|
||||
for key, values := range r.header {
|
||||
for _, value := range values {
|
||||
w.Header().Add(key, value)
|
||||
}
|
||||
}
|
||||
w.WriteHeader(r.status)
|
||||
_, _ = w.Write(r.body.Bytes())
|
||||
}
|
||||
21
all/internal/server/server_test.go
Normal file
21
all/internal/server/server_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestHTTPRouterIsAvailable(t *testing.T) {
|
||||
srv := New()
|
||||
srv.HTTP.GET("/healthz", func(c *gin.Context) { c.Status(http.StatusNoContent) })
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/healthz", nil)
|
||||
response := httptest.NewRecorder()
|
||||
srv.HTTP.ServeHTTP(response, request)
|
||||
if response.Code != http.StatusNoContent {
|
||||
t.Fatalf("unexpected status: %d", response.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user