Skip to content

Commit

Permalink
Add current config endpoint and handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Jan 27, 2024
1 parent 7bccb62 commit 32133b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const WelcomeHTML = `
<ul>
<li><a href="/metrics/">Metrics</a></li>
<li><a href="/debug/pprof/">Debug</a></li>
<li><a href="/config/">Current Config</a></li>
</ul>
{{if .SubConfigs}}
Expand All @@ -120,7 +121,7 @@ const WelcomeHTML = `
</div>
<footer>
<a href="https://github.com/Ehco1996/ehco">More information here</a>
<a href="https://github.com/Ehco1996/ehco">Source code</a>
</footer>
</div>
</body>
Expand Down
13 changes: 13 additions & 0 deletions internal/web/handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"encoding/json"
"fmt"
"net/http"
"text/template"
Expand Down Expand Up @@ -95,3 +96,15 @@ func (s *Server) HandleReload(w http.ResponseWriter, r *http.Request) {
return
}
}

func (s *Server) CurrentConfig(w http.ResponseWriter, r *http.Request) {
// return json config
ret, err := json.Marshal(s.cfg)
if err != nil {
writerBadRequestMsg(w, err.Error())
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(ret)
}
1 change: 1 addition & 0 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func NewServer(cfg *config.Config, relayReloader reloader.Reloader) (*Server, er
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)))
e.GET("/config/", echo.WrapHandler(http.HandlerFunc(s.CurrentConfig)))

e.POST("/reload/", echo.WrapHandler(http.HandlerFunc(s.HandleReload)))
return s, nil
Expand Down

0 comments on commit 32133b5

Please sign in to comment.