mirror of
https://github.com/foomo/gotsrpc.git
synced 2025-10-16 12:35:35 +00:00
177 lines
5.4 KiB
Go
177 lines
5.4 KiB
Go
// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT.
|
|
|
|
package backend
|
|
|
|
import (
|
|
io "io"
|
|
ioutil "io/ioutil"
|
|
http "net/http"
|
|
time "time"
|
|
|
|
gotsrpc "github.com/foomo/gotsrpc/v2"
|
|
)
|
|
|
|
const (
|
|
ServiceGoTSRPCProxyCustomError = "CustomError"
|
|
ServiceGoTSRPCProxyError = "Error"
|
|
ServiceGoTSRPCProxyMultiScalar = "MultiScalar"
|
|
ServiceGoTSRPCProxyScalar = "Scalar"
|
|
ServiceGoTSRPCProxyScalarError = "ScalarError"
|
|
ServiceGoTSRPCProxyTypedCustomError = "TypedCustomError"
|
|
ServiceGoTSRPCProxyTypedError = "TypedError"
|
|
ServiceGoTSRPCProxyTypedScalarError = "TypedScalarError"
|
|
ServiceGoTSRPCProxyTypedWrappedError = "TypedWrappedError"
|
|
ServiceGoTSRPCProxyWrappedError = "WrappedError"
|
|
)
|
|
|
|
type ServiceGoTSRPCProxy struct {
|
|
EndPoint string
|
|
service Service
|
|
}
|
|
|
|
func NewDefaultServiceGoTSRPCProxy(service Service) *ServiceGoTSRPCProxy {
|
|
return NewServiceGoTSRPCProxy(service, "/service/backend")
|
|
}
|
|
|
|
func NewServiceGoTSRPCProxy(service Service, endpoint string) *ServiceGoTSRPCProxy {
|
|
return &ServiceGoTSRPCProxy{
|
|
EndPoint: endpoint,
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
// ServeHTTP exposes your service
|
|
func (p *ServiceGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
if r.Method == http.MethodOptions {
|
|
return
|
|
}
|
|
gotsrpc.ErrorMethodNotAllowed(w)
|
|
return
|
|
}
|
|
defer io.Copy(ioutil.Discard, r.Body) // Drain Request Body
|
|
|
|
funcName := gotsrpc.GetCalledFunc(r, p.EndPoint)
|
|
callStats := gotsrpc.GetStatsForRequest(r)
|
|
if callStats != nil {
|
|
callStats.Func = funcName
|
|
callStats.Package = "github.com/foomo/gotsrpc/v2/example/errors/service/backend"
|
|
callStats.Service = "Service"
|
|
}
|
|
switch funcName {
|
|
case ServiceGoTSRPCProxyCustomError:
|
|
executionStart := time.Now()
|
|
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
|
|
customErrorE := p.service.CustomError(&rw, r)
|
|
if callStats != nil {
|
|
callStats.Execution = time.Now().Sub(executionStart)
|
|
}
|
|
if rw.Status() == http.StatusOK {
|
|
gotsrpc.Reply([]interface{}{customErrorE}, callStats, r, w)
|
|
}
|
|
return
|
|
case ServiceGoTSRPCProxyError:
|
|
executionStart := time.Now()
|
|
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
|
|
errorE := p.service.Error(&rw, r)
|
|
if callStats != nil {
|
|
callStats.Execution = time.Now().Sub(executionStart)
|
|
}
|
|
if rw.Status() == http.StatusOK {
|
|
gotsrpc.Reply([]interface{}{errorE}, callStats, r, w)
|
|
}
|
|
return
|
|
case ServiceGoTSRPCProxyMultiScalar:
|
|
executionStart := time.Now()
|
|
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
|
|
multiScalarE := p.service.MultiScalar(&rw, r)
|
|
if callStats != nil {
|
|
callStats.Execution = time.Now().Sub(executionStart)
|
|
}
|
|
if rw.Status() == http.StatusOK {
|
|
gotsrpc.Reply([]interface{}{multiScalarE}, callStats, r, w)
|
|
}
|
|
return
|
|
case ServiceGoTSRPCProxyScalar:
|
|
executionStart := time.Now()
|
|
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
|
|
scalarE := p.service.Scalar(&rw, r)
|
|
if callStats != nil {
|
|
callStats.Execution = time.Now().Sub(executionStart)
|
|
}
|
|
if rw.Status() == http.StatusOK {
|
|
gotsrpc.Reply([]interface{}{scalarE}, callStats, r, w)
|
|
}
|
|
return
|
|
case ServiceGoTSRPCProxyScalarError:
|
|
executionStart := time.Now()
|
|
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
|
|
scalarErrorE := p.service.ScalarError(&rw, r)
|
|
if callStats != nil {
|
|
callStats.Execution = time.Now().Sub(executionStart)
|
|
}
|
|
if rw.Status() == http.StatusOK {
|
|
gotsrpc.Reply([]interface{}{scalarErrorE}, callStats, r, w)
|
|
}
|
|
return
|
|
case ServiceGoTSRPCProxyTypedCustomError:
|
|
executionStart := time.Now()
|
|
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
|
|
typedCustomErrorE := p.service.TypedCustomError(&rw, r)
|
|
if callStats != nil {
|
|
callStats.Execution = time.Now().Sub(executionStart)
|
|
}
|
|
if rw.Status() == http.StatusOK {
|
|
gotsrpc.Reply([]interface{}{typedCustomErrorE}, callStats, r, w)
|
|
}
|
|
return
|
|
case ServiceGoTSRPCProxyTypedError:
|
|
executionStart := time.Now()
|
|
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
|
|
typedErrorE := p.service.TypedError(&rw, r)
|
|
if callStats != nil {
|
|
callStats.Execution = time.Now().Sub(executionStart)
|
|
}
|
|
if rw.Status() == http.StatusOK {
|
|
gotsrpc.Reply([]interface{}{typedErrorE}, callStats, r, w)
|
|
}
|
|
return
|
|
case ServiceGoTSRPCProxyTypedScalarError:
|
|
executionStart := time.Now()
|
|
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
|
|
typedScalarErrorE := p.service.TypedScalarError(&rw, r)
|
|
if callStats != nil {
|
|
callStats.Execution = time.Now().Sub(executionStart)
|
|
}
|
|
if rw.Status() == http.StatusOK {
|
|
gotsrpc.Reply([]interface{}{typedScalarErrorE}, callStats, r, w)
|
|
}
|
|
return
|
|
case ServiceGoTSRPCProxyTypedWrappedError:
|
|
executionStart := time.Now()
|
|
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
|
|
typedWrappedErrorE := p.service.TypedWrappedError(&rw, r)
|
|
if callStats != nil {
|
|
callStats.Execution = time.Now().Sub(executionStart)
|
|
}
|
|
if rw.Status() == http.StatusOK {
|
|
gotsrpc.Reply([]interface{}{typedWrappedErrorE}, callStats, r, w)
|
|
}
|
|
return
|
|
case ServiceGoTSRPCProxyWrappedError:
|
|
executionStart := time.Now()
|
|
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
|
|
wrappedErrorE := p.service.WrappedError(&rw, r)
|
|
if callStats != nil {
|
|
callStats.Execution = time.Now().Sub(executionStart)
|
|
}
|
|
if rw.Status() == http.StatusOK {
|
|
gotsrpc.Reply([]interface{}{wrappedErrorE}, callStats, r, w)
|
|
}
|
|
return
|
|
default:
|
|
gotsrpc.ClearStats(r)
|
|
http.Error(w, "404 - not found "+r.URL.Path, http.StatusNotFound)
|
|
}
|
|
}
|