-
Notifications
You must be signed in to change notification settings - Fork 4
/
service.go
142 lines (122 loc) · 3.47 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package api
import (
"fmt"
"net/http"
"github.com/clubpay/ronykit/example/simple-rest-server/dto"
"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/desc"
"github.com/clubpay/ronykit/std/gateways/fasthttp"
)
var SampleDesc desc.ServiceDescFunc = func() *desc.Service {
return desc.NewService("SampleService").
SetEncoding(kit.JSON).
AddError(dto.Err(http.StatusBadRequest, "INPUT")).
AddContract(
desc.NewContract().
SetInput(&dto.EchoRequest{}).
SetOutput(&dto.EchoResponse{}).
NamedSelector("EchoGET", fasthttp.REST(http.MethodGet, "/echo/:randomID")).
NamedSelector("EchoPOST", fasthttp.REST(http.MethodPost, "/echo-post")).
NamedSelector("EchoRPC", fasthttp.RPC("echoRequest")).
AddModifier(func(envelope *kit.Envelope) {
envelope.SetHdr("X-Custom-Header", "justForTestingModifier")
}).
SetHandler(EchoHandler),
).
AddContract(
desc.NewContract().
SetName("Sum").
SetInput(&dto.SumRequest{}).
SetOutput(&dto.SumResponse{}).
NamedSelector("Sum1", fasthttp.REST(http.MethodGet, "/sum/:val1/:val2")).
NamedSelector("Sum2", fasthttp.REST(http.MethodPost, "/sum")).
SetHandler(SumHandler),
).
AddContract(
desc.NewContract().
SetInput(&dto.SumRequest{}).
SetOutput(&dto.SumResponse{}).
NamedSelector("SumRedirect", fasthttp.REST(http.MethodGet, "/sum-redirect/:val1/:val2")).
Selector(fasthttp.REST(http.MethodPost, "/sum-redirect")).
SetHandler(SumRedirectHandler),
).
AddContract(
desc.NewContract().
SetInput(&dto.RedirectRequest{}).
Selector(fasthttp.REST(http.MethodGet, "/redirect")).
SetHandler(Redirect),
).
AddContract(
desc.NewContract().
SetInput(kit.RawMessage{}).
SetOutput(kit.RawMessage{}).
Selector(fasthttp.REST(http.MethodPost, "/raw_echo")).
SetHandler(RawEchoHandler),
)
}
func EchoHandler(ctx *kit.Context) {
//nolint:forcetypeassert
req := ctx.In().GetMsg().(*dto.EchoRequest)
ctx.In().Reply().
SetHdr("Content-Type", "application/json").
SetMsg(
&dto.EchoResponse{
RandomID: req.RandomID,
Ok: req.Ok,
},
).Send()
return
}
func RawEchoHandler(ctx *kit.Context) {
//nolint:forcetypeassert
req := ctx.In().GetMsg().(kit.RawMessage)
ctx.In().Reply().SetMsg(req).Send()
return
}
func SumHandler(ctx *kit.Context) {
//nolint:forcetypeassert
req := ctx.In().GetMsg().(*dto.SumRequest)
ctx.In().Reply().
SetHdr("Content-Type", "application/json").
SetMsg(
&dto.SumResponse{
EmbeddedHeader: req.EmbeddedHeader,
Val: req.Val1 + req.Val2,
},
).Send()
return
}
func SumRedirectHandler(ctx *kit.Context) {
//nolint:forcetypeassert
req := ctx.In().GetMsg().(*dto.SumRequest)
rc, ok := ctx.Conn().(kit.RESTConn)
if !ok {
ctx.In().Reply().
SetMsg(dto.Err(http.StatusBadRequest, "Only supports REST requests")).
Send()
return
}
switch rc.GetMethod() {
case fasthttp.MethodGet:
rc.Redirect(
http.StatusTemporaryRedirect,
fmt.Sprintf("http://%s/sum/%d/%d", rc.GetHost(), req.Val1, req.Val2),
)
case fasthttp.MethodPost:
rc.Redirect(
http.StatusTemporaryRedirect,
fmt.Sprintf("http://%s/sum", rc.GetHost()),
)
default:
ctx.In().Reply().
SetMsg(dto.Err(http.StatusBadRequest, "Unsupported method")).
Send()
return
}
return
}
func Redirect(ctx *kit.Context) {
req := ctx.In().GetMsg().(*dto.RedirectRequest) //nolint:forcetypeassert
rc := ctx.Conn().(kit.RESTConn) //nolint:forcetypeassert
rc.Redirect(http.StatusTemporaryRedirect, req.URL)
}