From 83e6b78a93158156174599f12e0cbeb18f6f6805 Mon Sep 17 00:00:00 2001 From: WJQSERVER Date: Wed, 1 Jan 2025 08:45:21 +0800 Subject: [PATCH] add embed.FS and debug --- .github/workflows/build-dev.yml | 2 +- config/config.go | 1 + config/config.toml | 1 + deploy/config.toml | 1 + docker/dockerfile/nocache/config.toml | 1 + main.go | 32 ++++++++++++++++++++++----- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index 715f49b..4a3a226 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -37,7 +37,7 @@ jobs: GOOS: ${{ matrix.goos }} GOARCH: ${{ matrix.goarch }} run: | - CGO_ENABLED=0 go build -ldflags "-X main.version=${{ env.VERSION }}" -o ${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}} ./main.go + CGO_ENABLED=0 go build -ldflags "-X main.version=${{ env.VERSION }} -X main.dev=true" -o ${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}} ./main.go - name: 打包 run: | mkdir ghproxyd diff --git a/config/config.go b/config/config.go index 1b9a028..ea325a4 100644 --- a/config/config.go +++ b/config/config.go @@ -20,6 +20,7 @@ type ServerConfig struct { Host string `toml:"host"` SizeLimit int `toml:"sizeLimit"` EnableH2C string `toml:"enableH2C"` + Debug bool `toml:"debug"` } type PagesConfig struct { diff --git a/config/config.toml b/config/config.toml index e4cc2e7..5aff583 100644 --- a/config/config.toml +++ b/config/config.toml @@ -3,6 +3,7 @@ host = "127.0.0.1" port = 8080 sizeLimit = 125 # MB enableH2C = "on" # "on" or "off" +debug = false [pages] enabled = false diff --git a/deploy/config.toml b/deploy/config.toml index f3f9e16..4afc4c2 100644 --- a/deploy/config.toml +++ b/deploy/config.toml @@ -3,6 +3,7 @@ host = "127.0.0.1" port = 8080 sizeLimit = 125 # MB enableH2C = false +debug = false [pages] enabled = true diff --git a/docker/dockerfile/nocache/config.toml b/docker/dockerfile/nocache/config.toml index aca6281..a0a4815 100644 --- a/docker/dockerfile/nocache/config.toml +++ b/docker/dockerfile/nocache/config.toml @@ -3,6 +3,7 @@ host = "0.0.0.0" port = 80 #修改此配置会导致容器异常 sizeLimit = 125 # MB enableH2C = "off" # on / off +debug = false [pages] enabled = true diff --git a/main.go b/main.go index 3734e14..9814cad 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import ( + "embed" "flag" "fmt" + "io/fs" "log" "net/http" "time" @@ -24,10 +26,17 @@ var ( configfile = "/data/ghproxy/config/config.toml" cfgfile string version string + dev bool + runMode string limiter *rate.RateLimiter iplimiter *rate.IPRateLimiter ) +var ( + //go:embed pages/* + pagesFS embed.FS +) + var ( logw = logger.Logw logInfo = logger.LogInfo @@ -89,7 +98,16 @@ func init() { loadlist(cfg) setupRateLimit(cfg) - gin.SetMode(gin.ReleaseMode) + if cfg.Server.Debug { + dev = true + } + if dev { + gin.SetMode(gin.DebugMode) + runMode = "dev" + } else { + gin.SetMode(gin.ReleaseMode) + runMode = "release" + } router = gin.Default() //H2C默认值为true,而后遵循cfg.Server.EnableH2C的设置 @@ -112,14 +130,16 @@ func init() { }) router.StaticFile("/favicon.ico", faviconPath) } else if !cfg.Pages.Enabled { - router.GET("/", func(c *gin.Context) { - c.String(http.StatusForbidden, "403 Forbidden Access") - logWarning("403 > Path:/ IP:%s UA:%s METHOD:%s HTTPv:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method, c.Request.Proto) - }) + pages, err := fs.Sub(pagesFS, "pages") + if err != nil { + log.Fatalf("Failed when processing pages: %s", err) + } + router.GET("/", gin.WrapH(http.FileServer(http.FS(pages)))) + router.GET("/favicon.ico", gin.WrapH(http.FileServer(http.FS(pages)))) } router.NoRoute(func(c *gin.Context) { - proxy.NoRouteHandler(cfg, limiter, iplimiter)(c) + proxy.NoRouteHandler(cfg, limiter, iplimiter, runMode)(c) }) }