-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
39 lines (31 loc) · 996 Bytes
/
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
package v1
import (
"encoding/json"
"net/http"
"github.com/cloudproud/specs/http/kit/problem"
"github.com/go-chi/chi/v5"
)
//go:generate oapi-codegen -package v1 -o resources_gen.go -generate types,chi-server resources.yaml
// Use set's up the v1 OpenAPI endpoints server handler within the given router.
func Use(router chi.Router, server ServerInterface) {
HandlerWithOptions(server, ChiServerOptions{
BaseRouter: router,
})
}
// WriteProblem writes a JSON v1 problem message to the given response writer.
// The problem status, description, and other properties are unwrapped from the
// given error.
func WriteProblem(w http.ResponseWriter, err error) {
title, description := problem.GetDescription(err)
status := problem.GetStatus(err)
result := Problem{
Title: title,
Detail: description,
}
if result.Title == "" {
result.Title = http.StatusText(status)
}
bb, _ := json.Marshal(result) //nolint:errcheck
w.WriteHeader(status)
w.Write(bb) //nolint:errcheck
}