Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
all: add /usernames and /usernames/:username
main.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/main.go b/main.go index ae2dd69c5a7ddb0cfb436273e48d8b4ba596cab3..0abe7e1850b9733f03474d409ffd18234a5e65ec 100644 --- a/main.go +++ b/main.go @@ -72,6 +72,56 @@ } }) } +func registerUsernames(router chi.Router) { + router.Get("/usernames", func(w http.ResponseWriter, r *http.Request) { + jsonFile, err := os.Open("data/usernames.json") + + if err != nil { + w.WriteHeader(500) + w.Write([]byte("There was some error when opening the raw file")) + + return + } + + // defer the closing of our jsonFile so that we can parse it later on + defer jsonFile.Close() + + byteValue, _ := ioutil.ReadAll(jsonFile) + + w.Header().Add("Content-Type", "application/json; charset=utf-8") + w.WriteHeader(200) + w.Write(byteValue) + }) + + router.Get("/usernames/*", func(w http.ResponseWriter, r *http.Request) { + domain := chi.URLParam(r, "*") + + jsonFile, err := os.Open("data/usernames.json") + + if err != nil { + w.WriteHeader(500) + w.Write([]byte("There was some error when opening the raw file")) + + return + } + + defer jsonFile.Close() + byteValue, _ := ioutil.ReadAll(jsonFile) + + // XXX: Maybe we could do this only once? + var domains []string + json.Unmarshal(byteValue, &domains) + + if isOnList(domains, domain) { + w.WriteHeader(200) + w.Write([]byte("")) + } else { + w.WriteHeader(404) + w.Write([]byte("")) + } + }) +} + func main() { if len(os.Args) < 2 { fmt.Printf("Usage: %s serve\n", os.Args[0]) @@ -84,6 +134,7 @@ router.Use(middleware.RealIP) router.Use(middleware.Logger) registerDomains(router) + registerUsernames(router) addr := ":8121" if len(os.Args) > 2 {