68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
PORT = "8080"
|
|
)
|
|
|
|
func handleGetPing(w http.ResponseWriter, r *http.Request) {
|
|
log.Printf("[*] Received request %v %v\n", r.Method, r.URL)
|
|
fmt.Fprintf(w, "PONG\n")
|
|
}
|
|
|
|
func handlePostSubmit(w http.ResponseWriter, r *http.Request) {
|
|
log.Printf("[*] Received request %v %v\n", r.Method, r.URL)
|
|
log.Printf("[*] %v\n", r.Body)
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin")
|
|
|
|
var choices [][]bool
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&choices)
|
|
if err != nil {
|
|
log.Printf("[!] Error decoding request Body %v, error: %s\n", r.Body, err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
}
|
|
|
|
log.Printf("TODO: Persist to database %v\n", choices)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func handleOptionsSubmit(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc(
|
|
"GET /api/ping",
|
|
handleGetPing,
|
|
)
|
|
|
|
http.HandleFunc(
|
|
"POST /api/submit",
|
|
handlePostSubmit,
|
|
)
|
|
|
|
http.HandleFunc(
|
|
"OPTIONS /api/submit",
|
|
handleOptionsSubmit,
|
|
)
|
|
|
|
log.Printf("[*] Datefinder server listening on :%s\n", PORT)
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
log.Fatalf("[!] Could not start server on %s, error: %s\n", PORT, err)
|
|
}
|
|
}
|