Kaynağa Gözat

restapi: Add accts, restructure a little

tags/v0.5.0
Daniel Oaks 7 yıl önce
ebeveyn
işleme
539e5431b9
1 değiştirilmiş dosya ile 35 ekleme ve 1 silme
  1. 35
    1
      irc/rest_api.go

+ 35
- 1
irc/rest_api.go Dosyayı Görüntüle

@@ -8,6 +8,7 @@ package irc
8 8
 import (
9 9
 	"encoding/json"
10 10
 	"net/http"
11
+	"time"
11 12
 
12 13
 	"fmt"
13 14
 
@@ -30,6 +31,16 @@ type restDLinesResp struct {
30 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 44
 func restStatus(w http.ResponseWriter, r *http.Request) {
34 45
 	rs := restStatusResp{
35 46
 		Clients:  restAPIServer.clients.Count(),
@@ -56,6 +67,28 @@ func restDLines(w http.ResponseWriter, r *http.Request) {
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 92
 func (s *Server) startRestAPI() {
60 93
 	// so handlers can ref it later
61 94
 	restAPIServer = s
@@ -63,7 +96,8 @@ func (s *Server) startRestAPI() {
63 96
 	// start router
64 97
 	r := mux.NewRouter()
65 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 102
 	// start api
69 103
 	go http.ListenAndServe(s.restAPI.Listen, r)

Loading…
İptal
Kaydet