[#15] Add docs

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-07-08 13:34:34 +03:00 committed by Alex Vanin
parent 592be967f6
commit 5bee10d096
4 changed files with 111 additions and 1 deletions

View file

@ -53,6 +53,8 @@ const (
// ContextKeyRequestID is the ContextKey for RequestID.
ContextKeyRequestID ContextKey = "requestID"
docPrefix = "/doc"
)
// New creates a new API using specified logger, connection pool and other parameters.
@ -99,7 +101,7 @@ func (a *API) Configure(api *operations.NeofsRestGwAPI) http.Handler {
api.ServerShutdown = func() {}
return a.setupGlobalMiddleware(api.Serve(setupMiddlewares))
return a.setupGlobalMiddleware(a.docMiddleware(api.Serve(setupMiddlewares)))
}
// The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
@ -123,6 +125,18 @@ func (a *API) setupGlobalMiddleware(handler http.Handler) http.Handler {
})
}
func (a *API) docMiddleware(handler http.Handler) http.Handler {
fh := http.StripPrefix(docPrefix, http.FileServer(http.Dir("static/doc")))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, docPrefix) {
fh.ServeHTTP(w, r)
} else {
handler.ServeHTTP(w, r)
}
})
}
func (a *API) logAndGetErrorResponse(msg string, err error, fields ...zap.Field) *models.ErrorResponse {
fields = append(fields, zap.Error(err))
a.log.Error(msg, fields...)