SHA256
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
// Package router 路由配置,注册 API 路由和前端静态文件服务
|
|
package router
|
|
|
|
import (
|
|
"ashareview-server/handler"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Setup 注册所有路由
|
|
// - /api/* : 后端 API 接口
|
|
// - /assets/*, /vite.svg : 前端静态资源
|
|
// - 其余路径: SPA fallback,返回 index.html
|
|
func Setup(r *gin.Engine, dh *handler.DashboardHandler) {
|
|
// 后端 API 路由组
|
|
api := r.Group("/api")
|
|
{
|
|
api.GET("/dashboard/summary", dh.GetSummary) // 市场概览
|
|
api.GET("/dashboard/daily", dh.GetDailySummary) // 每日统计
|
|
api.GET("/dashboard/consecutive", dh.GetConsecutiveLimits) // 连板统计
|
|
api.GET("/dashboard/high100", dh.GetHigh100) // 百日新高
|
|
api.GET("/dashboard/index-kline", dh.GetIndexKline) // 指数K线
|
|
api.GET("/dashboard/stock-kline", dh.GetStockKline) // 个股K线
|
|
}
|
|
|
|
// 前端静态文件服务
|
|
staticDir := "static"
|
|
if _, err := os.Stat(staticDir); os.IsNotExist(err) {
|
|
// 当前目录不存在时,尝试可执行文件所在目录
|
|
exePath, _ := os.Executable()
|
|
staticDir = filepath.Join(filepath.Dir(exePath), "static")
|
|
}
|
|
|
|
r.Static("/assets", filepath.Join(staticDir, "assets"))
|
|
r.StaticFile("/vite.svg", filepath.Join(staticDir, "vite.svg"))
|
|
|
|
// SPA fallback: 非 API 路径全部返回 index.html,支持前端路由
|
|
r.NoRoute(func(c *gin.Context) {
|
|
path := c.Request.URL.Path
|
|
if len(path) < 4 || path[:4] != "/api" {
|
|
indexPath := filepath.Join(staticDir, "index.html")
|
|
c.File(indexPath)
|
|
return
|
|
}
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
})
|
|
}
|