-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·104 lines (88 loc) · 2.69 KB
/
main.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"fmt"
"gochess/chessBoard"
"io/fs"
"net/http"
"path/filepath"
"slices"
"strings"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
var allTemplates *template.Template
func ParseAllTemplates(location string) error {
allTemplates = template.New("").Funcs(sprig.FuncMap())
return filepath.WalkDir(location, func(path string, _ fs.DirEntry, _ error) error {
if strings.HasSuffix(path, ".html") {
_, err := allTemplates.New("").Funcs(sprig.FuncMap()).ParseFiles(path)
return err
}
return nil
})
}
func main() {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(middleware.Timeout(30 * time.Second))
err := ParseAllTemplates("templates")
if err != nil {
fmt.Println("Template parsing failed", err)
}
newBoard := chessBoard.New()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "text/html")
allTemplates.ExecuteTemplate(w, "Main", map[string]any{"board": newBoard.GetRepresentationalSquares()})
})
var highlighted *chessBoard.Square
r.Post("/move/{square}", func(w http.ResponseWriter, r *http.Request) {
squareId := chi.URLParam(r, "square")
fmt.Println(r.Body)
defer r.Body.Close()
if len(squareId) != 2 {
w.WriteHeader(http.StatusInternalServerError)
return
}
ri, ci := int(squareId[1]-'1'), int(squareId[0]-'a')
square := newBoard.GetSquare(ri, ci)
if square == nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Invalid square"))
return
}
err = newBoard.MakeMove(highlighted, square)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
allTemplates.ExecuteTemplate(w, "Main", map[string]any{"board": newBoard.GetRepresentationalSquares()})
})
r.Post("/highlight/{square}", func(w http.ResponseWriter, r *http.Request) {
squareId := chi.URLParam(r, "square")
fmt.Println(r.Body)
defer r.Body.Close()
if len(squareId) != 2 {
w.WriteHeader(http.StatusInternalServerError)
return
}
ri, ci := int(squareId[1]-'1'), int(squareId[0]-'a')
square := newBoard.GetSquare(ri, ci)
if square == nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
highlighted = square
// hx-include will allow replacing only highlighted and to-be-highlighted squares but I couldn't get it working
for i := range newBoard.Squares {
for j := range newBoard.Squares[i] {
allTemplates.ExecuteTemplate(w, "Square", map[string]any{"data": newBoard.Squares[i][j], "highlight": slices.Contains(square.LegalMoves, &newBoard.Squares[i][j])})
}
}
})
http.ListenAndServe("127.0.0.1:8080", r)
}