move css and js to seperate files (#280)
* move css and js to seperate files * Test passes * Add test for the new files
This commit is contained in:
+66
-55
@@ -1,12 +1,13 @@
|
|||||||
package ginSwagger
|
package ginSwagger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
htmlTemplate "html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sync"
|
"sync"
|
||||||
|
textTemplate "text/template"
|
||||||
|
|
||||||
"golang.org/x/net/webdav"
|
"golang.org/x/net/webdav"
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ type swaggerConfig struct {
|
|||||||
URL string
|
URL string
|
||||||
DocExpansion string
|
DocExpansion string
|
||||||
Title string
|
Title string
|
||||||
Oauth2RedirectURL template.JS
|
Oauth2RedirectURL htmlTemplate.JS
|
||||||
DefaultModelsExpandDepth int
|
DefaultModelsExpandDepth int
|
||||||
DeepLinking bool
|
DeepLinking bool
|
||||||
PersistAuthorization bool
|
PersistAuthorization bool
|
||||||
@@ -138,9 +139,11 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create a template with name
|
// create a template with name
|
||||||
index, _ := template.New("swagger_index.html").Parse(swaggerIndexTpl)
|
index, _ := htmlTemplate.New("swagger_index.html").Parse(swaggerIndexTpl)
|
||||||
|
js, _ := textTemplate.New("swagger_index.js").Parse(swaggerJSTpl)
|
||||||
|
css, _ := textTemplate.New("swagger_index.css").Parse(swaggerStyleTpl)
|
||||||
|
|
||||||
var matcher = regexp.MustCompile(`(.*)(index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[?|.]*`)
|
var matcher = regexp.MustCompile(`(.*)(index\.html|index\.css|swagger-initializer\.js|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[?|.]*`)
|
||||||
|
|
||||||
return func(ctx *gin.Context) {
|
return func(ctx *gin.Context) {
|
||||||
if ctx.Request.Method != http.MethodGet {
|
if ctx.Request.Method != http.MethodGet {
|
||||||
@@ -178,6 +181,10 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc
|
|||||||
switch path {
|
switch path {
|
||||||
case "index.html":
|
case "index.html":
|
||||||
_ = index.Execute(ctx.Writer, config.toSwaggerConfig())
|
_ = index.Execute(ctx.Writer, config.toSwaggerConfig())
|
||||||
|
case "index.css":
|
||||||
|
_ = css.Execute(ctx.Writer, config.toSwaggerConfig())
|
||||||
|
case "swagger-initializer.js":
|
||||||
|
_ = js.Execute(ctx.Writer, config.toSwaggerConfig())
|
||||||
case "doc.json":
|
case "doc.json":
|
||||||
doc, err := swag.ReadDoc(config.InstanceName)
|
doc, err := swag.ReadDoc(config.InstanceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -221,6 +228,59 @@ func DisablingCustomWrapHandler(config *Config, handler *webdav.Handler, envName
|
|||||||
return CustomWrapHandler(config, handler)
|
return CustomWrapHandler(config, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const swaggerStyleTpl = `
|
||||||
|
html
|
||||||
|
{
|
||||||
|
box-sizing: border-box;
|
||||||
|
overflow: -moz-scrollbars-vertical;
|
||||||
|
overflow-y: scroll;
|
||||||
|
}
|
||||||
|
*,
|
||||||
|
*:before,
|
||||||
|
*:after
|
||||||
|
{
|
||||||
|
box-sizing: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
margin:0;
|
||||||
|
background: #fafafa;
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
const swaggerJSTpl = `
|
||||||
|
window.onload = function() {
|
||||||
|
// Build a system
|
||||||
|
const ui = SwaggerUIBundle({
|
||||||
|
url: "{{.URL}}",
|
||||||
|
dom_id: '#swagger-ui',
|
||||||
|
validatorUrl: null,
|
||||||
|
oauth2RedirectUrl: {{.Oauth2RedirectURL}},
|
||||||
|
persistAuthorization: {{.PersistAuthorization}},
|
||||||
|
presets: [
|
||||||
|
SwaggerUIBundle.presets.apis,
|
||||||
|
SwaggerUIStandalonePreset
|
||||||
|
],
|
||||||
|
plugins: [
|
||||||
|
SwaggerUIBundle.plugins.DownloadUrl
|
||||||
|
],
|
||||||
|
layout: "StandaloneLayout",
|
||||||
|
docExpansion: "{{.DocExpansion}}",
|
||||||
|
deepLinking: {{.DeepLinking}},
|
||||||
|
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
|
||||||
|
})
|
||||||
|
|
||||||
|
const defaultClientId = "{{.Oauth2DefaultClientID}}";
|
||||||
|
if (defaultClientId) {
|
||||||
|
ui.initOAuth({
|
||||||
|
clientId: defaultClientId
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
window.ui = ui
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
const swaggerIndexTpl = `<!-- HTML for static distribution bundle build -->
|
const swaggerIndexTpl = `<!-- HTML for static distribution bundle build -->
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
@@ -230,25 +290,7 @@ const swaggerIndexTpl = `<!-- HTML for static distribution bundle build -->
|
|||||||
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
|
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
|
||||||
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
|
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
|
||||||
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
|
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
|
||||||
<style>
|
<link rel="stylesheet" type="text/css" href="index.css" />
|
||||||
html
|
|
||||||
{
|
|
||||||
box-sizing: border-box;
|
|
||||||
overflow: -moz-scrollbars-vertical;
|
|
||||||
overflow-y: scroll;
|
|
||||||
}
|
|
||||||
*,
|
|
||||||
*:before,
|
|
||||||
*:after
|
|
||||||
{
|
|
||||||
box-sizing: inherit;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
margin:0;
|
|
||||||
background: #fafafa;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
@@ -291,38 +333,7 @@ const swaggerIndexTpl = `<!-- HTML for static distribution bundle build -->
|
|||||||
|
|
||||||
<script src="./swagger-ui-bundle.js"> </script>
|
<script src="./swagger-ui-bundle.js"> </script>
|
||||||
<script src="./swagger-ui-standalone-preset.js"> </script>
|
<script src="./swagger-ui-standalone-preset.js"> </script>
|
||||||
<script>
|
<script src="./swagger-initializer.js"> </script>
|
||||||
window.onload = function() {
|
|
||||||
// Build a system
|
|
||||||
const ui = SwaggerUIBundle({
|
|
||||||
url: "{{.URL}}",
|
|
||||||
dom_id: '#swagger-ui',
|
|
||||||
validatorUrl: null,
|
|
||||||
oauth2RedirectUrl: {{.Oauth2RedirectURL}},
|
|
||||||
persistAuthorization: {{.PersistAuthorization}},
|
|
||||||
presets: [
|
|
||||||
SwaggerUIBundle.presets.apis,
|
|
||||||
SwaggerUIStandalonePreset
|
|
||||||
],
|
|
||||||
plugins: [
|
|
||||||
SwaggerUIBundle.plugins.DownloadUrl
|
|
||||||
],
|
|
||||||
layout: "StandaloneLayout",
|
|
||||||
docExpansion: "{{.DocExpansion}}",
|
|
||||||
deepLinking: {{.DeepLinking}},
|
|
||||||
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
|
|
||||||
})
|
|
||||||
|
|
||||||
const defaultClientId = "{{.Oauth2DefaultClientID}}";
|
|
||||||
if (defaultClientId) {
|
|
||||||
ui.initOAuth({
|
|
||||||
clientId: defaultClientId
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
window.ui = ui
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -67,6 +67,14 @@ func TestWrapCustomHandler(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusOK, w5.Code)
|
assert.Equal(t, http.StatusOK, w5.Code)
|
||||||
assert.Equal(t, w5.Header()["Content-Type"][0], "application/javascript")
|
assert.Equal(t, w5.Header()["Content-Type"][0], "application/javascript")
|
||||||
|
|
||||||
|
w6 := performRequest(http.MethodGet, "/index.css", router)
|
||||||
|
assert.Equal(t, http.StatusOK, w6.Code)
|
||||||
|
assert.Equal(t, w6.Header()["Content-Type"][0], "text/css; charset=utf-8")
|
||||||
|
|
||||||
|
w7 := performRequest(http.MethodGet, "/swagger-initializer.js", router)
|
||||||
|
assert.Equal(t, http.StatusOK, w7.Code)
|
||||||
|
assert.Equal(t, w7.Header()["Content-Type"][0], "application/javascript")
|
||||||
|
|
||||||
assert.Equal(t, http.StatusNotFound, performRequest(http.MethodGet, "/notfound", router).Code)
|
assert.Equal(t, http.StatusNotFound, performRequest(http.MethodGet, "/notfound", router).Code)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPost, "/index.html", router).Code)
|
assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPost, "/index.html", router).Code)
|
||||||
|
|||||||
Reference in New Issue
Block a user