1 Star 0 Fork 0

不平凡的平凡 / rpcx-gateway

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server.go 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
鸟窝 提交于 2020-05-21 10:49 . parse servicePath in header
package echo
import (
"net/http"
"strings"
"github.com/labstack/echo"
. "github.com/rpcxio/rpcx-gateway"
)
type Server struct {
addr string
e *echo.Echo
}
// New returns a server.
func New(addr string) *Server {
return &Server{
addr: addr,
}
}
// NewWithEcho returns a server with preconfigured echo.
func NewWithEcho(addr string, e *echo.Echo) *Server {
return &Server{
addr: addr,
e: e,
}
}
// RegisterHandler configures the handler to handle http rpcx invoke.
// It wraps ServiceHandler into httprouter.Handle.
func (s *Server) RegisterHandler(base string, handler ServiceHandler) {
e := s.e
if e == nil {
e = echo.New()
}
h := wrapServiceHandler(handler)
e.POST(base, h)
e.GET(base, h)
e.PUT(base, h)
s.e = e
}
func wrapServiceHandler(handler ServiceHandler) echo.HandlerFunc {
return func(ctx echo.Context) error {
r := ctx.Request()
w := ctx.Response()
if r.Header.Get(XServicePath) == "" {
servicePath := ctx.Param("servicePath")
if strings.HasPrefix(servicePath, "/") {
servicePath = servicePath[1:]
}
r.Header.Set(XServicePath, servicePath)
}
servicePath := r.Header.Get(XServicePath)
messageID := r.Header.Get(XMessageID)
wh := w.Header()
if messageID != "" {
wh.Set(XMessageID, messageID)
}
meta, payload, err := handler(r, servicePath)
for k, v := range meta {
wh.Set(k, v)
}
if err == nil {
ctx.Blob(http.StatusOK, "application/octet-stream", payload)
return nil
}
rh := r.Header
for k, v := range rh {
if strings.HasPrefix(k, "X-RPCX-") && len(v) > 0 {
wh.Set(k, v[0])
}
}
wh.Set(XMessageStatusType, "Error")
wh.Set(XErrorMessage, err.Error())
ctx.String(http.StatusOK, err.Error())
return nil
}
}
func (s *Server) Serve() error {
return s.e.Start(s.addr)
}
1
https://gitee.com/lflxp/rpcx-gateway.git
git@gitee.com:lflxp/rpcx-gateway.git
lflxp
rpcx-gateway
rpcx-gateway
9d18d81fd810

搜索帮助