package fasthttp import ( "bytes" "fmt" "strings" "git.loafle.net/commons_go/rpc/server" "github.com/valyala/fasthttp" ) type FastHTTPAdapter struct { RPCServerHandler server.ServerHandler } func New(rpcServerHandler server.ServerHandler) *FastHTTPAdapter { return &FastHTTPAdapter{ RPCServerHandler: rpcServerHandler, } } func (a *FastHTTPAdapter) FastHTTPHandler(ctx *fasthttp.RequestCtx) { if !ctx.IsPost() { writeError(ctx, 405, "rpc: POST method required, received "+string(ctx.Method())) return } contentType := string(ctx.Request.Header.ContentType()) idx := strings.Index(contentType, ";") if idx != -1 { contentType = contentType[:idx] } codec, err := a.RPCServerHandler.GetCodec(contentType) if nil != err { writeError(ctx, 415, "rpc: Unsupported Media Type "+contentType) return } r := bytes.NewReader(ctx.PostBody()) if err := server.Handle(a.RPCServerHandler, codec, r, ctx); nil != err { writeError(ctx, 500, err.Error()) return } } func writeError(ctx *fasthttp.RequestCtx, status int, msg string) { ctx.SetStatusCode(status) ctx.SetContentType("text/plain; charset=utf-8") fmt.Fprint(ctx, msg) }