Skip to content

Commit

Permalink
feat: proxy rest
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Aug 17, 2024
1 parent 1255da1 commit a8c01ce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
44 changes: 23 additions & 21 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,35 @@ <h1>Akash Proxy</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>URL</th>
<th>Average response time</th>
<th>Server</th>
<th>Request Count</th>
<th>Avg response time</th>
<th>Error Rate</th>
<th>Status</th>
<th>Kind</th>
</tr>
</thead>
<!-- prettier-ignore -->
<tbody>
{{ range .}}
<tr>
<th>{{.Name}}</th>
<th><a href="{{.URL}}">{{.URL}}</a></th>
<th>{{.Avg}}</th>
<th>{{.Requests}}</th>
<th>{{.ErrorRate}}%</th>
<th>
<!-- prettier-ignore -->
{{ if not .Initialized}}
initializing
{{ else if .Degraded }}
degraded
{{else}}
OK
{{end}}
</th>
</tr>
{{ range $key, $value := . }}
{{ range $value }}
<tr>
<th><a href="{{ .URL }}">{{ .Name }}</a></th>
<th>{{ .Requests }}</th>
<th>{{ .Avg }}</th>
<th>{{ .ErrorRate }}%</th>
<th>
{{ if not .Initialized }}
initializing
{{ else if .Degraded }}
degraded
{{ else }}
OK
{{ end }}
</th>
<th>{{ $key }}</th>
</tr>
{{ end }}
{{ end }}
</tbody>
</table>
Expand Down
18 changes: 12 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,34 @@ func main() {
am.HostPolicy = autocert.HostWhitelist(hosts...)
}

proxyHandler := proxy.New(cfg)
rpcProxyHandler := proxy.New(proxy.RPC, cfg)
restProxyHandler := proxy.New(proxy.Rest, cfg)

proxyCtx, proxyCtxCancel := context.WithCancel(context.Background())
defer proxyCtxCancel()
proxyHandler.Start(proxyCtx)
rpcProxyHandler.Start(proxyCtx)
restProxyHandler.Start(proxyCtx)

indexTpl := template.Must(template.New("stats").Parse(string(index)))

m := http.NewServeMux()
m.Handle("/health/ready", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !proxyHandler.Ready() {
if !rpcProxyHandler.Ready() || !restProxyHandler.Ready() {
w.WriteHeader(http.StatusServiceUnavailable)
}
}))
m.Handle("/health/live", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !proxyHandler.Live() {
if !rpcProxyHandler.Live() || !restProxyHandler.Live() {
w.WriteHeader(http.StatusServiceUnavailable)
}
}))
m.Handle("/rpc", proxyHandler)
m.Handle("/rpc", rpcProxyHandler)
m.Handle("/rest", restProxyHandler)
m.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := indexTpl.Execute(w, proxyHandler.Stats()); err != nil {
if err := indexTpl.Execute(w, map[string][]proxy.ServerStat{
"RPC": rpcProxyHandler.Stats(),
"Rest": restProxyHandler.Stats(),
}); err != nil {
slog.Error("could render stats", "err", err)
}
}))
Expand Down

0 comments on commit a8c01ce

Please sign in to comment.