From 7bccb62f1445a11d8479018797a058594bbc1efb Mon Sep 17 00:00:00 2001 From: Ehco1996 Date: Sat, 27 Jan 2024 17:12:52 +0800 Subject: [PATCH] Refactor welcome page HTML template and update welcome handler --- internal/constant/constant.go | 139 +++++++++++++++++++++++++++------- internal/web/handlers.go | 27 ++++++- internal/web/server.go | 2 +- 3 files changed, 137 insertions(+), 31 deletions(-) diff --git a/internal/constant/constant.go b/internal/constant/constant.go index 34fe0f9ca..8f4f5c8b8 100644 --- a/internal/constant/constant.go +++ b/internal/constant/constant.go @@ -10,32 +10,6 @@ var ( GitBranch string GitRevision string BuildTime string - - WelcomeHTML = ` - - - - - -

ehco is a network relay tool and a typo :)

-
-

Version: ` + Version + `

-

GitBranch: ` + GitBranch + `

-

GitRevision: ` + GitRevision + `

-

BuildTime: ` + BuildTime + `

-
-

Metrics

-

Debug

-

Clash Proxy Provider

- -
- -
-
-

More information here

- - - ` ) const ( @@ -61,3 +35,116 @@ const ( BUFFER_POOL_SIZE = 1024 // support 512 connections BUFFER_SIZE = 20 * 1024 // 20KB the maximum packet size of shadowsocks is about 16 KiB ) + +const WelcomeHTML = ` + + + + + + + + +
+

ehco is a network relay tool and a typo :)

+
+

Build Info:

+

Version: {{.Version}}

+

GitBranch: {{.GitBranch}}

+

GitRevision: {{.GitRevision}}

+

BuildTime: {{.BuildTime}}

+
+ + + +
+ +
+ + +
+ + + + + +` diff --git a/internal/web/handlers.go b/internal/web/handlers.go index 8c45ddab9..eb43dbcc2 100644 --- a/internal/web/handlers.go +++ b/internal/web/handlers.go @@ -3,7 +3,9 @@ package web import ( "fmt" "net/http" + "text/template" + "github.com/Ehco1996/ehco/internal/config" "github.com/Ehco1996/ehco/internal/constant" "go.uber.org/zap" ) @@ -15,15 +17,32 @@ func MakeIndexF() http.HandlerFunc { } } -func welcome(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, constant.WelcomeHTML) -} - func writerBadRequestMsg(w http.ResponseWriter, msg string) { w.WriteHeader(http.StatusBadRequest) _, _ = w.Write([]byte(msg)) } +func (s *Server) welcome(w http.ResponseWriter, r *http.Request) { + tmpl := template.Must(template.New("").Parse(constant.WelcomeHTML)) + data := struct { + Version string + GitBranch string + GitRevision string + BuildTime string + SubConfigs []*config.SubConfig + }{ + Version: "1.0", + GitBranch: "master", + GitRevision: "1a2b3c", + BuildTime: "2022-01-01T00:00:00Z", + SubConfigs: s.cfg.SubConfigs, + } + if err := tmpl.Execute(w, data); err != nil { + writerBadRequestMsg(w, err.Error()) + return + } +} + func (s *Server) HandleClashProxyProvider(w http.ResponseWriter, r *http.Request) { subName := r.URL.Query().Get("sub_name") if subName == "" { diff --git a/internal/web/server.go b/internal/web/server.go index fa0ab7d2d..c8a51df71 100644 --- a/internal/web/server.go +++ b/internal/web/server.go @@ -50,7 +50,7 @@ func NewServer(cfg *config.Config, relayReloader reloader.Reloader) (*Server, er } // register handler - e.GET("/", echo.WrapHandler(http.HandlerFunc(welcome))) + e.GET("/", echo.WrapHandler(http.HandlerFunc(s.welcome))) e.GET("/metrics/", echo.WrapHandler(promhttp.Handler())) e.GET("/debug/pprof/*", echo.WrapHandler(http.DefaultServeMux)) e.GET("/clash_proxy_provider/", echo.WrapHandler(http.HandlerFunc(s.HandleClashProxyProvider)))