Repository navigation
Expand file tree
/
Copy pathembed.go
More file actions
147 lines (135 loc) · 4.19 KB
/
Copy pathembed.go
File metadata and controls
147 lines (135 loc) · 4.19 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Package webui 提供内嵌前端静态资源服务与 SPA 路由 fallback。
//
// 生产产物写入 internal/webui/dist/ 并由 embed.FS 内嵌,随单个 Go 二进制分发。
package webui
import (
"embed"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"path"
"strings"
)
//go:embed dist/*
var embedded embed.FS
// dist 是内嵌的 dist 子文件系统。
var dist fs.FS
func init() {
dist, _ = fs.Sub(embedded, "dist")
}
// Embedded 返回内嵌的 dist 文件系统。
func Embedded() (fs.FS, error) {
if dist == nil {
return nil, errors.New("前端资源未构建")
}
return dist, nil
}
// mimeByExt 返回常见扩展名对应的 Content-Type。
func mimeByExt(name string) string {
switch {
case strings.HasSuffix(name, ".html"):
return "text/html; charset=utf-8"
case strings.HasSuffix(name, ".js"):
return "text/javascript; charset=utf-8"
case strings.HasSuffix(name, ".css"):
return "text/css; charset=utf-8"
case strings.HasSuffix(name, ".json"):
return "application/json"
case strings.HasSuffix(name, ".svg"):
return "image/svg+xml"
case strings.HasSuffix(name, ".png"):
return "image/png"
case strings.HasSuffix(name, ".jpg"), strings.HasSuffix(name, ".jpeg"):
return "image/jpeg"
case strings.HasSuffix(name, ".gif"):
return "image/gif"
case strings.HasSuffix(name, ".ico"):
return "image/x-icon"
case strings.HasSuffix(name, ".woff2"):
return "font/woff2"
case strings.HasSuffix(name, ".woff"):
return "font/woff"
case strings.HasSuffix(name, ".txt"):
return "text/plain; charset=utf-8"
default:
return "application/octet-stream"
}
}
// hashedAsset 判断路径是否含哈希文件名(如 app-abc123.js)。
func hashedAsset(name string) bool {
base := path.Base(name)
dot := strings.Index(base, ".")
if dot < 0 {
return false
}
return len(base[:dot]) > 1 && strings.Contains(base[:dot], "-")
}
// Handler 返回服务内嵌静态资源并支持 SPA fallback 的 http.Handler。
//
// 只允许 GET/HEAD;路径先 path.Clean 并拒绝 "..";存在文件就按扩展名服务,
// 不存在且路径不含文件扩展名时返回 index.html;生产 dist 未生成时返回
// 清晰的 503 中文纯文本,而不是 panic。
func Handler(fsys fs.FS) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.Header().Set("Allow", "GET, HEAD")
http.Error(w, "方法不允许", http.StatusMethodNotAllowed)
return
}
clean := path.Clean("/" + r.URL.Path)
if strings.Contains(clean, "..") {
http.Error(w, "路径无效", http.StatusBadRequest)
return
}
name := strings.TrimPrefix(clean, "/")
if name == "" {
name = "index.html"
}
// 检查文件是否存在
file, err := fsys.Open(name)
if err == nil {
defer file.Close()
if info, statErr := file.Stat(); statErr == nil && !info.IsDir() {
if strings.HasSuffix(name, ".html") || name == "index.html" {
w.Header().Set("Cache-Control", "no-cache")
} else if hashedAsset(name) {
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
}
w.Header().Set("Content-Type", mimeByExt(name))
// 显式写状态,避免继承外层(如 gin NoRoute)预设的状态码
w.WriteHeader(http.StatusOK)
if r.Method == http.MethodHead {
return
}
_, _ = io.Copy(w, file)
return
}
// 目录:落到 SPA fallback
}
// 路径含文件扩展名 → 404(不 fallback)
if path.Ext(clean) != "" && clean != "/" && !strings.HasSuffix(clean, "/") {
http.NotFound(w, r)
return
}
// SPA fallback:返回 index.html(不存在时 503 中文提示)
index, err := fsys.Open("index.html")
if err != nil {
msg := "管理界面资源未构建,请先运行 npm run build 后重新编译服务"
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = fmt.Fprintln(w, msg)
return
}
defer index.Close()
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// 显式写状态,避免继承外层(如 gin NoRoute)预设的状态码
w.WriteHeader(http.StatusOK)
if r.Method == http.MethodHead {
return
}
_, _ = io.Copy(w, index)
})
}