diff options
| -rw-r--r-- | bingo-factory.go | 19 | ||||
| -rw-r--r-- | net/net.go | 81 | ||||
| -rw-r--r-- | templates/base.html | 11 | ||||
| -rw-r--r-- | templates/bingo.html | 16 |
4 files changed, 127 insertions, 0 deletions
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" }} +<h1>Bingo Factory</h1> +<p>We're currently under construction, please check back soon!</p> +{{ 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 @@ +<!DOCTYPE html> +<html lang=en> + <head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>{{ block "title" . }}Bingo Factory{{ end }}</title> + </head> + <body> + {{ template "content" . }} + </body> +</html> 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" }} +<table> + <tbody> + {{ range .Rows }} + <tr> + {{ range . }} + <td class="{{ if .Checked }}checked{{ end }} {{ if not .Checkable }}locked{{ end }}">{{ .Text }}</td> + {{ end }} + </tr> + {{ else }} + <td>No Rows!</td> + {{ end }} + </tbody> +</table> +{{ end }} |
