mirror of
https://git.fightbot.fun/hxuanyu/BingPaper.git
synced 2026-02-15 18:59:33 +08:00
支持内嵌前端页面,新增配置项 web.path
This commit is contained in:
@@ -11,6 +11,7 @@
|
|||||||
- **数据库支持**:支持 SQLite, MySQL, PostgreSQL。
|
- **数据库支持**:支持 SQLite, MySQL, PostgreSQL。
|
||||||
- **公共 API**:提供今日图片、随机图片、指定日期图片的纯图及元数据接口。
|
- **公共 API**:提供今日图片、随机图片、指定日期图片的纯图及元数据接口。
|
||||||
- **管理后台**:内置极简管理后台,支持 Token 管理、任务控制、配置查看。
|
- **管理后台**:内置极简管理后台,支持 Token 管理、任务控制、配置查看。
|
||||||
|
- **单文件分发**:支持将前端页面嵌入二进制文件,实现无依赖运行。
|
||||||
- **行为模式**:支持 `local`(服务转发)和 `redirect`(302 跳转至公网 URL)两种模式。
|
- **行为模式**:支持 `local`(服务转发)和 `redirect`(302 跳转至公网 URL)两种模式。
|
||||||
|
|
||||||
## 快速启动
|
## 快速启动
|
||||||
@@ -83,7 +84,7 @@ go run .
|
|||||||
.\scripts\build.ps1 [version]
|
.\scripts\build.ps1 [version]
|
||||||
```
|
```
|
||||||
|
|
||||||
编译后的打包文件将生成在 `output` 目录下。
|
编译后的打包文件将生成在 `output` 目录下。二进制文件已内置默认前端页面,即使不带 `web` 目录也能运行。如果需要自定义页面,可在配置中指定 `web.path`。
|
||||||
|
|
||||||
### 发布流程
|
### 发布流程
|
||||||
本项目集成了 GitHub Actions,可通过以下步骤发布新版本:
|
本项目集成了 GitHub Actions,可通过以下步骤发布新版本:
|
||||||
|
|||||||
@@ -45,3 +45,6 @@ token:
|
|||||||
|
|
||||||
feature:
|
feature:
|
||||||
write_daily_files: true
|
write_daily_files: true
|
||||||
|
|
||||||
|
web:
|
||||||
|
path: web
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ type Config struct {
|
|||||||
Admin AdminConfig `mapstructure:"admin"`
|
Admin AdminConfig `mapstructure:"admin"`
|
||||||
Token TokenConfig `mapstructure:"token"`
|
Token TokenConfig `mapstructure:"token"`
|
||||||
Feature FeatureConfig `mapstructure:"feature"`
|
Feature FeatureConfig `mapstructure:"feature"`
|
||||||
|
Web WebConfig `mapstructure:"web"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
@@ -89,6 +90,10 @@ type FeatureConfig struct {
|
|||||||
WriteDailyFiles bool `mapstructure:"write_daily_files"`
|
WriteDailyFiles bool `mapstructure:"write_daily_files"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WebConfig struct {
|
||||||
|
Path string `mapstructure:"path"`
|
||||||
|
}
|
||||||
|
|
||||||
// Bing 默认配置 (内置)
|
// Bing 默认配置 (内置)
|
||||||
const (
|
const (
|
||||||
BingMkt = "zh-CN"
|
BingMkt = "zh-CN"
|
||||||
@@ -125,6 +130,7 @@ func Init(configPath string) error {
|
|||||||
v.SetDefault("storage.local.root", "data/picture")
|
v.SetDefault("storage.local.root", "data/picture")
|
||||||
v.SetDefault("token.default_ttl", "168h")
|
v.SetDefault("token.default_ttl", "168h")
|
||||||
v.SetDefault("feature.write_daily_files", true)
|
v.SetDefault("feature.write_daily_files", true)
|
||||||
|
v.SetDefault("web.path", "web")
|
||||||
v.SetDefault("admin.password_bcrypt", "$2a$10$fYHPeWHmwObephJvtlyH1O8DIgaLk5TINbi9BOezo2M8cSjmJchka") // 默认密码: admin123
|
v.SetDefault("admin.password_bcrypt", "$2a$10$fYHPeWHmwObephJvtlyH1O8DIgaLk5TINbi9BOezo2M8cSjmJchka") // 默认密码: admin123
|
||||||
|
|
||||||
if err := v.ReadInConfig(); err != nil {
|
if err := v.ReadInConfig(); err != nil {
|
||||||
@@ -176,6 +182,7 @@ func SaveConfig(cfg *Config) error {
|
|||||||
v.Set("admin", cfg.Admin)
|
v.Set("admin", cfg.Admin)
|
||||||
v.Set("token", cfg.Token)
|
v.Set("token", cfg.Token)
|
||||||
v.Set("feature", cfg.Feature)
|
v.Set("feature", cfg.Feature)
|
||||||
|
v.Set("web", cfg.Web)
|
||||||
return v.WriteConfig()
|
return v.WriteConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
_ "BingPaper/docs"
|
_ "BingPaper/docs"
|
||||||
|
"BingPaper/internal/config"
|
||||||
"BingPaper/internal/http/handlers"
|
"BingPaper/internal/http/handlers"
|
||||||
"BingPaper/internal/http/middleware"
|
"BingPaper/internal/http/middleware"
|
||||||
|
"BingPaper/web"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
swaggerFiles "github.com/swaggo/files"
|
swaggerFiles "github.com/swaggo/files"
|
||||||
@@ -18,9 +24,28 @@ func SetupRouter() *gin.Engine {
|
|||||||
|
|
||||||
// 静态文件
|
// 静态文件
|
||||||
r.Static("/static", "./static")
|
r.Static("/static", "./static")
|
||||||
r.StaticFile("/", "./web/index.html")
|
|
||||||
r.StaticFile("/admin", "./web/index.html")
|
webPath := config.GetConfig().Web.Path
|
||||||
r.StaticFile("/login", "./web/index.html")
|
indexPath := filepath.Join(webPath, "index.html")
|
||||||
|
|
||||||
|
serveIndex := func(c *gin.Context) {
|
||||||
|
// 1. 优先尝试从配置的路径读取
|
||||||
|
if _, err := os.Stat(indexPath); err == nil {
|
||||||
|
c.File(indexPath)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 2. 如果外部文件不存在,则使用内置嵌入的文件
|
||||||
|
data, err := web.FS.ReadFile("index.html")
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "web files not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Data(http.StatusOK, "text/html; charset=utf-8", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.GET("/", serveIndex)
|
||||||
|
r.GET("/admin", serveIndex)
|
||||||
|
r.GET("/login", serveIndex)
|
||||||
|
|
||||||
api := r.Group("/api/v1")
|
api := r.Group("/api/v1")
|
||||||
{
|
{
|
||||||
|
|||||||
6
web/web.go
Normal file
6
web/web.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import "embed"
|
||||||
|
|
||||||
|
//go:embed index.html
|
||||||
|
var FS embed.FS
|
||||||
Reference in New Issue
Block a user