-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
126 lines (111 loc) · 2.81 KB
/
server.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
package shiftapi
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"time"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3gen"
)
const defaultReadTimeout = 10 * time.Second
const defaultWriteTimeout = 10 * time.Second
const defaultShutdownGracePeriod = 10 * time.Second
type ShiftAPI struct {
spec *openapi3.T
specGen *openapi3gen.Generator
mux *http.ServeMux
}
func New(
options ...func(*ShiftAPI) *ShiftAPI,
) *ShiftAPI {
mux := http.NewServeMux()
spec := &openapi3.T{
OpenAPI: "3.1",
Paths: &openapi3.Paths{},
Components: &openapi3.Components{
Schemas: make(openapi3.Schemas),
},
}
api := &ShiftAPI{
spec: spec,
specGen: openapi3gen.NewGenerator(),
mux: mux,
}
for _, option := range options {
api = option(api)
}
mux.HandleFunc("GET /openapi.json", api.serveSchema)
mux.HandleFunc("GET /docs", api.serveDocs)
mux.HandleFunc("GET /", api.redirectTo("/docs"))
return api
}
// Register adds 1 or more handlers to the server.
// The handlers are expected to be created via the shiftapi.Post, shiftapi.Get,
// shiftapi.Put, shiftapi.Patch, and shiftapi.Delete functions.
func (s *ShiftAPI) Register(handlers ...Handler) error {
for _, h := range handlers {
if err := h.register(s); err != nil {
return err
}
}
return nil
}
func (s *ShiftAPI) redirectTo(path string) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
http.Redirect(res, req, path, http.StatusTemporaryRedirect)
}
}
func (s *ShiftAPI) serveSchema(res http.ResponseWriter, req *http.Request) {
x, err := s.spec.MarshalYAML()
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
return
}
b, err := json.MarshalIndent(x, "", " ")
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
return
}
res.Header().Set("Content-Type", "application/json")
_, _ = res.Write(b)
}
func (s *ShiftAPI) serveDocs(res http.ResponseWriter, req *http.Request) {
title := ""
if s.spec.Info != nil {
title = s.spec.Info.Title
}
err := genRedocHTML(redocData{
Title: title,
SpecURL: "/openapi.json",
}, res)
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusInternalServerError)
}
}
func (s *ShiftAPI) ListenAndServe(ctx context.Context, addr string) error {
// TODO add address to schema
httpServer := &http.Server{
Addr: addr,
Handler: s.mux,
ReadTimeout: defaultReadTimeout,
WriteTimeout: defaultWriteTimeout,
BaseContext: func(listener net.Listener) context.Context {
return ctx
},
}
errCh := make(chan error, 1)
go func() {
errCh <- httpServer.ListenAndServe()
}()
select {
case <-ctx.Done():
ctx, cancel := context.WithTimeout(context.Background(), defaultShutdownGracePeriod)
defer cancel()
return httpServer.Shutdown(ctx)
case err := <-errCh:
return err
}
}