~athorp96/athena

6d892347d125737029b39026658979bde5ff56cb — Andrew Thorp 3 years ago 4dc5444 main
Split out further, format
3 files changed, 47 insertions(+), 33 deletions(-)

M document.go
A lookup.go
M main.go
M document.go => document.go +1 -1
@@ 2,8 2,8 @@ package main

import (
	"bufio"
	"os"
	"log"
	"os"

	"github.com/a-h/gemini"
)

A lookup.go => lookup.go +31 -0
@@ 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))
}

M main.go => main.go +15 -32
@@ 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 {