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

110 lines
3.0 KiB
Go

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