-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
160 lines (132 loc) · 3.29 KB
/
main.go
File metadata and controls
160 lines (132 loc) · 3.29 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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"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"`
}
// 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"
}
},
"required": ["count"]
}`)
}
func (r *Response) Unmarshal(data json.RawMessage) error {
return json.Unmarshal(data, r)
}
// =============================================================================
// 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,
}, nil
}
func CountHandler(ctx context.Context, r *http.Request, params model.Nil) (rsp *Response, err error) {
return &Response{
Count: count,
}, 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(CountHandler).
Path("/count").
WithOpID("count").
WithSummary("Get the current count").
WithDesc("Get the current count of the counter"))
// 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("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)
}
}