aboutsummaryrefslogtreecommitdiffstats
path: root/net/net.go
blob: 228d22d5e73f939da52f4e0edf1f4de05d0e7a70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
}