From afe79e8430302808004ac2f5b5cc7d21d4d5d170 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Sat, 28 Sep 2024 03:31:28 -0400 Subject: Add simple http server --- bingo-factory.go | 19 ++++++++++++ net/net.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ templates/base.html | 11 +++++++ templates/bingo.html | 16 +++++++++++ 4 files changed, 127 insertions(+) create mode 100644 bingo-factory.go create mode 100644 net/net.go create mode 100644 templates/base.html create mode 100644 templates/bingo.html diff --git a/bingo-factory.go b/bingo-factory.go new file mode 100644 index 0000000..98422c2 --- /dev/null +++ b/bingo-factory.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "net/http" + + "github.com/jpappel/bingo-factory/net" +) + +func main() { + const HOSTNAME string = "" + const PORT int = 8080 + addr := fmt.Sprintf("%s:%d", HOSTNAME, PORT) + + mux := net.NewMux() + + fmt.Printf("Listening on %s\n", addr) + fmt.Print(http.ListenAndServe(addr, mux)) +} diff --git a/net/net.go b/net/net.go new file mode 100644 index 0000000..228d22d --- /dev/null +++ b/net/net.go @@ -0,0 +1,81 @@ +package net + +import ( + "fmt" + "html/template" + "net/http" + "os" + "slices" + + "github.com/jpappel/bingo-factory/bingo" +) + +func errLog(w http.ResponseWriter, err error, logInfo string, httpInfo string) { + fmt.Fprintln(os.Stderr, logInfo, err) + http.Error(w, httpInfo, http.StatusInternalServerError) +} + +func game(w http.ResponseWriter, req *http.Request) { + game := bingo.Game{ + Length: 3, + FreeSquare: false, + } + + tiles := make([]bingo.Tile, 9) + for i := range 9 { + tiles[i].Text = fmt.Sprintf("Tile %d", i) + } + + game.Board = tiles + + t, err := template.ParseFiles("templates/base.html", "templates/bingo.html") + if err != nil { + errLog(w, err, "parsing template", "parse error") + return + } + + rows := slices.Collect(game.Rows()) + + data := struct { + Game bingo.Game + Rows [][]bingo.Tile + }{ + Game: game, + Rows: rows, + } + + if err = t.Execute(w, data); err != nil { + errLog(w, err, "template execute failed", "execute error") + return + } +} + +func home(w http.ResponseWriter, req *http.Request) { + + t, err := template.ParseFiles("templates/base.html") + if err != nil { + errLog(w, err, "parsing base template: ", "template parse error") + return + } + + t.Parse(` +{{ define "content" }} +

Bingo Factory

+

We're currently under construction, please check back soon!

+{{ end }} + `) + + if err = t.Execute(w, nil); err != nil { + errLog(w, err, "executing template", "template execute error") + return + } +} + +func NewMux() *http.ServeMux { + mux := http.NewServeMux() + + mux.Handle("GET /", http.HandlerFunc(home)) + mux.Handle("GET /bingo", http.HandlerFunc(game)) + + return mux +} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..d263063 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,11 @@ + + + + + + {{ block "title" . }}Bingo Factory{{ end }} + + + {{ template "content" . }} + + diff --git a/templates/bingo.html b/templates/bingo.html new file mode 100644 index 0000000..e00543c --- /dev/null +++ b/templates/bingo.html @@ -0,0 +1,16 @@ +{{ define "title" }}Bingo{{ end }} +{{ define "content" }} + + + {{ range .Rows }} + + {{ range . }} + + {{ end }} + + {{ else }} + + {{ end }} + +
{{ .Text }}
No Rows!
+{{ end }} -- cgit v1.2.3