diff options
-rw-r--r-- | main.go | 16 |
1 files changed, 14 insertions, 2 deletions
@@ -7,7 +7,18 @@ import ( "net/http" ) -func handleEntry(w http.ResponseWriter, r *http.Request) { +func handleEntryOptions(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "POST") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") + w.WriteHeader(http.StatusNoContent) +} + +func handleEntryPost(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "POST") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") + var entry Entry err := json.NewDecoder(r.Body).Decode(&entry) @@ -25,7 +36,8 @@ func handleEntry(w http.ResponseWriter, r *http.Request) { func handleRequests() { router := mux.NewRouter() - router.HandleFunc("/entry", handleEntry).Methods("POST") + router.HandleFunc("/entry", handleEntryPost).Methods("POST") + router.HandleFunc("/entry", handleEntryOptions).Methods("OPTIONS") log.Fatal(http.ListenAndServe(":8080", router)) } |