3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 20:09:41 +01:00

restapi: Add accts, restructure a little

This commit is contained in:
Daniel Oaks 2016-11-06 11:31:27 +10:00
parent ee3853f845
commit 539e5431b9

View File

@ -8,6 +8,7 @@ package irc
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"time"
"fmt" "fmt"
@ -30,6 +31,16 @@ type restDLinesResp struct {
DLines map[string]IPBanInfo `json:"dlines"` DLines map[string]IPBanInfo `json:"dlines"`
} }
type restAcct struct {
Name string
RegisteredAt time.Time `json:"registered-at"`
Clients int
}
type restAccountsResp struct {
Accounts map[string]restAcct
}
func restStatus(w http.ResponseWriter, r *http.Request) { func restStatus(w http.ResponseWriter, r *http.Request) {
rs := restStatusResp{ rs := restStatusResp{
Clients: restAPIServer.clients.Count(), Clients: restAPIServer.clients.Count(),
@ -56,6 +67,28 @@ func restDLines(w http.ResponseWriter, r *http.Request) {
} }
} }
func restAccounts(w http.ResponseWriter, r *http.Request) {
rs := restAccountsResp{
Accounts: make(map[string]restAcct),
}
// get accts
for key, info := range restAPIServer.accounts {
rs.Accounts[key] = restAcct{
Name: info.Name,
RegisteredAt: info.RegisteredAt,
Clients: len(info.Clients),
}
}
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
func (s *Server) startRestAPI() { func (s *Server) startRestAPI() {
// so handlers can ref it later // so handlers can ref it later
restAPIServer = s restAPIServer = s
@ -63,7 +96,8 @@ func (s *Server) startRestAPI() {
// start router // start router
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/status", restStatus) r.HandleFunc("/status", restStatus)
r.HandleFunc("/dlines", restDLines) r.HandleFunc("/status/dlines", restDLines)
r.HandleFunc("/status/accounts", restAccounts)
// start api // start api
go http.ListenAndServe(s.restAPI.Listen, r) go http.ListenAndServe(s.restAPI.Listen, r)