CHANGE: added Cors
This commit is contained in:
@@ -29,23 +29,26 @@ func main() {
|
||||
}
|
||||
|
||||
func timeCreateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
setCors(w)
|
||||
switch r.Method {
|
||||
case "PUT":
|
||||
createBooking(w, r)
|
||||
case "OPTIONS":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func timeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if os.Getenv("DEBUG") == "true" {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
setCors(w)
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
getBookings(w, r)
|
||||
case "PUT":
|
||||
updateBooking(w, r)
|
||||
case "OPTIONS":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
@@ -65,6 +68,7 @@ func createBooking(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(booking)
|
||||
}
|
||||
@@ -98,29 +102,30 @@ func updateBooking(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Invalid bookingID query parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
booking, err := (*models.Booking).GetBookingById(nil, booking_id)
|
||||
_booking, err := (*models.Booking).GetBookingById(nil, booking_id)
|
||||
if err != nil {
|
||||
log.Println("Error getting booking: ", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if card_id := r.URL.Query().Get("cardID"); card_id != "" {
|
||||
booking.CardID = card_id
|
||||
var booking models.Booking
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
err = dec.Decode(&booking)
|
||||
if err != nil {
|
||||
log.Println("Error parsing booking: ", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if reader_id := r.URL.Query().Get("readerID"); reader_id != "" {
|
||||
booking.ReaderID = reader_id
|
||||
if booking.Id != 0 && booking.Id != _booking.Id {
|
||||
log.Println("Booking Ids do not match")
|
||||
http.Error(w, "Booking Ids do not match", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if _booking_type := r.URL.Query().Get("bookingType"); _booking_type != "" {
|
||||
booking_type, err := strconv.Atoi(_booking_type)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid booking_type query parameter", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
booking.BookingType = booking_type
|
||||
}
|
||||
booking.Save()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(booking)
|
||||
_booking.Update(booking)
|
||||
_booking.Save()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(_booking)
|
||||
}
|
||||
|
||||
// func getBooking(w http.ResponseWriter, r *http.Request)
|
||||
@@ -131,3 +136,11 @@ func getEnv(key, fallback string) string {
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func setCors(w http.ResponseWriter) {
|
||||
if os.Getenv("DEBUG") == "true" {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "*")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user