Skip to content

Commit 92aa6ad

Browse files
committed
Add API to get a random domain
1 parent 2755a57 commit 92aa6ad

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func main() {
2828
http.HandleFunc("/images/", staticHandler.ServeHTTP)
2929
http.HandleFunc("/api/rank.json", apiHandler)
3030
http.HandleFunc("/api/multiple.json", multiHandler)
31+
http.HandleFunc("/api/random.json", randomHandler)
3132

3233
err := http.ListenAndServe(listenAddress+":"+strconv.Itoa(listenPort), nil)
3334
if err != nil {

randomHandler.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "net/http"
4+
5+
type RandomResponse struct {
6+
Success bool `json:"success"`
7+
Message string `json:"message"`
8+
Domain string `json:"domain"`
9+
Notice string `json:"notice"`
10+
}
11+
12+
func randomHandler(w http.ResponseWriter, r *http.Request) {
13+
14+
retVal := RandomResponse{}
15+
retVal.Notice = "Free for light, non-commericial use. For heavy or commercial use, please contact us."
16+
17+
// pick a random key from the rankings map: since go tries to be non-deterministic, we'll just pick the first one
18+
for k := range rankings {
19+
retVal.Domain = k
20+
break
21+
}
22+
23+
retVal.Success = true
24+
retVal.Message = "OK"
25+
26+
handleJson(w, r, retVal)
27+
}

0 commit comments

Comments
 (0)