main.go 1.13 KB
Newer Older
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
1
2
3
package main

import (
4
	"assignment-1/models"
Aksel Baardsen's avatar
Aksel Baardsen committed
5
	"encoding/json"
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
6
	"fmt"
7
	"log"
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
8
	"net/http"
Aksel Baardsen's avatar
works    
Aksel Baardsen committed
9
10

	"github.com/gorilla/mux"
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
11
12
13
14
)

func main() {

Aksel Baardsen's avatar
works    
Aksel Baardsen committed
15
	r := mux.NewRouter()
16
17
	c := r.PathPrefix("/conservation/v1").Methods("GET").Subrouter()

Aksel Baardsen's avatar
Aksel Baardsen committed
18
19
	c.HandleFunc("/country/{country_identifier:[a-zA-Z][a-zA-Z]}", cHandler)
	c.HandleFunc("/species/{speciesKey}", sHandler)
20
	c.HandleFunc("/diag/", dHandler)
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
21

Aksel Baardsen's avatar
works    
Aksel Baardsen committed
22
	http.Handle("/", r)
23
	log.Fatal(http.ListenAndServe(":5000", nil))
Aksel Baardsen's avatar
works    
Aksel Baardsen committed
24
25

}
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
26

Aksel Baardsen's avatar
Aksel Baardsen committed
27
28
29
30
31
/*
func helloHandle(w http.ResponseWriter, r *http.Request) {
	_, _ = fmt.Fprint(w, "Heisann!")
}
*/
32
33

func dHandler(w http.ResponseWriter, r *http.Request) {
34
	_, _ = fmt.Fprintln(w, "aiaiai")
35

Aksel Baardsen's avatar
works    
Aksel Baardsen committed
36
}
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
37

38
func cHandler(w http.ResponseWriter, r *http.Request) {
39
40
	vars := mux.Vars(r)

41
	c := models.GetCountryByCode(vars["country_identifier"])
42

43
	fmt.Fprintln(w, c)
44
45
46
47

	//country := models.GetCountryByCode(vars["country_identifier"])

	//_, _ = fmt.Fprintln(w, country)
48

Aksel Baardsen's avatar
works    
Aksel Baardsen committed
49
}
50
51

func sHandler(w http.ResponseWriter, r *http.Request) {
Aksel Baardsen's avatar
Aksel Baardsen committed
52
53
54
55
56
57
58
59
60
	//_, _ = fmt.Fprintln(w, "aiaiai")

	vars := mux.Vars(r)

	s := models.GetSpecies(vars["speciesKey"])

	fmt.Fprintln(w, s) //plain keys

	fmt.Fprintln(w, json.NewEncoder(w).Encode(s))
61
62

}