-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.go
132 lines (118 loc) · 4.11 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
127
128
129
130
131
132
// Code generated by goa v3.0.9, DO NOT EDIT.
//
// swagger HTTP server
//
// Command:
// $ goa gen github.com/artefactual-labs/enduro/internal/api/design -o
// internal/api
package server
import (
"context"
"net/http"
swagger "github.com/artefactual-labs/enduro/internal/api/gen/swagger"
goahttp "goa.design/goa/v3/http"
"goa.design/plugins/v3/cors"
)
// Server lists the swagger service endpoint HTTP handlers.
type Server struct {
Mounts []*MountPoint
CORS http.Handler
}
// ErrorNamer is an interface implemented by generated error structs that
// exposes the name of the error as defined in the design.
type ErrorNamer interface {
ErrorName() string
}
// MountPoint holds information about the mounted endpoints.
type MountPoint struct {
// Method is the name of the service method served by the mounted HTTP handler.
Method string
// Verb is the HTTP method used to match requests to the mounted handler.
Verb string
// Pattern is the HTTP request path pattern used to match requests to the
// mounted handler.
Pattern string
}
// New instantiates HTTP handlers for all the swagger service endpoints using
// the provided encoder and decoder. The handlers are mounted on the given mux
// using the HTTP verb and path defined in the design. errhandler is called
// whenever a response fails to be encoded. formatter is used to format errors
// returned by the service methods prior to encoding. Both errhandler and
// formatter are optional and can be nil.
func New(
e *swagger.Endpoints,
mux goahttp.Muxer,
decoder func(*http.Request) goahttp.Decoder,
encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
errhandler func(context.Context, http.ResponseWriter, error),
formatter func(err error) goahttp.Statuser,
) *Server {
return &Server{
Mounts: []*MountPoint{
{"CORS", "OPTIONS", "/swagger/swagger.json"},
{"internal/api/gen/http/openapi.json", "GET", "/swagger/swagger.json"},
},
CORS: NewCORSHandler(),
}
}
// Service returns the name of the service served.
func (s *Server) Service() string { return "swagger" }
// Use wraps the server handlers with the given middleware.
func (s *Server) Use(m func(http.Handler) http.Handler) {
s.CORS = m(s.CORS)
}
// Mount configures the mux to serve the swagger endpoints.
func Mount(mux goahttp.Muxer, h *Server) {
MountCORSHandler(mux, h.CORS)
MountInternalAPIGenHTTPOpenapiJSON(mux, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "internal/api/gen/http/openapi.json")
}))
}
// MountInternalAPIGenHTTPOpenapiJSON configures the mux to serve GET request
// made to "/swagger/swagger.json".
func MountInternalAPIGenHTTPOpenapiJSON(mux goahttp.Muxer, h http.Handler) {
mux.Handle("GET", "/swagger/swagger.json", handleSwaggerOrigin(h).ServeHTTP)
}
// MountCORSHandler configures the mux to serve the CORS endpoints for the
// service swagger.
func MountCORSHandler(mux goahttp.Muxer, h http.Handler) {
h = handleSwaggerOrigin(h)
f, ok := h.(http.HandlerFunc)
if !ok {
f = func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
}
}
mux.Handle("OPTIONS", "/swagger/swagger.json", f)
}
// NewCORSHandler creates a HTTP handler which returns a simple 200 response.
func NewCORSHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
})
}
// handleSwaggerOrigin applies the CORS response headers corresponding to the
// origin for the service swagger.
func handleSwaggerOrigin(h http.Handler) http.Handler {
origHndlr := h.(http.HandlerFunc)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin == "" {
// Not a CORS request
origHndlr(w, r)
return
}
if cors.MatchOrigin(origin, "*") {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, OPTIONS")
}
origHndlr(w, r)
return
}
origHndlr(w, r)
return
})
}