-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
64 lines (53 loc) · 1.17 KB
/
content.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
package main
import (
"bytes"
"embed"
"net/http"
"text/template"
"github.com/lestrrat-go/jwx/v2/jwk"
"go.uber.org/fx"
)
//go:embed swaggerui
var swaggerui embed.FS
//go:embed openapi.yaml.gt
var openapiYAML string
type SwaggerUIHandler http.Handler
type SwaggerYAMLHandler http.Handler
func provideContent() fx.Option {
return fx.Options(
fx.Provide(
func() SwaggerUIHandler {
return http.FileServer(
http.FS(swaggerui),
)
},
func(key jwk.Key) (syh SwaggerYAMLHandler, err error) {
var (
tmplt *template.Template
yaml []byte
)
tmplt, err = template.New("openapi.yaml").Parse(openapiYAML)
if err == nil {
var output bytes.Buffer
err = tmplt.Execute(
&output,
map[string]any{
"defaultKeyID": key.KeyID(),
},
)
yaml = output.Bytes()
}
if err == nil {
syh = SwaggerYAMLHandler(
http.HandlerFunc(func(response http.ResponseWriter, _ *http.Request) {
// NOTE: don't set a content type, since that seems to mess up browsers
// since there is, as yet, no official MIME type for YAML
response.Write(yaml)
}),
)
}
return
},
),
)
}