main
Bel LaPointe 2024-12-15 14:24:20 -07:00
parent bf3b341b69
commit ab3a549f78
1 changed files with 13 additions and 4 deletions

View File

@ -2,11 +2,11 @@ package main
import ( import (
"context" "context"
"crypto/rand"
"flag" "flag"
"fmt" "fmt"
"io" "io"
"log" "log"
"math/rand"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@ -72,6 +72,15 @@ func (s *S) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func (s *S) serveHTTP(w http.ResponseWriter, r *http.Request) error { func (s *S) serveHTTP(w http.ResponseWriter, r *http.Request) error {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "X-Auth-Token, content-type, Content-Type")
if r.Method == http.MethodOptions {
w.Header().Set("Content-Length", "0")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, TRACE, PATCH, HEAD, DELETE")
w.WriteHeader(http.StatusOK)
return nil
}
if isV1(r) || isWS(r) { if isV1(r) || isWS(r) {
return s.serveAPI(w, r) return s.serveAPI(w, r)
} }
@ -177,12 +186,12 @@ func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
switch r.Method + r.URL.Path { switch r.Method + r.URL.Path {
case "GET/v1/state/" + s.Session(ctx).ID: case "GET/v1/state/" + s.Session(ctx).ID:
if rand.Int()%2 == 0 { if rand.Int()%2 == 0 {
w.Write(`{"name": "foo"}`) w.Write([]byte(`{"name": "foo"}`))
} else { } else {
w.Write(`{"name": "bar", "party": "party name"}`) w.Write([]byte(`{"name": "bar", "party": "party name"}`))
} }
case "PUT/v1/state/" + s.Session(ctx).ID + "/party": case "PUT/v1/state/" + s.Session(ctx).ID + "/party":
w.Write(`{}`) w.Write([]byte(`{}`))
default: default:
http.NotFound(w, r) http.NotFound(w, r)
return nil return nil