summaryrefslogtreecommitdiff
path: root/main.go
blob: 1b5dd5bef97d024b5bc1be3c5e9a97dd82b9f26b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import (
	"encoding/json"
	"github.com/gorilla/mux"
	"log"
	"net/http"
)

func handleEntry(w http.ResponseWriter, r *http.Request) {
	var entry Entry

	err := json.NewDecoder(r.Body).Decode(&entry)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	err = InsertEntry(&entry)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

func handleRequests() {
	router := mux.NewRouter()
	router.HandleFunc("/entry", handleEntry).Methods("POST")

	log.Fatal(http.ListenAndServe(":8080", router))
}

func main() {
	log.Println("Starting up")
	InitEntries()
	defer CloseEntries()
	handleRequests()
}