-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassets.go
59 lines (52 loc) · 1.32 KB
/
assets.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
package web
import (
"io/fs"
"os"
"path/filepath"
)
var clientAssetsFS fs.FS
var swaggerAssetsFS fs.FS
// SetClientAssetsFS sets the global web client assets filesystem.
func SetClientAssetsFS(f fs.FS) {
clientAssetsFS = f
}
// SetSwaggerUIAssetsFS sets the filesystem containing the swagger UI assets.
func SetSwaggerUIAssetsFS(f fs.FS) {
swaggerAssetsFS = f
}
// GetClientAssetsFS returns web client assets.
func GetClientAssetsFS() fs.FS {
if clientAssetsFS != nil {
return clientAssetsFS
}
// If client assets were not set, we are in the development environment.
path := filepath.Join("client", "dist")
_, err := os.Stat(path)
if os.IsNotExist(err) {
panic(path + " assets are not available")
}
SetClientAssetsFS(os.DirFS(path))
return clientAssetsFS
}
// GetSwaggerUIAssetsFS returns web assets for swagger-ui.
func GetSwaggerUIAssetsFS() (fs.FS, error) {
if swaggerAssetsFS != nil {
return swaggerAssetsFS, nil
}
// If client assets were not set, we are in the development environment.
path := filepath.Join("swagger-ui")
_, err := os.Stat(path)
if err != nil {
return nil, err
}
SetSwaggerUIAssetsFS(os.DirFS(path))
return swaggerAssetsFS, nil
}
// MustSubFS returns fs by subpath.
func MustSubFS(orig fs.FS, path string) fs.FS {
sub, err := fs.Sub(orig, path)
if err != nil {
panic(err)
}
return sub
}