Skip to content

Commit d8507bb

Browse files
committed
chore(api,docs): use uniform HTML structure across all different API doc samples
1 parent 03bdd0b commit d8507bb

File tree

2 files changed

+71
-68
lines changed

2 files changed

+71
-68
lines changed

api.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -478,21 +478,22 @@ func NewAPI(config Config, a Adapter) API {
478478
}
479479
ctx.SetHeader("Content-Type", "text/html")
480480
// Very strict CSP so we never expose any data to the outside world
481-
ctx.SetHeader("Content-Security-Policy",
482-
"default-src 'none';"+
483-
" base-uri 'none';"+
484-
" connect-src 'self';"+
485-
" form-action 'none';"+
486-
" frame-ancestors 'none';"+
487-
" sandbox allow-same-origin allow-scripts;"+
488-
" script-src https://unpkg.com/;"+
489-
" style-src 'unsafe-inline' https://unpkg.com/;"+
490-
" trusted-types 'none'")
481+
csp := []string{
482+
"default-src 'none'",
483+
"base-uri 'none'",
484+
"connect-src 'self'",
485+
"form-action 'none'",
486+
"frame-ancestors 'none'",
487+
"sandbox allow-same-origin allow-scripts",
488+
"script-src https://unpkg.com/",
489+
"style-src 'unsafe-inline' https://unpkg.com/",
490+
}
491+
ctx.SetHeader("Content-Security-Policy", strings.Join(csp, "; "))
491492
title := "Elements in HTML"
492493
if config.Info != nil && config.Info.Title != "" {
493494
title = config.Info.Title + " Reference"
494495
}
495-
ctx.BodyWriter().Write([]byte(`<!doctype html>
496+
ctx.BodyWriter().Write([]byte(`<!DOCTYPE html>
496497
<html lang="en">
497498
<head>
498499
<meta charset="utf-8" />

docs/docs/features/api-docs.md

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,23 @@ api := humachi.New(router, config)
3232
router.Get("/docs", func(w http.ResponseWriter, r *http.Request) {
3333
w.Header().Set("Content-Type", "text/html")
3434
// Very strict CSP so we never expose any data to the outside world
35-
w.Header().Set("Content-Security-Policy",
36-
"default-src 'none';"+
37-
" base-uri 'none';"+
38-
" connect-src 'self';"+
39-
" form-action 'none';"+
40-
" frame-ancestors 'none';"+
41-
" sandbox allow-same-origin allow-scripts;"+
42-
" script-src https://unpkg.com/;"+
43-
" style-src 'unsafe-inline' https://unpkg.com/;"+
44-
" trusted-types 'none'")
45-
w.Write([]byte(`<!doctype html>
35+
csp := []string{
36+
"default-src 'none'",
37+
"base-uri 'none'",
38+
"connect-src 'self'",
39+
"form-action 'none'",
40+
"frame-ancestors 'none'",
41+
"sandbox allow-same-origin allow-scripts",
42+
"script-src https://unpkg.com/",
43+
"style-src 'unsafe-inline' https://unpkg.com/",
44+
}
45+
w.Header().Set("Content-Security-Policy", strings.Join(csp, "; "))
46+
w.Write([]byte(`<!DOCTYPE html>
4647
<html lang="en">
4748
<head>
4849
<meta charset="utf-8" />
49-
<meta name="referrer" content="same-origin" />
5050
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
51+
<meta name="referrer" content="same-origin" />
5152
<title>Docs Example reference</title>
5253
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements@9.0.12/styles.min.css" crossorigin integrity="sha384-iVQBHadsD+eV0M5+ubRCEVXrXEBj+BqcuwjUwPoVJc0Pb1fmrhYSAhL+BFProHdV" />
5354
<script src="https://unpkg.com/@stoplight/elements@9.0.12/web-components.min.js" crossorigin integrity="sha384-2AG+Hh93OYHuMcQJPPLM2671WnQzoHvHXh9FwbRfwMpyMLNc3++q/nJBKeVY0JMo"></script>
@@ -79,24 +80,24 @@ api := humachi.New(router, config)
7980
router.Get("/docs", func(w http.ResponseWriter, r *http.Request) {
8081
w.Header().Set("Content-Type", "text/html")
8182
// Very strict CSP so we never expose any data to the outside world
82-
w.Header().Set("Content-Security-Policy",
83-
"default-src 'none';"+
84-
" base-uri 'none';"+
85-
" connect-src 'self';"+
86-
" form-action 'none';"+
87-
" frame-ancestors 'none';"+
88-
" sandbox allow-same-origin allow-scripts;"+
89-
" script-src 'unsafe-eval' https://unpkg.com/;"+ // TODO: Somehow drop 'unsafe-eval'
90-
" style-src 'unsafe-inline' https://unpkg.com/;"+ // TODO: Somehow drop 'unsafe-inline'
91-
" trusted-types 'none'")
92-
w.Write([]byte(`<!doctype html>
93-
<html>
83+
csp := []string{
84+
"default-src 'none'",
85+
"base-uri 'none'",
86+
"connect-src 'self'",
87+
"form-action 'none'",
88+
"frame-ancestors 'none'",
89+
"sandbox allow-same-origin allow-scripts",
90+
"script-src 'unsafe-eval' https://unpkg.com/", // TODO: Somehow drop 'unsafe-eval'
91+
"style-src 'unsafe-inline' https://unpkg.com/", // TODO: Somehow drop 'unsafe-inline'
92+
}
93+
w.Header().Set("Content-Security-Policy", strings.Join(csp, "; "))
94+
w.Write([]byte(`<!DOCTYPE html>
95+
<html lang="en">
9496
<head>
95-
<title>API Reference</title>
9697
<meta charset="utf-8" />
97-
<meta
98-
name="viewport"
99-
content="width=device-width, initial-scale=1" />
98+
<meta name="viewport" content="width=device-width, initial-scale=1" />
99+
<meta name="referrer" content="same-origin" />
100+
<title>API Reference</title>
100101
</head>
101102
<body>
102103
<script
@@ -124,37 +125,38 @@ api := humachi.New(router, config)
124125
router.Get("/docs", func(w http.ResponseWriter, r *http.Request) {
125126
w.Header().Set("Content-Type", "text/html")
126127
// Very strict CSP so we never expose any data to the outside world
127-
w.Header().Set("Content-Security-Policy",
128-
"default-src 'none';"+
129-
" base-uri 'none';"+
130-
" connect-src 'self';"+
131-
" form-action 'none';"+
132-
" frame-ancestors 'none';"+
133-
" sandbox allow-same-origin allow-scripts;"+
134-
" script-src https://unpkg.com/ 'sha256-SWB2p1nUb0MJzt5MoVlrz+PWYxv53T2z7GdKFxZm9i4=';"+
135-
" style-src https://unpkg.com/;"+
136-
" trusted-types 'none'")
128+
csp := []string{
129+
"default-src 'none'",
130+
"base-uri 'none'",
131+
"connect-src 'self'",
132+
"form-action 'none'",
133+
"frame-ancestors 'none'",
134+
"sandbox allow-same-origin allow-scripts",
135+
"script-src https://unpkg.com/ 'sha256-pyvxInx2c2C9E/dNMA9dfGa9z3Lhk9YDz1ET62LbfZs='",
136+
"style-src https://unpkg.com/",
137+
}
138+
w.Header().Set("Content-Security-Policy", strings.Join(csp, "; "))
137139
w.Write([]byte(`<!DOCTYPE html>
138140
<html lang="en">
139-
<head>
140-
<meta charset="utf-8" />
141-
<meta name="viewport" content="width=device-width, initial-scale=1" />
142-
<meta name="description" content="SwaggerUI" />
143-
<title>SwaggerUI</title>
144-
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.30.2/swagger-ui.css" crossorigin integrity="sha384-++DMKo1369T5pxDNqojF1F91bYxYiT1N7b1M15a7oCzEodfljztKlApQoH6eQSKI" />
145-
</head>
146-
<body>
147-
<div id="swagger-ui"></div>
148-
<script src="https://unpkg.com/swagger-ui-dist@5.30.2/swagger-ui-bundle.js" crossorigin integrity="sha384-bBdB196maIUakX6v2F6J0XcjddQfaENm8kASsYfqTKCZua9xlYNh1AdtL18PGr0D"></script>
149-
<script>
150-
window.onload = () => {
151-
window.ui = SwaggerUIBundle({
152-
url: '/openapi.json',
153-
dom_id: '#swagger-ui',
154-
});
155-
};
156-
</script>
157-
</body>
141+
<head>
142+
<meta charset="utf-8" />
143+
<meta name="viewport" content="width=device-width, initial-scale=1" />
144+
<meta name="referrer" content="same-origin" />
145+
<title>SwaggerUI</title>
146+
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.30.2/swagger-ui.css" crossorigin integrity="sha384-++DMKo1369T5pxDNqojF1F91bYxYiT1N7b1M15a7oCzEodfljztKlApQoH6eQSKI" />
147+
</head>
148+
<body>
149+
<div id="swagger-ui"></div>
150+
<script src="https://unpkg.com/swagger-ui-dist@5.30.2/swagger-ui-bundle.js" crossorigin integrity="sha384-bBdB196maIUakX6v2F6J0XcjddQfaENm8kASsYfqTKCZua9xlYNh1AdtL18PGr0D"></script>
151+
<script>
152+
window.onload = () => {
153+
window.ui = SwaggerUIBundle({
154+
url: '/openapi.json',
155+
dom_id: '#swagger-ui',
156+
});
157+
};
158+
</script>
159+
</body>
158160
</html>`))
159161
})
160162
```

0 commit comments

Comments
 (0)