这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,31 +477,38 @@ func NewAPI(config Config, a Adapter) API {
openAPIPath = path.Join(prefix, openAPIPath)
}
ctx.SetHeader("Content-Type", "text/html")
// Very strict CSP so we never expose any data to the outside world
csp := []string{
"default-src 'none'",
"base-uri 'none'",
"connect-src 'self'",
"form-action 'none'",
"frame-ancestors 'none'",
"sandbox allow-same-origin allow-scripts",
"script-src https://unpkg.com/",
"style-src 'unsafe-inline' https://unpkg.com/",
}
ctx.SetHeader("Content-Security-Policy", strings.Join(csp, "; "))
title := "Elements in HTML"
if config.Info != nil && config.Info.Title != "" {
title = config.Info.Title + " Reference"
}
ctx.BodyWriter().Write([]byte(`<!doctype html>
ctx.BodyWriter().Write([]byte(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="referrer" content="same-origin" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>` + title + `</title>
<!-- Embed elements Elements via Web Component -->
<link href="https://unpkg.com/@stoplight/elements@9.0.0/styles.min.css" rel="stylesheet" />
<script src="https://unpkg.com/@stoplight/elements@9.0.0/web-components.min.js" integrity="sha256-Tqvw1qE2abI+G6dPQBc5zbeHqfVwGoamETU3/TSpUw4="
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements@9.0.12/styles.min.css" crossorigin integrity="sha384-iVQBHadsD+eV0M5+ubRCEVXrXEBj+BqcuwjUwPoVJc0Pb1fmrhYSAhL+BFProHdV" />
<script src="https://unpkg.com/@stoplight/elements@9.0.12/web-components.min.js" crossorigin integrity="sha384-2AG+Hh93OYHuMcQJPPLM2671WnQzoHvHXh9FwbRfwMpyMLNc3++q/nJBKeVY0JMo"></script>
</head>
<body style="height: 100vh;">

<elements-api
apiDescriptionUrl="` + openAPIPath + `.yaml"
router="hash"
layout="sidebar"
tryItCredentialsPolicy="same-origin"
/>

</body>
</html>`))
})
Expand Down
99 changes: 65 additions & 34 deletions docs/docs/features/api-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,32 @@ api := humachi.New(router, config)

router.Get("/docs", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<!doctype html>
// Very strict CSP so we never expose any data to the outside world
csp := []string{
"default-src 'none'",
"base-uri 'none'",
"connect-src 'self'",
"form-action 'none'",
"frame-ancestors 'none'",
"sandbox allow-same-origin allow-scripts",
"script-src https://unpkg.com/",
"style-src 'unsafe-inline' https://unpkg.com/",
}
w.Header().Set("Content-Security-Policy", strings.Join(csp, "; "))
w.Write([]byte(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="referrer" content="same-origin" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="referrer" content="same-origin" />
<title>Docs Example reference</title>
<!-- Embed elements Elements via Web Component -->
<link href="https://unpkg.com/@stoplight/elements@8.0.0/styles.min.css" rel="stylesheet" />
<script src="https://unpkg.com/@stoplight/elements@8.0.0/web-components.min.js"
integrity="sha256-yIhuSFMJJ6mp2XTUAb4SiSYneP3Qav8Uu+7NBhGJW5A="
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements@9.0.12/styles.min.css" crossorigin integrity="sha384-iVQBHadsD+eV0M5+ubRCEVXrXEBj+BqcuwjUwPoVJc0Pb1fmrhYSAhL+BFProHdV" />
<script src="https://unpkg.com/@stoplight/elements@9.0.12/web-components.min.js" crossorigin integrity="sha384-2AG+Hh93OYHuMcQJPPLM2671WnQzoHvHXh9FwbRfwMpyMLNc3++q/nJBKeVY0JMo"></script>
</head>
<body style="height: 100vh;">
<elements-api
apiDescriptionUrl="/openapi.yaml"
router="hash"
layout="stacked"
tryItCredentialsPolicy="same-origin"
/>
</body>
Expand All @@ -71,20 +79,31 @@ api := humachi.New(router, config)

router.Get("/docs", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<!doctype html>
<html>
// Very strict CSP so we never expose any data to the outside world
csp := []string{
"default-src 'none'",
"base-uri 'none'",
"connect-src 'self'",
"form-action 'none'",
"frame-ancestors 'none'",
"sandbox allow-same-origin allow-scripts",
"script-src 'unsafe-eval' https://unpkg.com/", // TODO: Somehow drop 'unsafe-eval'
"style-src 'unsafe-inline' https://unpkg.com/", // TODO: Somehow drop 'unsafe-inline'
}
w.Header().Set("Content-Security-Policy", strings.Join(csp, "; "))
w.Write([]byte(`<!DOCTYPE html>
<html lang="en">
<head>
<title>API Reference</title>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="referrer" content="same-origin" />
<title>API Reference</title>
</head>
<body>
<script
id="api-reference"
data-url="/openapi.json"></script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
<script src="https://unpkg.com/@scalar/api-reference@1.39.3/dist/browser/standalone.js" crossorigin integrity="sha384-76/gvOpu0/XSY2z9BOX4MhHQJACTk0S2GW1Cwh9gRMhcf3sf7mYqKbmMA1PDl3mL"></script>
</body>
</html>`))
})
Expand All @@ -105,27 +124,39 @@ api := humachi.New(router, config)

router.Get("/docs", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
// Very strict CSP so we never expose any data to the outside world
csp := []string{
"default-src 'none'",
"base-uri 'none'",
"connect-src 'self'",
"form-action 'none'",
"frame-ancestors 'none'",
"sandbox allow-same-origin allow-scripts",
"script-src https://unpkg.com/ 'sha256-pyvxInx2c2C9E/dNMA9dfGa9z3Lhk9YDz1ET62LbfZs='",
"style-src https://unpkg.com/",
}
w.Header().Set("Content-Security-Policy", strings.Join(csp, "; "))
w.Write([]byte(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI" />
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '/openapi.json',
dom_id: '#swagger-ui',
});
};
</script>
</body>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="referrer" content="same-origin" />
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.30.2/swagger-ui.css" crossorigin integrity="sha384-++DMKo1369T5pxDNqojF1F91bYxYiT1N7b1M15a7oCzEodfljztKlApQoH6eQSKI" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.30.2/swagger-ui-bundle.js" crossorigin integrity="sha384-bBdB196maIUakX6v2F6J0XcjddQfaENm8kASsYfqTKCZua9xlYNh1AdtL18PGr0D"></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '/openapi.json',
dom_id: '#swagger-ui',
});
};
</script>
</body>
</html>`))
})
```
Expand Down
Loading