rpc/adapter/fasthttp/adapter.go
crusader 168c78690c ing
2017-10-26 21:37:28 +09:00

52 lines
1.1 KiB
Go

package fasthttp
import (
"fmt"
"io"
"strings"
"git.loafle.net/commons_go/rpc"
"github.com/valyala/fasthttp"
)
type FastHTTPAdapter struct {
registry rpc.Registry
}
// FastHTTPHandler
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]
}
// err := a.registry.Invoke(contentType, ctx.Request., ctx, beforeWrite, afterWrite)
// if nil != err {
// writeError(ctx, 400, err.Error())
// }
}
func beforeWrite(w io.Writer) {
ctx := w.(*fasthttp.RequestCtx)
ctx.Response.Header.Set("x-content-type-options", "nosniff")
ctx.SetContentType("application/json; charset=utf-8")
}
func afterWrite(w io.Writer) {
}
func writeError(ctx *fasthttp.RequestCtx, status int, msg string) {
ctx.SetStatusCode(status)
ctx.SetContentType("text/plain; charset=utf-8")
fmt.Fprint(ctx, msg)
}