Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Start with a base image containing golang runtime
FROM golang:1.20

# Set the working directory in the container
WORKDIR /go/src/app

# Copy the go.mod and go.sum files
COPY go.mod go.sum ./

# Download the dependencies
RUN go mod download

# Copy the current directory contents into the container at /go/src/app
COPY ../ .

EXPOSE 8080
EXPOSE 8000
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3.9"
services:
godip-development:
build:
context: .
dockerfile: .docker/Dockerfile
container_name: godip-development
ports:
- "8080:8080"
- "8000:8000"
volumes:
- .:/go/src/app
tty: true
87 changes: 87 additions & 0 deletions gae/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

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

func resolve(w http.ResponseWriter, r *http.Request) {
corsHeaders(w)

variantName := mux.Vars(r)["variant"]
variant, found := variants.Variants[variantName]
if !found {
Expand Down Expand Up @@ -51,8 +53,55 @@ func resolve(w http.ResponseWriter, r *http.Request) {
}
}

func resolveWithOptions(w http.ResponseWriter, r *http.Request) {
corsHeaders(w)

variantName := mux.Vars(r)["variant"]
variant, found := variants.Variants[variantName]
if !found {
http.Error(w, fmt.Sprintf("Variant %q not found", variantName), 404)
return
}
p := &Phase{}
if err := json.NewDecoder(r.Body).Decode(p); err != nil {
http.Error(w, err.Error(), 400)
return
}
state, err := p.State(variant)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
if err = state.Next(); err != nil {
http.Error(w, err.Error(), 500)
return
}

nextPhase := NewPhase(state)

options := map[godip.Nation]godip.Options{}
for _, nation := range state.Graph().Nations() {
options[nation] = state.Phase().Options(state, nation)
}

response := struct {
Phase *Phase `json:"phase"`
Options map[godip.Nation]godip.Options `json:"options"`
}{
Phase: nextPhase,
Options: options,
}

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err = json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), 500)
return
}
}

func start(w http.ResponseWriter, r *http.Request) {
corsHeaders(w)

variantName := mux.Vars(r)["variant"]
variant, found := variants.Variants[variantName]
if !found {
Expand All @@ -65,13 +114,49 @@ func start(w http.ResponseWriter, r *http.Request) {
return
}
phase := NewPhase(state)

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err = json.NewEncoder(w).Encode(phase); err != nil {
http.Error(w, err.Error(), 500)
return
}
}

func startWithOptions(w http.ResponseWriter, r *http.Request) {
corsHeaders(w)

variantName := mux.Vars(r)["variant"]
variant, found := variants.Variants[variantName]
if !found {
http.Error(w, fmt.Sprintf("Variant %q not found", variantName), 404)
return
}
state, err := variant.Start()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
phase := NewPhase(state)

options := map[godip.Nation]godip.Options{}
for _, nation := range state.Graph().Nations() {
options[nation] = state.Phase().Options(state, nation)
}
response := struct {
Phase *Phase `json:"phase"`
Options map[godip.Nation]godip.Options `json:"options"`
}{
Phase: phase,
Options: options,
}

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err = json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), 500)
return
}
}

func listVariants(w http.ResponseWriter, r *http.Request) {
corsHeaders(w)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
Expand All @@ -86,7 +171,9 @@ func main() {
r.Methods("OPTIONS").HandlerFunc(preflight)
variants := r.Path("/{variant}").Subrouter()
variants.Methods("POST").HandlerFunc(resolve)
variants.Path("/resolve-with-options").Methods("POST").HandlerFunc(resolveWithOptions)
variants.Methods("GET").HandlerFunc(start)
r.Path("/start-with-options/{variant}").Methods("GET").HandlerFunc(startWithOptions)
r.Path("/").HandlerFunc(listVariants)
http.Handle("/", r)
appengine.Main()
Expand Down
Loading