Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
all: add /domains and /domain/:domain to the list
go.mod | 8 ++++ go.sum | 10 ++++++ main.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/go.mod b/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..95e200c48b3cbcbfc52d6ca1594ff8164b4447bb --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module git.sr.ht/~porcellis/antispam-api + +go 1.16 + +require ( + github.com/go-chi/chi v4.1.2+incompatible + golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..66b39d1fc912a469b0bef2e3bd09d84597856c30 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec= +github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= +golang.org/x/net v0.0.0-20211216030914-fe4d6282115f h1:hEYJvxw1lSnWIl8X9ofsYMklzaDs90JI2az5YMd4fPM= +golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000000000000000000000000000000000000..ae2dd69c5a7ddb0cfb436273e48d8b4ba596cab3 --- /dev/null +++ b/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + + "github.com/go-chi/chi" + "github.com/go-chi/chi/middleware" +) + +func isOnList(list []string, str string) bool { + for _, v := range list { + if v == str { + return true + } + } + + return false +} + +func registerDomains(router chi.Router) { + router.Get("/domains", func(w http.ResponseWriter, r *http.Request) { + jsonFile, err := os.Open("data/domains.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("/domains/*", func(w http.ResponseWriter, r *http.Request) { + domain := chi.URLParam(r, "*") + + jsonFile, err := os.Open("data/domains.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]) + os.Exit(1) + } + + router := chi.NewRouter() + + router.Use(middleware.RealIP) + router.Use(middleware.Logger) + + registerDomains(router) + + addr := ":8121" + if len(os.Args) > 2 { + addr = os.Args[2] + } + + log.Printf("Listening on %s", addr) + http.ListenAndServe(addr, router) +}