add embed.FS and debug

This commit is contained in:
WJQSERVER 2025-01-01 08:45:21 +08:00
parent 8371f9564f
commit 83e6b78a93
6 changed files with 31 additions and 7 deletions

View file

@ -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

View file

@ -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 {

View file

@ -3,6 +3,7 @@ host = "127.0.0.1"
port = 8080
sizeLimit = 125 # MB
enableH2C = "on" # "on" or "off"
debug = false
[pages]
enabled = false

View file

@ -3,6 +3,7 @@ host = "127.0.0.1"
port = 8080
sizeLimit = 125 # MB
enableH2C = false
debug = false
[pages]
enabled = true

View file

@ -3,6 +3,7 @@ host = "0.0.0.0"
port = 80 #修改此配置会导致容器异常
sizeLimit = 125 # MB
enableH2C = "off" # on / off
debug = false
[pages]
enabled = true

30
main.go
View file

@ -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)
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)
})
}