Skip to content

Commit 3d3192a

Browse files
committed
add option for edit pwa name
1 parent 02ae365 commit 3d3192a

File tree

7 files changed

+60
-2
lines changed

7 files changed

+60
-2
lines changed

README.MD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ serve_limit:
105105
# 上行速率限制 (KiB/s), 0 表示无限制
106106
upload_rate: 0
107107

108+
# 内置的仪表板
109+
dashboard:
110+
# 是否启用
111+
enable: true
112+
# PWA 的名称
113+
pwa-name: GoOpemBmclApi Dashboard
114+
108115
## 特殊要求: 重定向到 OSS
109116
oss:
110117
# 是否启用该功能

config.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ type ServeLimitConfig struct {
4343
UploadRate int `yaml:"upload_rate"`
4444
}
4545

46+
type DashboardConfig struct {
47+
Enable bool `yaml:"enable"`
48+
PwaName string `yaml:"pwa-name"`
49+
}
50+
4651
type OSSConfig struct {
4752
Enable bool `yaml:"enable"`
4853
List []*OSSItem `yaml:"list"`
@@ -73,10 +78,17 @@ type Config struct {
7378
DownloadMaxConn int `yaml:"download_max_conn"`
7479
UseGzip bool `yaml:"use_gzip"`
7580
ServeLimit ServeLimitConfig `yaml:"serve_limit"`
81+
Dashboard DashboardConfig `yaml:"dashboard"`
7682
Oss OSSConfig `yaml:"oss"`
7783
Hijack HijackConfig `yaml:"hijack_port"`
7884
}
7985

86+
func (cfg *Config) applyWebManifest(manifest map[string]any) {
87+
if cfg.Dashboard.Enable {
88+
manifest["name"] = cfg.Dashboard.PwaName
89+
}
90+
}
91+
8092
func readConfig() (config Config) {
8193
const configPath = "config.yaml"
8294

@@ -102,6 +114,11 @@ func readConfig() (config Config) {
102114
UploadRate: 1024 * 12, // 12MB
103115
},
104116

117+
Dashboard: DashboardConfig{
118+
Enable: true,
119+
PwaName: "GoOpemBmclApi Dashboard",
120+
},
121+
105122
Oss: OSSConfig{
106123
Enable: false,
107124
List: []*OSSItem{

config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ serve_limit:
1717
enable: false
1818
max_conn: 0
1919
upload_rate: 0
20+
dashboard:
21+
enable: true
22+
pwa-name: GoOpemBmclApi Dashboard
2023
oss:
2124
enable: false
2225
list:

dashboard.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
package main
2121

2222
import (
23+
"bytes"
2324
"embed"
25+
"encoding/json"
2426
"io"
2527
"io/fs"
2628
"mime"
@@ -45,13 +47,36 @@ var dsbDist = func() fs.FS {
4547
//go:embed dashboard/dist/index.html
4648
var dsbIndexHtml string
4749

50+
//go:embed dashboard/dist/manifest.webmanifest
51+
var _dsbManifest []byte
52+
var dsbManifest = func() (dsbManifest map[string]any) {
53+
err := json.Unmarshal(_dsbManifest, &dsbManifest)
54+
if err != nil {
55+
panic(err)
56+
}
57+
return
58+
}()
59+
4860
func (cr *Cluster) serveDashboard(rw http.ResponseWriter, req *http.Request, pth string) {
4961
if req.Method != http.MethodGet && req.Method != http.MethodHead {
5062
rw.Header().Set("Allow", http.MethodGet+", "+http.MethodHead)
5163
http.Error(rw, "405 Method Not Allowed", http.StatusMethodNotAllowed)
5264
return
5365
}
54-
if pth != "" {
66+
switch pth {
67+
case "":
68+
break
69+
case "manifest.webmanifest":
70+
buf, err := json.Marshal(dsbManifest)
71+
if err != nil {
72+
rw.WriteHeader(http.StatusInternalServerError)
73+
io.WriteString(rw, err.Error())
74+
return
75+
}
76+
rw.Header().Set("Content-Type", "application/manifest+json")
77+
http.ServeContent(rw, req, "manifest.webmanifest", startTime, bytes.NewReader(buf))
78+
return
79+
default:
5580
fd, err := dsbDist.Open(pth)
5681
if err == nil {
5782
defer fd.Close()

dashboard/src/config/pwa.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default VitePWA({
66
includeAssets: ['favicon.ico'],
77
manifest: {
88
name: 'GoOpemBmclApi Dashboard',
9-
short_name: 'GoOpemBmclApiDashboard',
9+
short_name: 'GOBA Dash',
1010
description: 'Go-Openbmclapi Internal Dashboard',
1111
theme_color: '#4c89fe',
1212
icons: [

handler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
274274
return
275275
}
276276
case strings.HasPrefix(rawpath, "/dashboard/"):
277+
if !config.Dashboard.Enable {
278+
http.NotFound(rw, req)
279+
return
280+
}
277281
pth := rawpath[len("/dashboard/"):]
278282
cr.serveDashboard(rw, req, pth)
279283
return

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ START:
248248

249249
config = readConfig()
250250

251+
config.applyWebManifest(dsbManifest)
252+
251253
httpcli := &http.Client{
252254
Timeout: 30 * time.Second,
253255
}

0 commit comments

Comments
 (0)