From 6d892347d125737029b39026658979bde5ff56cb Mon Sep 17 00:00:00 2001 From: Andrew Thorp Date: Sat, 14 Aug 2021 20:53:43 -0400 Subject: [PATCH] Split out further, format --- document.go | 2 +- lookup.go | 31 +++++++++++++++++++++++++++++++ main.go | 47 +++++++++++++++-------------------------------- 3 files changed, 47 insertions(+), 33 deletions(-) create mode 100644 lookup.go diff --git a/document.go b/document.go index 895833d..252df4b 100644 --- a/document.go +++ b/document.go @@ -2,8 +2,8 @@ package main import ( "bufio" - "os" "log" + "os" "github.com/a-h/gemini" ) diff --git a/lookup.go b/lookup.go new file mode 100644 index 0000000..9f45c00 --- /dev/null +++ b/lookup.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + + "github.com/a-h/gemini" +) + +func handleLookup(w gemini.ResponseWriter, r *gemini.Request) { + const URLTemplate string = "https://api.dictionaryapi.dev/api/v2/entries/en_US/%s" + // TODO: Sanitize input + word := r.URL.RawQuery + log.Printf("Looking up \"%s\"\n", word) + + resp, err := http.Get(fmt.Sprintf(URLTemplate, word)) + if err != nil { + log.Fatalln(err) + } + // Why is the body the stream? + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + + w.Write(definitionToPage(body)) +} diff --git a/main.go b/main.go index bb02ea9..8c74045 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,7 @@ package main import ( "context" "fmt" - "io/ioutil" "log" - "net/http" "github.com/a-h/gemini" "github.com/a-h/gemini/mux" @@ -38,27 +36,6 @@ func configure() athenaConfig { return config } -func handleLookup(w gemini.ResponseWriter, r *gemini.Request) { - const URLTemplate string = "https://api.dictionaryapi.dev/api/v2/entries/en_US/%s" - // TODO: Sanitize input - word := r.URL.RawQuery - log.Printf("Looking up \"%s\"\n", word) - - resp, err := http.Get(fmt.Sprintf(URLTemplate, word)) - if err != nil { - log.Fatalln(err) - } - // Why is the body the stream? - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatalln(err) - } - - w.Write(definitionToPage(body)) -} - func handleRoot(w gemini.ResponseWriter, r *gemini.Request) { page := newAthenaDocument() page.AddLine("Search for a word to continue") @@ -69,14 +46,25 @@ func handleRoot(w gemini.ResponseWriter, r *gemini.Request) { w.Write(rawPage) } +// routes is a wrapper to map the route strings to their respective functions. +func routes() map[string]gemini.HandlerFunc { + lookupHandler := gemini.RequireInputHandler(gemini.HandlerFunc(handleLookup), "Enter seach word") + return map[string]gemini.HandlerFunc{ + // Add new routes here + "/": gemini.HandlerFunc(handleRoot), + "/lookup": lookupHandler.(gemini.HandlerFunc), + } +} + func main() { + // Configure app cfg := configure() - lookupHandler := gemini.RequireInputHandler(gemini.HandlerFunc(handleLookup), "Enter seach word") - rootHandler := gemini.HandlerFunc(handleRoot) + // Initialize routes router := mux.NewMux() - router.AddRoute("/lookup", lookupHandler) - router.AddRoute("/", rootHandler) + for route, handler := range routes() { + router.AddRoute(route, handler) + } ctx := context.Background() domainHandler, err := gemini.NewDomainHandlerFromFiles(cfg.domain, cfg.certPath, cfg.keyPath, router) @@ -84,11 +72,6 @@ func main() { log.Fatal("Error creating domain handler:", err) } - if err != nil { - log.Fatal("error creating domain handler B:", err) - } - - // Start the server for two domains (a.gemini / b.gemini). addr := fmt.Sprintf(":%d", cfg.port) err = gemini.ListenAndServe(ctx, addr, domainHandler) if err != nil { -- 2.45.2