Skip to content

Commit a366e16

Browse files
authored
Merge pull request #189 from zond/johnpooch/add-new-endpoints-including-options
Add new endpoints to include options
2 parents 25a3554 + 6e0fa69 commit a366e16

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

.docker/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Start with a base image containing golang runtime
2+
FROM golang:1.20
3+
4+
# Set the working directory in the container
5+
WORKDIR /go/src/app
6+
7+
# Copy the go.mod and go.sum files
8+
COPY go.mod go.sum ./
9+
10+
# Download the dependencies
11+
RUN go mod download
12+
13+
# Copy the current directory contents into the container at /go/src/app
14+
COPY ../ .
15+
16+
EXPOSE 8080
17+
EXPOSE 8000

docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: "3.9"
2+
services:
3+
godip-development:
4+
build:
5+
context: .
6+
dockerfile: .docker/Dockerfile
7+
container_name: godip-development
8+
ports:
9+
- "8080:8080"
10+
- "8000:8000"
11+
volumes:
12+
- .:/go/src/app
13+
tty: true

gae/router.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77

88
"github.com/gorilla/mux"
9+
"github.com/zond/godip"
910
"github.com/zond/godip/variants"
1011
"google.golang.org/appengine"
1112
)
@@ -22,6 +23,7 @@ func preflight(w http.ResponseWriter, r *http.Request) {
2223

2324
func resolve(w http.ResponseWriter, r *http.Request) {
2425
corsHeaders(w)
26+
2527
variantName := mux.Vars(r)["variant"]
2628
variant, found := variants.Variants[variantName]
2729
if !found {
@@ -51,8 +53,55 @@ func resolve(w http.ResponseWriter, r *http.Request) {
5153
}
5254
}
5355

56+
func resolveWithOptions(w http.ResponseWriter, r *http.Request) {
57+
corsHeaders(w)
58+
59+
variantName := mux.Vars(r)["variant"]
60+
variant, found := variants.Variants[variantName]
61+
if !found {
62+
http.Error(w, fmt.Sprintf("Variant %q not found", variantName), 404)
63+
return
64+
}
65+
p := &Phase{}
66+
if err := json.NewDecoder(r.Body).Decode(p); err != nil {
67+
http.Error(w, err.Error(), 400)
68+
return
69+
}
70+
state, err := p.State(variant)
71+
if err != nil {
72+
http.Error(w, err.Error(), 400)
73+
return
74+
}
75+
if err = state.Next(); err != nil {
76+
http.Error(w, err.Error(), 500)
77+
return
78+
}
79+
80+
nextPhase := NewPhase(state)
81+
82+
options := map[godip.Nation]godip.Options{}
83+
for _, nation := range state.Graph().Nations() {
84+
options[nation] = state.Phase().Options(state, nation)
85+
}
86+
87+
response := struct {
88+
Phase *Phase `json:"phase"`
89+
Options map[godip.Nation]godip.Options `json:"options"`
90+
}{
91+
Phase: nextPhase,
92+
Options: options,
93+
}
94+
95+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
96+
if err = json.NewEncoder(w).Encode(response); err != nil {
97+
http.Error(w, err.Error(), 500)
98+
return
99+
}
100+
}
101+
54102
func start(w http.ResponseWriter, r *http.Request) {
55103
corsHeaders(w)
104+
56105
variantName := mux.Vars(r)["variant"]
57106
variant, found := variants.Variants[variantName]
58107
if !found {
@@ -65,13 +114,49 @@ func start(w http.ResponseWriter, r *http.Request) {
65114
return
66115
}
67116
phase := NewPhase(state)
117+
68118
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
69119
if err = json.NewEncoder(w).Encode(phase); err != nil {
70120
http.Error(w, err.Error(), 500)
71121
return
72122
}
73123
}
74124

125+
func startWithOptions(w http.ResponseWriter, r *http.Request) {
126+
corsHeaders(w)
127+
128+
variantName := mux.Vars(r)["variant"]
129+
variant, found := variants.Variants[variantName]
130+
if !found {
131+
http.Error(w, fmt.Sprintf("Variant %q not found", variantName), 404)
132+
return
133+
}
134+
state, err := variant.Start()
135+
if err != nil {
136+
http.Error(w, err.Error(), 500)
137+
return
138+
}
139+
phase := NewPhase(state)
140+
141+
options := map[godip.Nation]godip.Options{}
142+
for _, nation := range state.Graph().Nations() {
143+
options[nation] = state.Phase().Options(state, nation)
144+
}
145+
response := struct {
146+
Phase *Phase `json:"phase"`
147+
Options map[godip.Nation]godip.Options `json:"options"`
148+
}{
149+
Phase: phase,
150+
Options: options,
151+
}
152+
153+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
154+
if err = json.NewEncoder(w).Encode(response); err != nil {
155+
http.Error(w, err.Error(), 500)
156+
return
157+
}
158+
}
159+
75160
func listVariants(w http.ResponseWriter, r *http.Request) {
76161
corsHeaders(w)
77162
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
@@ -86,7 +171,9 @@ func main() {
86171
r.Methods("OPTIONS").HandlerFunc(preflight)
87172
variants := r.Path("/{variant}").Subrouter()
88173
variants.Methods("POST").HandlerFunc(resolve)
174+
variants.Path("/resolve-with-options").Methods("POST").HandlerFunc(resolveWithOptions)
89175
variants.Methods("GET").HandlerFunc(start)
176+
r.Path("/start-with-options/{variant}").Methods("GET").HandlerFunc(startWithOptions)
90177
r.Path("/").HandlerFunc(listVariants)
91178
http.Handle("/", r)
92179
appengine.Main()

0 commit comments

Comments
 (0)