Files
AFI/afi_embed_dev.go

60 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//go:build dev
package main
import (
"encoding/base64"
"html/template"
"os"
"path/filepath"
"strings"
)
var tmpl *template.Template
func init() {
// 开发模式下从当前目录读取 UI 文件
uiDir := "ui"
// 如果当前目录没有 ui尝试从可执行文件目录查找
if _, err := os.Stat(uiDir); os.IsNotExist(err) {
execPath, err := os.Executable()
if err != nil {
panic(err)
}
uiDir = filepath.Join(filepath.Dir(execPath), "ui")
}
// 读取 UI 文件
styleCSS, err := os.ReadFile(filepath.Join(uiDir, "style.css"))
if err != nil {
panic(err)
}
scriptJS, err := os.ReadFile(filepath.Join(uiDir, "script.js"))
if err != nil {
panic(err)
}
faviconSVG, err := os.ReadFile(filepath.Join(uiDir, "favicon.svg"))
if err != nil {
panic(err)
}
uiTmpl, err := os.ReadFile(filepath.Join(uiDir, "ui.html"))
if err != nil {
panic(err)
}
// 组装模板
t := strings.Replace(string(uiTmpl), "css_will_be_here", string(styleCSS), 1)
t = strings.Replace(t, "js_will_be_here", string(scriptJS), 1)
t = strings.Replace(t, "favicon_will_be_here", base64.StdEncoding.EncodeToString(faviconSVG), 2)
var parseErr error
tmpl, parseErr = template.New("").Parse(t)
if parseErr != nil {
panic(parseErr)
}
}