Files
apinto/drivers/router/http-router/http-handler.go
2022-12-21 13:11:15 +08:00

72 lines
1.8 KiB
Go

package http_router
import (
"net/http"
"github.com/eolinker/eosc/utils"
http_service "github.com/eolinker/apinto/node/http-context"
http_complete "github.com/eolinker/apinto/drivers/router/http-router/http-complete"
"github.com/eolinker/apinto/service"
"github.com/eolinker/eosc/eocontext"
http_context "github.com/eolinker/eosc/eocontext/http-context"
)
var completeCaller = http_complete.NewHttpCompleteCaller()
type httpHandler struct {
completeHandler eocontext.CompleteHandler
routerName string
routerId string
serviceName string
finisher eocontext.FinishHandler
service service.IService
filters eocontext.IChainPro
disable bool
websocket bool
}
func (h *httpHandler) ServeHTTP(ctx eocontext.EoContext) {
httpContext, err := http_context.Assert(ctx)
if err != nil {
return
}
if h.disable {
httpContext.Response().SetStatus(http.StatusNotFound, "")
httpContext.Response().SetBody([]byte("router disable"))
httpContext.FastFinish()
return
}
if h.websocket {
wsCtx, err := http_service.NewWebsocketContext(httpContext)
if err != nil {
httpContext.Response().SetStatus(http.StatusInternalServerError, "")
httpContext.Response().SetBody([]byte(err.Error()))
httpContext.FastFinish()
return
}
ctx = wsCtx
}
globalLabels := utils.GlobalLabelGet()
for key, value := range globalLabels {
ctx.SetLabel(key, value)
}
//Set Label
ctx.SetLabel("api", h.routerName)
ctx.SetLabel("api_id", h.routerId)
ctx.SetLabel("service", h.serviceName)
ctx.SetLabel("service_id", h.service.Id())
ctx.SetLabel("ip", httpContext.Request().ReadIP())
ctx.SetFinish(h.finisher)
ctx.SetCompleteHandler(h.completeHandler)
ctx.SetApp(h.service)
ctx.SetBalance(h.service)
ctx.SetUpstreamHostHandler(h.service)
h.filters.Chain(ctx, completeCaller)
}