add static web

This commit is contained in:
xugo
2025-01-07 00:22:57 +08:00
parent cd73be5bf2
commit 5d69fa6d72
7 changed files with 40 additions and 21 deletions

View File

@@ -94,3 +94,17 @@ ZLM使用文档 [https://github.com/ZLMediaKit/ZLMediaKit](https://github.com/ZL
## GoWVP, GB/T28181 交流群
![](./wechat.jpg)
## QA
> 怎么没有前端资源? 如何加载网页呢?
前端资源打包后放到项目根目录,重命名为 `www` 即可正常加载。
> 有没有代码相关的学习资料?
[GoWVP 全栈开发日记[1]:从 0 到实现 GB/T 28181 协议的完整实践](https://juejin.cn/post/7456722441395568651)
[GoWVP 全栈开发日记[2]:搭建服务端,解决跨域,接口联调](https://juejin.cn/post/7456796962120417314)
开发中...

View File

@@ -36,6 +36,10 @@ func getBuildRelease() bool {
func main() {
flag.Parse()
bin, _ := os.Executable()
if err := os.Chdir(filepath.Dir(bin)); err != nil {
slog.Error("change dir error")
}
// 初始化配置
var bc conf.Bootstrap
filedir, _ := abs(*configDir)
@@ -48,7 +52,7 @@ func main() {
bc.BuildVersion = buildVersion
// 初始化日志
logDir := filepath.Join(system.GetCWD(), bc.Log.Dir)
logDir := filepath.Join(system.Getwd(), bc.Log.Dir)
log, clean := logger.SetupSlog(logger.Config{
Dir: logDir, // 日志地址
Debug: bc.Debug, // 服务级别Debug/Release
@@ -67,11 +71,6 @@ func main() {
}))
}
bin, _ := os.Executable()
if err := os.Chdir(filepath.Dir(bin)); err != nil {
slog.Error("change dir error")
}
handler, cleanUp, err := wireApp(&bc, log)
if err != nil {
slog.Error("程序构建失败", "err", err)

View File

@@ -1,6 +1,6 @@
[Server]
[Server.HTTP]
Port = 18081
Port = 8080
JwtSecret = ""
Timeout = "60s"

4
go.mod
View File

@@ -3,10 +3,11 @@ module github.com/gowvp/gb28181
go 1.23.4
require (
github.com/gin-contrib/gzip v1.1.0
github.com/gin-gonic/gin v1.10.0
github.com/glebarez/sqlite v1.11.0
github.com/google/wire v0.6.0
github.com/ixugo/goweb v1.0.9
github.com/ixugo/goweb v1.0.10
github.com/pelletier/go-toml/v2 v2.2.3
github.com/shirou/gopsutil v3.21.11+incompatible
gorm.io/driver/postgres v1.5.11
@@ -46,7 +47,6 @@ require (
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/stretchr/testify v1.10.0 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect

6
go.sum
View File

@@ -14,6 +14,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/gabriel-vasile/mimetype v1.4.7 h1:SKFKl7kD0RiPdbht0s7hFtjl489WcQ1VyPW8ZzUMYCA=
github.com/gabriel-vasile/mimetype v1.4.7/go.mod h1:GDlAgAyIRT27BhFl53XNAFtfjzOkLaF35JdEG0P7LtU=
github.com/gin-contrib/gzip v1.1.0 h1:kVw7Nr9M+Z6Ch4qo7aGMbiqxDeyQFru+07MgAcUF62M=
github.com/gin-contrib/gzip v1.1.0/go.mod h1:iHJXCup4CWiKyPUEl+GwkHjchl+YyYuMKbOCiXujPIA=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
@@ -47,8 +49,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/wire v0.6.0 h1:HBkoIh4BdSxoyo9PveV8giw7ZsaBOvzWKfcg/6MrVwI=
github.com/google/wire v0.6.0/go.mod h1:F4QhpQ9EDIdJ1Mbop/NZBRB+5yrR6qg3BnctaoUk6NA=
github.com/ixugo/goweb v1.0.9 h1:BZwr8sVKcPx28toH0FSQynhU70uba/qOv3o85ypFZds=
github.com/ixugo/goweb v1.0.9/go.mod h1:iBwaaazAtvEuuODjnoCR/5bssvTi49Eft6x8ULg/jsg=
github.com/ixugo/goweb v1.0.10 h1:zICH1bn/wBoj42R74bj8aQrnqArIB6z1lpvjkcUzsBU=
github.com/ixugo/goweb v1.0.10/go.mod h1:iBwaaazAtvEuuODjnoCR/5bssvTi49Eft6x8ULg/jsg=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=

View File

@@ -42,5 +42,5 @@ func getDialector(dsn string) (gorm.Dialector, bool) {
DSN: dsn,
}), false
}
return sqlite.Open(filepath.Join(system.GetCWD(), dsn)), true
return sqlite.Open(filepath.Join(system.Getwd(), dsn)), true
}

View File

@@ -11,6 +11,7 @@ import (
"strings"
"time"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/gowvp/gb28181/plugin/stat"
"github.com/gowvp/gb28181/plugin/stat/statapi"
@@ -21,7 +22,7 @@ import (
var startRuntime = time.Now()
func setupRouter(r *gin.Engine, uc *Usecase) {
go stat.LoadTop(system.GetCWD(), func(m map[string]any) {
go stat.LoadTop(system.Getwd(), func(m map[string]any) {
_ = m
})
r.Use(
@@ -39,21 +40,24 @@ func setupRouter(r *gin.Engine, uc *Usecase) {
)
go web.CountGoroutines(10*time.Minute, 20)
admin := r.Group("/web")
admin.StaticFS("/", http.Dir(filepath.Join(system.GetCWD(), "www")))
const staticPrefix = "/web"
const staticDir = "www"
admin := r.Group(staticPrefix, gzip.Gzip(gzip.DefaultCompression))
admin.Static("/", filepath.Join(system.Getwd(), staticDir))
r.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api") {
c.JSON(404, "来到了无人的荒漠") // 返回 JSON 格式的 404 错误信息
return
// react-router 路由指向前端资源
if strings.HasPrefix(c.Request.URL.Path, staticPrefix) {
c.File(filepath.Join(system.Getwd(), staticDir, "index.html"))
}
// 网页
c.File(filepath.Join(system.GetCWD(), "www", "index.html"))
c.JSON(404, "来到了无人的荒漠")
})
// 访问根路径时重定向到前端资源
r.GET("/", func(ctx *gin.Context) {
ctx.Redirect(http.StatusPermanentRedirect, "/web/index.html")
ctx.Redirect(http.StatusPermanentRedirect, filepath.Join(staticPrefix, "index.html"))
})
auth := web.AuthMiddleware(uc.Conf.Server.HTTP.JwtSecret)
r.GET("/health", web.WarpH(uc.getHealth))
r.GET("/app/metrics/api", web.WarpH(uc.getMetricsAPI))
registerVersion(r, uc.Version, auth)