Browse Source

restapi: Add version and rehashing

tags/v0.5.0
Daniel Oaks 7 years ago
parent
commit
65cb932e46
1 changed files with 55 additions and 6 deletions
  1. 55
    6
      irc/rest_api.go

+ 55
- 6
irc/rest_api.go View File

21
 // way to do it, given how HTTP handlers dispatch and work.
21
 // way to do it, given how HTTP handlers dispatch and work.
22
 var restAPIServer *Server
22
 var restAPIServer *Server
23
 
23
 
24
+type restVersionResp struct {
25
+	Version string `json:"version"`
26
+}
27
+
24
 type restStatusResp struct {
28
 type restStatusResp struct {
25
 	Clients  int `json:"clients"`
29
 	Clients  int `json:"clients"`
26
 	Opers    int `json:"opers"`
30
 	Opers    int `json:"opers"`
38
 }
42
 }
39
 
43
 
40
 type restAccountsResp struct {
44
 type restAccountsResp struct {
41
-	Accounts map[string]restAcct
45
+	Accounts map[string]restAcct `json:"accounts"`
46
+}
47
+
48
+type restRehashResp struct {
49
+	Successful bool      `json:"successful"`
50
+	Error      string    `json:"error"`
51
+	Time       time.Time `json:"time"`
52
+}
53
+
54
+func restVersion(w http.ResponseWriter, r *http.Request) {
55
+	rs := restVersionResp{
56
+		Version: SemVer,
57
+	}
58
+	b, err := json.Marshal(rs)
59
+	if err != nil {
60
+		fmt.Fprintln(w, restErr)
61
+	} else {
62
+		fmt.Fprintln(w, string(b))
63
+	}
42
 }
64
 }
43
 
65
 
44
 func restStatus(w http.ResponseWriter, r *http.Request) {
66
 func restStatus(w http.ResponseWriter, r *http.Request) {
55
 	}
77
 	}
56
 }
78
 }
57
 
79
 
58
-func restDLines(w http.ResponseWriter, r *http.Request) {
80
+func restGetDLines(w http.ResponseWriter, r *http.Request) {
59
 	rs := restDLinesResp{
81
 	rs := restDLinesResp{
60
 		DLines: restAPIServer.dlines.AllBans(),
82
 		DLines: restAPIServer.dlines.AllBans(),
61
 	}
83
 	}
67
 	}
89
 	}
68
 }
90
 }
69
 
91
 
70
-func restAccounts(w http.ResponseWriter, r *http.Request) {
92
+func restGetAccounts(w http.ResponseWriter, r *http.Request) {
71
 	rs := restAccountsResp{
93
 	rs := restAccountsResp{
72
 		Accounts: make(map[string]restAcct),
94
 		Accounts: make(map[string]restAcct),
73
 	}
95
 	}
89
 	}
111
 	}
90
 }
112
 }
91
 
113
 
114
+func restRehash(w http.ResponseWriter, r *http.Request) {
115
+	err := restAPIServer.rehash()
116
+
117
+	rs := restRehashResp{
118
+		Successful: err == nil,
119
+		Time:       time.Now(),
120
+	}
121
+	if err != nil {
122
+		rs.Error = err.Error()
123
+	}
124
+
125
+	b, err := json.Marshal(rs)
126
+	if err != nil {
127
+		fmt.Fprintln(w, restErr)
128
+	} else {
129
+		fmt.Fprintln(w, string(b))
130
+	}
131
+}
132
+
92
 func (s *Server) startRestAPI() {
133
 func (s *Server) startRestAPI() {
93
 	// so handlers can ref it later
134
 	// so handlers can ref it later
94
 	restAPIServer = s
135
 	restAPIServer = s
95
 
136
 
96
 	// start router
137
 	// start router
97
 	r := mux.NewRouter()
138
 	r := mux.NewRouter()
98
-	r.HandleFunc("/status", restStatus)
99
-	r.HandleFunc("/status/dlines", restDLines)
100
-	r.HandleFunc("/status/accounts", restAccounts)
139
+
140
+	// GET methods
141
+	rg := r.Methods("GET").Subrouter()
142
+	rg.HandleFunc("/version", restVersion)
143
+	rg.HandleFunc("/status", restStatus)
144
+	rg.HandleFunc("/dlines", restGetDLines)
145
+	rg.HandleFunc("/accounts", restGetAccounts)
146
+
147
+	// PUT methods
148
+	rp := r.Methods("POST").Subrouter()
149
+	rp.HandleFunc("/rehash", restRehash)
101
 
150
 
102
 	// start api
151
 	// start api
103
 	go http.ListenAndServe(s.restAPI.Listen, r)
152
 	go http.ListenAndServe(s.restAPI.Listen, r)

Loading…
Cancel
Save