-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
206 lines (171 loc) · 4.21 KB
/
main.go
File metadata and controls
206 lines (171 loc) · 4.21 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/tailbits/mason"
"github.com/tailbits/mason/model"
"github.com/tailbits/mason/openapi"
)
var _ model.Entity = (*Input)(nil)
// Input Model
type Input struct {
Increment *int `json:"increment"`
}
// Example implements model.Entity.
func (r *Input) Example() []byte {
return []byte(`{
"increment": 5
}`)
}
func (r *Input) Marshal() (json.RawMessage, error) {
return json.Marshal(r)
}
func (r *Input) Name() string {
return "IncrementInput"
}
func (r *Input) Schema() []byte {
return []byte(`{
"type": "object",
"properties": {
"increment": {
"type": ["integer", "null"]
}
},
"additionalProperties": false
}`)
}
func (r *Input) Unmarshal(data json.RawMessage) error {
return json.Unmarshal(data, r)
}
// Output Model
var _ model.Entity = (*Response)(nil)
type Response struct {
Count int `json:"count"`
Server HealthResponse `json:"server"`
}
// Example implements model.Entity.
func (r *Response) Example() []byte {
return []byte(`{
"count": 5
}`)
}
func (r *Response) Marshal() (json.RawMessage, error) {
return json.Marshal(r)
}
func (r *Response) Name() string {
return "CountResponse"
}
func (r *Response) Schema() []byte {
return []byte(`{
"type": "object",
"properties": {
"count": {
"type": "integer"
},
"server": {
"$ref": "#/definitions/HealthResponse"
}
},
"required": ["count"]
}`)
}
func (r *Response) Unmarshal(data json.RawMessage) error {
return json.Unmarshal(data, r)
}
// HealthResponse Model
var _ model.Entity = (*Response)(nil)
type HealthResponse struct {
Timestamp time.Time `json:"timestamp"`
}
// Example implements model.Entity.
func (r *HealthResponse) Example() []byte {
return []byte(`{
"timestamp": "2023-10-01T12:00:00Z"
}`)
}
func (r *HealthResponse) Marshal() (json.RawMessage, error) {
return json.Marshal(r)
}
func (r *HealthResponse) Name() string {
return "HealthResponse"
}
func (r *HealthResponse) Schema() []byte {
return []byte(`{
"type": "object",
"properties": {
"timestamp": {
"type": "string",
"format": "date-time"
}
},
"required": ["timestamp"]
}`)
}
func (r *HealthResponse) Unmarshal(data json.RawMessage) error {
return json.Unmarshal(data, r)
}
func HealthCheckHandler(ctx context.Context, r *http.Request, params model.Nil) (rsp *HealthResponse, err error) {
return &HealthResponse{
Timestamp: time.Now().UTC(),
}, nil
}
// =============================================================================
// Handlers
var count int
func IncrementHandler(ctx context.Context, r *http.Request, inp *Input, params model.Nil) (rsp *Response, err error) {
inc := 1
if inp.Increment != nil {
inc = *inp.Increment
}
count += inc
return &Response{
Count: count,
Server: HealthResponse{
Timestamp: time.Now().UTC(),
},
}, nil
}
func main() {
rtm := mason.NewHTTPRuntime()
api := mason.NewAPI(rtm)
grp := api.NewRouteGroup("counter")
grp.Register(mason.HandlePost(IncrementHandler).
Path("/increment").
WithOpID("increment").
WithSummary("Increment the counter").
WithDesc("Increment the counter by one, or the supplied increment"))
grp.Register(mason.HandleGet(HealthCheckHandler).
Path("/healthcheck").
WithOpID("healthcheck").
WithSummary("Get the server status"))
// Generate the OpenAPI schema
gen, err := openapi.NewGenerator(api)
if err != nil {
panic(fmt.Errorf("failed to create OpenAPI generator: %w", err))
}
gen.Spec.Info.WithTitle("Healthy Counter API")
schema, err := gen.Schema()
if err != nil {
panic(fmt.Errorf("failed to generate OpenAPI schema: %w", err))
}
// We can mix mason endpoints, with standard HTTP handlers
rtm.Handle("GET", "/openapi.json", func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(schema); err != nil {
return fmt.Errorf("failed to write OpenAPI schema: %w", err)
}
return nil
})
server := &http.Server{
Addr: ":9090",
Handler: rtm,
}
fmt.Println("API URL : http://localhost:9090")
fmt.Println("OpenAPI spec : http://localhost:9090/openapi.json")
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}