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

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