Skip to content

Commit cf24adf

Browse files
committed
Adds usage examples
Signed-off-by: Tiago Angelo <kurtis.angelo@gmail.com>
1 parent 38a71e7 commit cf24adf

3 files changed

Lines changed: 182 additions & 1 deletion

File tree

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
11
# Go OpenAPI
22

33
This project extends the [go-chi](https://github.com/go-chi/chi) router to support OpenAPI 3, bringing to you a simple
4-
interface to build a router conforming your API contract.
4+
interface to build a router conforming your API contract.
5+
6+
## Examples
7+
8+
See [_examples/](https://github.com/angelokurtis/go-openapi/blob/main/_examples/) for more examples.
9+
10+
**As easy as:**
11+
12+
```go
13+
package main
14+
15+
import (
16+
_ "embed"
17+
"net/http"
18+
19+
"github.com/go-chi/chi/v5"
20+
"github.com/go-chi/chi/v5/middleware"
21+
"github.com/go-chi/render"
22+
23+
"github.com/angelokurtis/go-openapi"
24+
)
25+
26+
//go:embed openapi.yaml
27+
var contract []byte
28+
29+
func main() {
30+
r := openapi.NewRouter(contract)
31+
r.Use(middleware.Logger)
32+
r.HandleOperation("getInvites", func(w http.ResponseWriter, r *http.Request) {
33+
render.Status(r, http.StatusOK)
34+
render.JSON(w, r, map[string]string{"operation": "getInvites"})
35+
})
36+
r.HandleOperation("createInvite", func(w http.ResponseWriter, r *http.Request) {
37+
render.Status(r, http.StatusCreated)
38+
})
39+
r.HandleOperation("deleteInvite", func(w http.ResponseWriter, r *http.Request) {
40+
_ = chi.URLParam(r, "inviteId")
41+
render.Status(r, http.StatusNoContent)
42+
})
43+
http.ListenAndServe(":3000", r)
44+
}
45+
```

_examples/guestlist/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
_ "embed"
5+
"net/http"
6+
7+
"github.com/go-chi/chi/v5"
8+
"github.com/go-chi/chi/v5/middleware"
9+
"github.com/go-chi/render"
10+
11+
"github.com/angelokurtis/go-openapi"
12+
)
13+
14+
//go:embed openapi.yaml
15+
var contract []byte
16+
17+
func main() {
18+
r := openapi.NewRouter(contract)
19+
r.Use(middleware.Logger)
20+
r.HandleOperation("getInvites", func(w http.ResponseWriter, r *http.Request) {
21+
render.Status(r, http.StatusOK)
22+
render.JSON(w, r, map[string]string{"operation": "getInvites"})
23+
})
24+
r.HandleOperation("createInvite", func(w http.ResponseWriter, r *http.Request) {
25+
render.Status(r, http.StatusCreated)
26+
})
27+
r.HandleOperation("deleteInvite", func(w http.ResponseWriter, r *http.Request) {
28+
_ = chi.URLParam(r, "inviteId")
29+
render.Status(r, http.StatusNoContent)
30+
})
31+
http.ListenAndServe(":3000", r)
32+
}

_examples/guestlist/openapi.yaml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Guestlist
4+
version: 1.0.0
5+
paths:
6+
/invites:
7+
summary: Path used to manage the list of invites.
8+
description: 'The REST endpoint/path used to list and create zero or more `Invite` entities. This path contains a `GET` and `POST` operation to perform the list and create tasks, respectively.'
9+
get:
10+
responses:
11+
'200':
12+
content:
13+
application/json:
14+
schema:
15+
type: array
16+
items:
17+
$ref: '#/components/schemas/Invite'
18+
description: Successful response - returns an array of `Invite` entities.
19+
operationId: getInvites
20+
summary: List All Invites
21+
description: Gets a list of all `Invite` entities.
22+
post:
23+
requestBody:
24+
description: A new `Invite` to be created.
25+
content:
26+
application/json:
27+
schema:
28+
$ref: '#/components/schemas/Invite'
29+
required: true
30+
responses:
31+
'201':
32+
description: Successful response.
33+
operationId: createInvite
34+
summary: Create a Invite
35+
description: Creates a new instance of a `Invite`.
36+
'/invites/{inviteId}':
37+
summary: Path used to manage a single Invite.
38+
description: 'The REST endpoint/path used to get, update, and delete single instances of an `Invite`. This path contains `GET`, `PUT`, and `DELETE` operations used to perform the get, update, and delete tasks, respectively.'
39+
get:
40+
responses:
41+
'200':
42+
content:
43+
application/json:
44+
schema:
45+
$ref: '#/components/schemas/Invite'
46+
description: Successful response - returns a single `Invite`.
47+
operationId: getInvite
48+
summary: Get a Invite
49+
description: Gets the details of a single instance of a `Invite`.
50+
put:
51+
requestBody:
52+
description: Updated `Invite` information.
53+
content:
54+
application/json:
55+
schema:
56+
$ref: '#/components/schemas/Invite'
57+
required: true
58+
responses:
59+
'202':
60+
description: Successful response.
61+
operationId: updateInvite
62+
summary: Update a Invite
63+
description: Updates an existing `Invite`.
64+
delete:
65+
responses:
66+
'204':
67+
description: Successful response.
68+
operationId: deleteInvite
69+
summary: Delete a Invite
70+
description: Deletes an existing `Invite`.
71+
parameters:
72+
- name: inviteId
73+
description: A unique identifier for a `Invite`.
74+
schema:
75+
type: string
76+
in: path
77+
required: true
78+
components:
79+
schemas:
80+
Invite:
81+
title: Root Type for Invite
82+
description: ''
83+
type: object
84+
example:
85+
age: 42
86+
name: Athena
87+
status: CONFIRMED
88+
properties:
89+
name:
90+
type: string
91+
age:
92+
format: int32
93+
type: integer
94+
status:
95+
type: string
96+
enum:
97+
- CREATED
98+
- ACCEPTED
99+
- DECLINED
100+
- CONFIRMED
101+
- DENIED
102+
default: CREATED
103+
readOnly: true
104+
required:
105+
- name
106+
servers:
107+
- description: local
108+
url: 'http://localhost:3000'

0 commit comments

Comments
 (0)