-
Notifications
You must be signed in to change notification settings - Fork 0
/
swagger.go
74 lines (61 loc) · 1.53 KB
/
swagger.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
65
66
67
68
69
70
71
72
73
74
package GoZeroSwaggerUI
import (
"html/template"
"net/http"
"os"
"path/filepath"
swaggerFiles "github.com/swaggo/files"
)
type Config struct {
URL string
}
var (
config Config
)
func FromJson(jsonFilePath string) error {
jsonFile, err := os.ReadFile(jsonFilePath)
if err != nil {
return err
}
err = swaggerFiles.WriteFile(filepath.Base(jsonFilePath), jsonFile, os.ModePerm)
if err != nil {
return err
}
config.URL = filepath.Base(jsonFilePath)
return nil
}
func FileSystem() http.FileSystem {
swaggerInitializerJsFile, err := swaggerFiles.FS.OpenFile(swaggerFiles.CTX, "swagger-initializer.js", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
return swaggerFiles.HTTP
}
initializerJS, _ := template.New("swagger-initializer").Parse(swaggerInitializerJSTpl)
err = initializerJS.Execute(swaggerInitializerJsFile, config)
if err != nil {
return swaggerFiles.HTTP
}
err = swaggerInitializerJsFile.Close()
if err != nil {
return swaggerFiles.HTTP
}
return swaggerFiles.HTTP
}
const swaggerInitializerJSTpl = `window.onload = function() {
//<editor-fold desc="Changeable Configuration Block">
// the following lines will be replaced by docker/configurator, when it runs in a docker-container
window.ui = SwaggerUIBundle({
url: "{{.URL}}",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});
//</editor-fold>
};
`