Skip to content

Commit

Permalink
http: display all api.
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldin95 committed Oct 19, 2024
1 parent 9065599 commit 0255bc9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
5 changes: 4 additions & 1 deletion cmd/api/v5/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ func (u Network) Commands(app *api.App) {
Name: "list",
Usage: "Display all network",
Aliases: []string{"ls"},
Action: u.List,
Flags: []cli.Flag{
&cli.StringFlag{Name: "name"},
},
Action: u.List,
},
{
Name: "add",
Expand Down
44 changes: 34 additions & 10 deletions pkg/proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ func NewHttpProxy(cfg *co.HttpProxy, px Proxyer) *HttpProxy {
func (t *HttpProxy) loadUrl() {
if strings.HasPrefix(t.cfg.Listen, "127.0.0.") {
t.api.HandleFunc("/", t.GetStats).Methods("GET")
t.api.HandleFunc("/config", t.GetConfig).Methods("GET")
t.api.HandleFunc("/config/match/{rule}/to/{remote}", t.AddMatch).Methods("POST")
t.api.HandleFunc("/config/match/{rule}/to/{remote}", t.DelMatch).Methods("DELETE")
t.api.HandleFunc("/api", t.GetApi).Methods("GET")
t.api.HandleFunc("/api/config", t.GetConfig).Methods("GET")
t.api.HandleFunc("/api/match/{domain}/to/{backend}", t.AddMatch).Methods("POST")
t.api.HandleFunc("/api/match/{domain}/to/{backend}", t.DelMatch).Methods("DELETE")
t.api.HandleFunc("/pac", t.GetPac).Methods("GET")
}
t.api.NotFoundHandler = http.HandlerFunc(NotFound)
Expand Down Expand Up @@ -460,7 +461,10 @@ var httpTmpl = map[string]string{
<td>Total:</td><td>{{ .Total }}</td>
</tr>
<tr>
<td>Configuration:</td><td><a href="/config">display</a></td>
<td>Configuration:</td><td><a href="/api/config">display</a></td>
</tr>
<tr>
<td>APIs:</td><td><a href="/api">display</a></td>
</tr>
<tr>
<td>StartAt:</td><td>{{ .StartAt }}</td>
Expand Down Expand Up @@ -540,13 +544,13 @@ func (t *HttpProxy) GetConfig(w http.ResponseWriter, r *http.Request) {
func (t *HttpProxy) AddMatch(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

rule := vars["rule"]
remote := vars["remote"]
domain := vars["domain"]
backend := vars["backend"]

t.lock.Lock()
defer t.lock.Unlock()

if t.cfg.AddMatch(rule, remote) > -1 {
if t.cfg.AddMatch(domain, backend) > -1 {
encodeYaml(w, "success")
} else {
encodeYaml(w, "failed")
Expand All @@ -557,13 +561,13 @@ func (t *HttpProxy) AddMatch(w http.ResponseWriter, r *http.Request) {
func (t *HttpProxy) DelMatch(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

rule := vars["rule"]
remote := vars["remote"]
domain := vars["domain"]
backend := vars["backend"]

t.lock.Lock()
defer t.lock.Unlock()

if t.cfg.DelMatch(rule, remote) > -1 {
if t.cfg.DelMatch(domain, backend) > -1 {
encodeYaml(w, "success")
} else {
encodeYaml(w, "failed")
Expand Down Expand Up @@ -596,3 +600,23 @@ func (t *HttpProxy) findQuery(r *http.Request, name string) string {
}
return ""
}

func (t *HttpProxy) GetApi(w http.ResponseWriter, r *http.Request) {
var urls []string

t.api.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
path, err := route.GetPathTemplate()
if err != nil {
return nil
}
methods, err := route.GetMethods()
if err != nil {
return nil
}
for _, m := range methods {
urls = append(urls, fmt.Sprintf("%-6s %s", m, path))
}
return nil
})
encodeYaml(w, urls)
}

0 comments on commit 0255bc9

Please sign in to comment.