-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
173 lines (150 loc) · 4.71 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
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
package apiimpl
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/routers/gorillamux"
"github.com/MadAppGang/httplog"
"github.com/flowchartsman/swaggerui"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/getkin/kin-openapi/routers"
"github.com/go-chi/chi/v5"
"github.com/gorilla/sessions"
logging "github.com/ipfs/go-log/v2"
"github.com/jiaozifs/jiaozifs/api"
"github.com/jiaozifs/jiaozifs/auth"
"github.com/jiaozifs/jiaozifs/auth/crypt"
"github.com/jiaozifs/jiaozifs/config"
"github.com/jiaozifs/jiaozifs/models"
"github.com/rs/cors"
"go.uber.org/fx"
)
var log = logging.Logger("rpc")
const (
APIV1Prefix = "/api/v1"
extensionValidationExcludeBody = "x-validation-exclude-body"
)
func SetupAPI(lc fx.Lifecycle, apiConfig *config.APIConfig, secretStore crypt.SecretStore, sessionStore sessions.Store, repo models.IRepo, controller APIController) error {
swagger, err := api.GetSwagger()
if err != nil {
return err
}
// This is how you set up a basic chi router
r := chi.NewRouter()
r.Use(httplog.LoggerWithName("http"),
cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{
http.MethodHead,
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
}).Handler,
)
// Use our validation middleware to check all requests against the
// OpenAPI schema.
apiRouter := r.With(
OapiRequestValidatorWithOptions(swagger, &openapi3filter.Options{
AuthenticationFunc: openapi3filter.NoopAuthenticationFunc,
}),
auth.Middleware(swagger, nil, secretStore, repo.UserRepo(), sessionStore),
)
raw, err := api.RawSpec()
if err != nil {
return err
}
api.HandlerFromMuxWithBaseURL(controller, apiRouter, APIV1Prefix)
r.Handle("/api/docs/*", http.StripPrefix("/api/docs", swaggerui.Handler(raw)))
url, err := url.Parse(apiConfig.Listen)
if err != nil {
return err
}
listener, err := net.Listen("tcp", url.Host)
if err != nil {
return err
}
log.Infof("Start listen api %s", listener.Addr())
go func() {
err := http.Serve(listener, r)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Errorf("listen address fail %s", err)
}
}()
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return listener.Close()
},
})
return nil
}
// OapiRequestValidatorWithOptions Creates middleware to validate request by swagger spec.
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *openapi3filter.Options) func(next http.Handler) http.Handler {
router, err := gorillamux.NewRouter(swagger)
if err != nil {
panic(err)
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// validate request
if statusCode, err := validateRequest(r, router, options); err != nil {
http.Error(w, err.Error(), statusCode)
return
}
// serve
next.ServeHTTP(w, r)
})
}
}
// validateRequest is called from the middleware above and actually does the work
// of validating a request.
func validateRequest(r *http.Request, router routers.Router, options *openapi3filter.Options) (int, error) {
// Find route
route, pathParams, err := router.FindRoute(r)
if err != nil {
return http.StatusNotFound, err // We failed to find a matching route for the request.
}
// Validate request
requestValidationInput := &openapi3filter.RequestValidationInput{
Request: r,
PathParams: pathParams,
Route: route,
}
if options != nil {
optCopy := *options
requestValidationInput.Options = &optCopy
}
if _, ok := route.Operation.Extensions[extensionValidationExcludeBody]; ok {
requestValidationInput.Options.ExcludeRequestBody = true
}
if err := openapi3filter.ValidateRequest(r.Context(), requestValidationInput); err != nil {
me := openapi3.MultiError{}
if errors.As(err, &me) {
return http.StatusBadRequest, err
}
switch e := err.(type) {
case *openapi3filter.RequestError:
// We've got a bad request
// Split up the verbose error by lines and return the first one
// openapi errors seem to be multi-line with a decent message on the first
errorLines := strings.Split(e.Error(), "\n")
return http.StatusBadRequest, fmt.Errorf(errorLines[0])
case *openapi3filter.SecurityRequirementsError:
return http.StatusUnauthorized, err
default:
// This should never happen today, but if our upstream code changes,
// we don't want to crash the server, so handle the unexpected error.
return http.StatusInternalServerError, fmt.Errorf("error validating route: %s", err.Error())
}
}
return http.StatusOK, nil
}