-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathstatic.go
More file actions
43 lines (34 loc) · 1020 Bytes
/
static.go
File metadata and controls
43 lines (34 loc) · 1020 Bytes
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
package tio
import (
"embed"
"io/fs"
"net/http"
"strings"
"github.com/emicklei/go-restful/v3"
)
var (
//go:embed "config.default.yaml"
DefaultConfigYaml []byte
//go:embed "api/swagger_ui/*"
swagFS embed.FS
//go:embed "web/dist/*"
webFS embed.FS
)
func RouteSwagger(container *restful.Container) {
d, _ := fs.Sub(swagFS, "api/swagger_ui")
container.Handle("/docs/", http.StripPrefix("/docs/", http.FileServer(http.FS(d))))
}
func RouteWeb(container *restful.Container) {
d, _ := fs.Sub(webFS, "web/dist")
h := http.StripPrefix("/web/", http.FileServer(http.FS(d)))
container.Handle("/web/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// set cache header
if strings.HasSuffix(r.URL.Path, ".js") || strings.HasSuffix(r.URL.Path, ".css") {
w.Header().Set("Cache-Control", "public, max-age=31536000")
}
h.ServeHTTP(w, r)
}))
container.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/web", http.StatusFound)
}))
}