diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index fe87d4a..5020aed 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -54,6 +54,16 @@ jobs: run: | make build-release + # 验证 index.html 模板代码 + - name: 验证 index.html 模板代码 + run: | + if grep -q "{{.CustomJS}}" web/dist/index.html && grep -q "{{.CustomCSS}}" web/dist/index.html; then + echo "✓ 自定义模板代码已成功添加到 index.html" + else + echo "✗ 自定义模板代码未找到,重新执行 patch-html" + go run cmd/patch-html/main.go + fi + # 列出编译产物 - name: 列出编译产物 run: | diff --git a/Makefile b/Makefile index 160a9b3..faa86c1 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ GOFLAGS=CGO_ENABLED=0 # 构建前端 build-web: cd web && yarn && yarn build + @go run cmd/patch-html/main.go # 构建服务端(开发) build-server: diff --git a/cmd/patch-html/main.go b/cmd/patch-html/main.go new file mode 100644 index 0000000..45420a4 --- /dev/null +++ b/cmd/patch-html/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "os" + "strings" +) + +func main() { + // 读取 web/dist/index.html + htmlPath := "web/dist/index.html" + content, err := os.ReadFile(htmlPath) + if err != nil { + fmt.Fprintf(os.Stderr, "读取文件失败: %v\n", err) + os.Exit(1) + } + + html := string(content) + + // 检查是否已经包含模板代码(避免重复插入) + if strings.Contains(html, "{{.CustomJS}}") { + fmt.Println("模板代码已存在,跳过插入") + return + } + + // 在 前插入模板代码 + customTemplate := ` + +` + + // 替换 + newHTML := strings.Replace(html, "", customTemplate+"", 1) + + // 写回文件 + err = os.WriteFile(htmlPath, []byte(newHTML), 0644) + if err != nil { + fmt.Fprintf(os.Stderr, "写入文件失败: %v\n", err) + os.Exit(1) + } + + fmt.Println("✓ 成功添加自定义模板到 index.html") +} diff --git a/internal/app.go b/internal/app.go index 314099a..5c1a870 100644 --- a/internal/app.go +++ b/internal/app.go @@ -3,11 +3,10 @@ package internal import ( "bytes" "context" - "encoding/json" - "html/template" "log" "net/http" "strings" + "text/template" "time" "github.com/dushixiang/pika/internal/config" @@ -110,12 +109,7 @@ func setupApi(app *orz.App, components *AppComponents) { e.Use(middleware.Recover()) e.Use(ErrorHandler(logger)) - indexTemplate, err := template.New("index").Funcs(template.FuncMap{ - "json": func(v interface{}) template.JS { - b, _ := json.Marshal(v) - return template.JS(b) - }, - }).Parse(web.IndexHtml()) + indexTemplate, err := template.New("index").Parse(web.IndexHtml()) if err != nil { logger.Fatal("failed to parse index.html", zap.Error(err)) } diff --git a/web/index.html b/web/index.html index 8b1b620..3ec002d 100644 --- a/web/index.html +++ b/web/index.html @@ -6,12 +6,19 @@ {{.SystemNameZh}} | {{.SystemNameEn}} - + -
- +
+ \ No newline at end of file diff --git a/web/src/init.ts b/web/src/init.ts deleted file mode 100644 index 656568e..0000000 --- a/web/src/init.ts +++ /dev/null @@ -1,52 +0,0 @@ - -// Default configuration -const defaultConfig = { - SystemNameZh: "Pika", - SystemNameEn: "Monitor", - ICPCode: "", - DefaultView: "grid", -}; - -// Initialize SystemConfig from injected JSON -try { - const el = document.getElementById('app-config'); - if (el && el.textContent) { - const config = JSON.parse(el.textContent); - - // Merge with default config and expose to window - window.SystemConfig = { - SystemNameZh: config.systemNameZh || defaultConfig.SystemNameZh, - SystemNameEn: config.systemNameEn || defaultConfig.SystemNameEn, - ICPCode: config.icpCode || defaultConfig.ICPCode, - DefaultView: config.defaultView || defaultConfig.DefaultView, - }; - - // Inject Custom CSS - if (config.customCSS) { - const style = document.createElement('style'); - style.innerHTML = config.customCSS; - document.head.appendChild(style); - } - - // Inject Custom JS - if (config.customJS) { - const injectJS = () => { - const script = document.createElement('script'); - script.innerHTML = config.customJS; - document.body.appendChild(script); - }; - - if (document.body) { - injectJS(); - } else { - window.addEventListener('DOMContentLoaded', injectJS); - } - } - } else { - // Fallback if no config element found - window.SystemConfig = defaultConfig; - } -} catch (e) { - console.error('Failed to parse app config:', e); - window.SystemConfig = defaultConfig; -} diff --git a/web/src/main.tsx b/web/src/main.tsx index c172597..38180b9 100644 --- a/web/src/main.tsx +++ b/web/src/main.tsx @@ -1,7 +1,6 @@ -import './init' -import { StrictMode } from 'react' -import { createRoot } from 'react-dom/client' -import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import {StrictMode} from 'react' +import {createRoot} from 'react-dom/client' +import {QueryClient, QueryClientProvider} from '@tanstack/react-query' import './index.css' import App from './App.tsx' @@ -17,7 +16,7 @@ const queryClient = new QueryClient({ createRoot(document.getElementById('root')!).render( - + , )