From 50cfd64db8c699bddccad8185731defbced1a3c7 Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Fri, 28 Mar 2025 10:37:07 +0800 Subject: [PATCH] update readme.md --- README.md | 6 +- main.go | 166 +++++++++++++++++++++++++++--------------------------- 2 files changed, 87 insertions(+), 85 deletions(-) diff --git a/README.md b/README.md index 9f5595f..7f5da74 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ - 支持Git clone,raw,realeases等文件拉取 - 支持多个前端主题 - 支持自定义黑名单/白名单 -- 支持Git Clone缓存(配合组件) +- 支持Git Clone缓存(配合[Smart-Git](https://github.com/WJQSERVER-STUDIO/smart-git)) - 支持Docker部署 - 支持速率限制 - 支持用户鉴权 @@ -62,6 +62,8 @@ git clone https://ghproxy.1888866.xyz/https://github.com/WJQSERVER-STUDIO/ghprox ## 部署说明 +可参考文章: https://blog.wjqserver.com/post/ghproxy-deploy-with-smart-git/ + ### Docker部署 - Docker-cli @@ -184,6 +186,8 @@ url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890" 支持Socks5/HTTP(S) ### 前端页面 +参看[GHProxy-Frontend](https://github.com/WJQSERVER-STUDIO/GHProxy-Frontend) + #### Bootstrap主题 ![ghproxy-demo.png](https://webp.wjqserver.com/ghproxy/1.8.1-light.png) ![ghproxy-demo-dark.png](https://webp.wjqserver.com/ghproxy/1.8.1-dark.png) diff --git a/main.go b/main.go index e1332a6..e8d6a7e 100644 --- a/main.go +++ b/main.go @@ -156,7 +156,7 @@ func InitReq(cfg *config.Config) { } // loadEmbeddedPages 加载嵌入式页面资源 -func loadEmbeddedPages(cfg *config.Config) (fs.FS, error) { +func loadEmbeddedPages(cfg *config.Config) (fs.FS, fs.FS, error) { var pages fs.FS var err error switch cfg.Pages.Theme { @@ -178,125 +178,123 @@ func loadEmbeddedPages(cfg *config.Config) (fs.FS, error) { } if err != nil { - return nil, fmt.Errorf("failed to load embedded pages: %w", err) + return nil, nil, fmt.Errorf("failed to load embedded pages: %w", err) } - return pages, nil + + var assets fs.FS + assets, err = fs.Sub(pagesFS, "pages/assets") + return pages, assets, nil } // setupPages 设置页面路由 func setupPages(cfg *config.Config, r *server.Hertz) { switch cfg.Pages.Mode { case "internal": - // 加载嵌入式资源 - pages, err := loadEmbeddedPages(cfg) + err := setInternalRoute(cfg, r) if err != nil { logError("Failed when processing internal pages: %s", err) + fmt.Println(err.Error()) return } - // 设置嵌入式资源路由 - r.GET("/", func(ctx context.Context, c *app.RequestContext) { - staticServer := http.FileServer(http.FS(pages)) - req, err := adaptor.GetCompatRequest(&c.Request) - if err != nil { - logError("%s", err) - return - } - staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) - }) - r.GET("/favicon.ico", func(ctx context.Context, c *app.RequestContext) { - staticServer := http.FileServer(http.FS(pages)) - req, err := adaptor.GetCompatRequest(&c.Request) - if err != nil { - logError("%s", err) - return - } - staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) - }) - r.GET("/script.js", func(ctx context.Context, c *app.RequestContext) { - staticServer := http.FileServer(http.FS(pages)) - req, err := adaptor.GetCompatRequest(&c.Request) - if err != nil { - logError("%s", err) - return - } - staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) - }) - r.GET("/style.css", func(ctx context.Context, c *app.RequestContext) { - staticServer := http.FileServer(http.FS(pages)) - req, err := adaptor.GetCompatRequest(&c.Request) - if err != nil { - logError("%s", err) - return - } - staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) - }) - case "external": // 设置外部资源路径 indexPagePath := fmt.Sprintf("%s/index.html", cfg.Pages.StaticDir) faviconPath := fmt.Sprintf("%s/favicon.ico", cfg.Pages.StaticDir) javascriptsPath := fmt.Sprintf("%s/script.js", cfg.Pages.StaticDir) stylesheetsPath := fmt.Sprintf("%s/style.css", cfg.Pages.StaticDir) - //bootstrapPath := fmt.Sprintf("%s/bootstrap.min.css", cfg.Pages.StaticDir) + bootstrapPath := fmt.Sprintf("%s/bootstrap.min.css", cfg.Pages.StaticDir) + bootstrapBundlePath := fmt.Sprintf("%s/bootstrap.bundle.min.js", cfg.Pages.StaticDir) // 设置外部资源路由 r.StaticFile("/", indexPagePath) r.StaticFile("/favicon.ico", faviconPath) r.StaticFile("/script.js", javascriptsPath) r.StaticFile("/style.css", stylesheetsPath) + r.StaticFile("/bootstrap.min.css", bootstrapPath) + r.StaticFile("/bootstrap.bundle.min.js", bootstrapBundlePath) //router.StaticFile("/bootstrap.min.css", bootstrapPath) default: // 处理无效的Pages Mode logWarning("Invalid Pages Mode: %s, using default embedded theme", cfg.Pages.Mode) - // 加载嵌入式资源 - pages, err := loadEmbeddedPages(cfg) + err := setInternalRoute(cfg, r) if err != nil { - logError("Failed when processing pages: %s", err) + logError("Failed when processing internal pages: %s", err) + fmt.Println(err.Error()) return } - // 设置嵌入式资源路由 - r.GET("/", func(ctx context.Context, c *app.RequestContext) { - staticServer := http.FileServer(http.FS(pages)) - req, err := adaptor.GetCompatRequest(&c.Request) - if err != nil { - logError("%s", err) - return - } - staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) - }) - r.GET("/favicon.ico", func(ctx context.Context, c *app.RequestContext) { - staticServer := http.FileServer(http.FS(pages)) - req, err := adaptor.GetCompatRequest(&c.Request) - if err != nil { - logError("%s", err) - return - } - staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) - }) - r.GET("/script.js", func(ctx context.Context, c *app.RequestContext) { - staticServer := http.FileServer(http.FS(pages)) - req, err := adaptor.GetCompatRequest(&c.Request) - if err != nil { - logError("%s", err) - return - } - staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) - }) - r.GET("/style.css", func(ctx context.Context, c *app.RequestContext) { - staticServer := http.FileServer(http.FS(pages)) - req, err := adaptor.GetCompatRequest(&c.Request) - if err != nil { - logError("%s", err) - return - } - staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) - }) + } } +func setInternalRoute(cfg *config.Config, r *server.Hertz) error { + + // 加载嵌入式资源 + pages, assets, err := loadEmbeddedPages(cfg) + if err != nil { + logError("Failed when processing pages: %s", err) + return err + } + // 设置嵌入式资源路由 + r.GET("/", func(ctx context.Context, c *app.RequestContext) { + staticServer := http.FileServer(http.FS(pages)) + req, err := adaptor.GetCompatRequest(&c.Request) + if err != nil { + logError("%s", err) + return + } + staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) + }) + r.GET("/favicon.ico", func(ctx context.Context, c *app.RequestContext) { + staticServer := http.FileServer(http.FS(pages)) + req, err := adaptor.GetCompatRequest(&c.Request) + if err != nil { + logError("%s", err) + return + } + staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) + }) + r.GET("/script.js", func(ctx context.Context, c *app.RequestContext) { + staticServer := http.FileServer(http.FS(pages)) + req, err := adaptor.GetCompatRequest(&c.Request) + if err != nil { + logError("%s", err) + return + } + staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) + }) + r.GET("/style.css", func(ctx context.Context, c *app.RequestContext) { + staticServer := http.FileServer(http.FS(pages)) + req, err := adaptor.GetCompatRequest(&c.Request) + if err != nil { + logError("%s", err) + return + } + staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) + }) + r.GET("/bootstrap.min.css", func(ctx context.Context, c *app.RequestContext) { + staticServer := http.FileServer(http.FS(assets)) + req, err := adaptor.GetCompatRequest(&c.Request) + if err != nil { + logError("%s", err) + return + } + staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) + }) + r.GET("/bootstrap.bundle.min.js", func(ctx context.Context, c *app.RequestContext) { + staticServer := http.FileServer(http.FS(assets)) + req, err := adaptor.GetCompatRequest(&c.Request) + if err != nil { + logError("%s", err) + return + } + staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req) + }) + return nil +} + func init() { readFlag() flag.Parse()