-
Notifications
You must be signed in to change notification settings - Fork 5
/
handlers.go
149 lines (135 loc) · 3.69 KB
/
handlers.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// +build !tinygo
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/marianogappa/cheesse/api"
)
var a = api.New()
func handleServerDefaultGame(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(a.DefaultGame())
}
func handleCliDefaultGame() {
byts, _ := json.Marshal(a.DefaultGame())
fmt.Println(string(byts))
}
func handleServerParseGame(w http.ResponseWriter, r *http.Request) {
var ig api.InputGame
if err := json.NewDecoder(r.Body).Decode(&ig); err != nil {
fmt.Fprintln(w, formatError(err))
return
}
defer r.Body.Close()
outputGame, err := a.ParseGame(ig)
if err != nil {
fmt.Fprintln(w, formatError(err))
return
}
json.NewEncoder(w).Encode(outputGame)
}
func handleCliParseGame(flagParseGame *string) {
var ig api.InputGame
if err := json.Unmarshal([]byte(*flagParseGame), &ig); err != nil {
mustCliFatal(err)
}
outputGame, err := a.ParseGame(ig)
if err != nil {
mustCliFatal(err)
}
byts, _ := json.Marshal(outputGame)
fmt.Println(string(byts))
}
func handleServerDoAction(w http.ResponseWriter, r *http.Request) {
type args struct {
Game api.InputGame `json:"game"`
Action api.InputAction `json:"action"`
}
var input args
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
fmt.Fprintln(w, formatError(err))
return
}
defer r.Body.Close()
outputGame, outputAction, err := a.DoAction(input.Game, input.Action)
if err != nil {
fmt.Fprintln(w, formatError(err))
return
}
type out struct {
Game api.OutputGame `json:"game"`
Action api.OutputAction `json:"action"`
}
json.NewEncoder(w).Encode(out{outputGame, outputAction})
}
func handleCliDoAction(flagDoAction *string) {
type args struct {
Game api.InputGame `json:"game"`
Action api.InputAction `json:"action"`
}
var input args
if err := json.Unmarshal([]byte(*flagDoAction), &input); err != nil {
mustCliFatal(err)
}
outputGame, outputAction, err := a.DoAction(input.Game, input.Action)
if err != nil {
mustCliFatal(err)
}
type out struct {
Game api.OutputGame `json:"game"`
Action api.OutputAction `json:"action"`
}
byts, _ := json.Marshal(out{outputGame, outputAction})
fmt.Println(string(byts))
}
func handleServerParseNotation(w http.ResponseWriter, r *http.Request) {
type args struct {
Game api.InputGame `json:"game"`
NotationString string `json:"notationString"`
}
var input args
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
fmt.Fprintln(w, formatError(err))
return
}
defer r.Body.Close()
outputGame, outputGameSteps, err := a.ParseNotation(input.Game, input.NotationString)
if err != nil {
fmt.Fprintln(w, formatError(err))
return
}
type out struct {
Game api.OutputGame `json:"game"`
OutputGameSteps []api.OutputGameStep `json:"outputGameSteps"`
}
json.NewEncoder(w).Encode(out{outputGame, outputGameSteps})
}
func handleCliParseNotation(flagParseNotation *string) {
type args struct {
Game api.InputGame `json:"game"`
NotationString string `json:"notationString"`
}
var input args
if err := json.Unmarshal([]byte(*flagParseNotation), &input); err != nil {
mustCliFatal(err)
}
outputGame, outputGameSteps, err := a.ParseNotation(input.Game, input.NotationString)
if err != nil {
mustCliFatal(err)
}
type out struct {
Game api.OutputGame `json:"game"`
OutputGameSteps []api.OutputGameStep `json:"outputGameSteps"`
}
byts, _ := json.Marshal(out{outputGame, outputGameSteps})
fmt.Println(string(byts))
}
func mustCliFatal(err error) {
fmt.Println(formatError(err))
os.Exit(1)
}
func formatError(err error) string {
errByts, _ := json.Marshal(err.Error())
return fmt.Sprintf(`{"error": %v}`, string(errByts))
}