Przeglądaj źródła

restapi: Add accts, restructure a little

tags/v0.5.0
Daniel Oaks 7 lat temu
rodzic
commit
539e5431b9
1 zmienionych plików z 35 dodań i 1 usunięć
  1. 35
    1
      irc/rest_api.go

+ 35
- 1
irc/rest_api.go Wyświetl plik

8
 import (
8
 import (
9
 	"encoding/json"
9
 	"encoding/json"
10
 	"net/http"
10
 	"net/http"
11
+	"time"
11
 
12
 
12
 	"fmt"
13
 	"fmt"
13
 
14
 
30
 	DLines map[string]IPBanInfo `json:"dlines"`
31
 	DLines map[string]IPBanInfo `json:"dlines"`
31
 }
32
 }
32
 
33
 
34
+type restAcct struct {
35
+	Name         string
36
+	RegisteredAt time.Time `json:"registered-at"`
37
+	Clients      int
38
+}
39
+
40
+type restAccountsResp struct {
41
+	Accounts map[string]restAcct
42
+}
43
+
33
 func restStatus(w http.ResponseWriter, r *http.Request) {
44
 func restStatus(w http.ResponseWriter, r *http.Request) {
34
 	rs := restStatusResp{
45
 	rs := restStatusResp{
35
 		Clients:  restAPIServer.clients.Count(),
46
 		Clients:  restAPIServer.clients.Count(),
56
 	}
67
 	}
57
 }
68
 }
58
 
69
 
70
+func restAccounts(w http.ResponseWriter, r *http.Request) {
71
+	rs := restAccountsResp{
72
+		Accounts: make(map[string]restAcct),
73
+	}
74
+
75
+	// get accts
76
+	for key, info := range restAPIServer.accounts {
77
+		rs.Accounts[key] = restAcct{
78
+			Name:         info.Name,
79
+			RegisteredAt: info.RegisteredAt,
80
+			Clients:      len(info.Clients),
81
+		}
82
+	}
83
+
84
+	b, err := json.Marshal(rs)
85
+	if err != nil {
86
+		fmt.Fprintln(w, restErr)
87
+	} else {
88
+		fmt.Fprintln(w, string(b))
89
+	}
90
+}
91
+
59
 func (s *Server) startRestAPI() {
92
 func (s *Server) startRestAPI() {
60
 	// so handlers can ref it later
93
 	// so handlers can ref it later
61
 	restAPIServer = s
94
 	restAPIServer = s
63
 	// start router
96
 	// start router
64
 	r := mux.NewRouter()
97
 	r := mux.NewRouter()
65
 	r.HandleFunc("/status", restStatus)
98
 	r.HandleFunc("/status", restStatus)
66
-	r.HandleFunc("/dlines", restDLines)
99
+	r.HandleFunc("/status/dlines", restDLines)
100
+	r.HandleFunc("/status/accounts", restAccounts)
67
 
101
 
68
 	// start api
102
 	// start api
69
 	go http.ListenAndServe(s.restAPI.Listen, r)
103
 	go http.ListenAndServe(s.restAPI.Listen, r)

Ładowanie…
Anuluj
Zapisz