gotsrpc/example/errors/service/frontend/gotsrpc_gen.go
2022-03-02 15:18:09 +01:00

85 lines
2.1 KiB
Go

// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT.
package frontend
import (
io "io"
ioutil "io/ioutil"
http "net/http"
time "time"
gotsrpc "github.com/foomo/gotsrpc/v2"
)
func init() {
gotsrpc.MustRegisterUnionExt(ErrMulti{})
}
const (
ServiceGoTSRPCProxyMultiple = "Multiple"
ServiceGoTSRPCProxySimple = "Simple"
)
type ServiceGoTSRPCProxy struct {
EndPoint string
service Service
}
func NewDefaultServiceGoTSRPCProxy(service Service) *ServiceGoTSRPCProxy {
return NewServiceGoTSRPCProxy(service, "/service/frontend")
}
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/frontend"
callStats.Service = "Service"
}
switch funcName {
case ServiceGoTSRPCProxyMultiple:
executionStart := time.Now()
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
multipleE := p.service.Multiple(&rw, r)
if callStats != nil {
callStats.Execution = time.Now().Sub(executionStart)
}
if rw.Status() == http.StatusOK {
gotsrpc.Reply([]interface{}{multipleE}, callStats, r, w)
}
return
case ServiceGoTSRPCProxySimple:
executionStart := time.Now()
rw := gotsrpc.ResponseWriter{ResponseWriter: w}
simpleE := p.service.Simple(&rw, r)
if callStats != nil {
callStats.Execution = time.Now().Sub(executionStart)
}
if rw.Status() == http.StatusOK {
gotsrpc.Reply([]interface{}{simpleE}, callStats, r, w)
}
return
default:
gotsrpc.ClearStats(r)
http.Error(w, "404 - not found "+r.URL.Path, http.StatusNotFound)
}
}