Skip to content

Commit

Permalink
Add API to get a random domain
Browse files Browse the repository at this point in the history
  • Loading branch information
fileformat committed Dec 12, 2024
1 parent 2755a57 commit 92aa6ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func main() {
http.HandleFunc("/images/", staticHandler.ServeHTTP)
http.HandleFunc("/api/rank.json", apiHandler)
http.HandleFunc("/api/multiple.json", multiHandler)
http.HandleFunc("/api/random.json", randomHandler)

err := http.ListenAndServe(listenAddress+":"+strconv.Itoa(listenPort), nil)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions randomHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import "net/http"

type RandomResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Domain string `json:"domain"`
Notice string `json:"notice"`
}

func randomHandler(w http.ResponseWriter, r *http.Request) {

retVal := RandomResponse{}
retVal.Notice = "Free for light, non-commericial use. For heavy or commercial use, please contact us."

// pick a random key from the rankings map: since go tries to be non-deterministic, we'll just pick the first one
for k := range rankings {
retVal.Domain = k
break
}

retVal.Success = true
retVal.Message = "OK"

handleJson(w, r, retVal)
}

0 comments on commit 92aa6ad

Please sign in to comment.