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

import (
Aksel Baardsen's avatar
Aksel Baardsen committed
4
	"assignment-1/pkg"
Aksel Baardsen's avatar
Aksel Baardsen committed
5
	"encoding/json"
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
6
	"fmt"
7
	"github.com/gorilla/mux"
8
	"log"
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
9
10
11
12
13
	"net/http"
)

func main() {

Aksel Baardsen's avatar
Aksel Baardsen committed
14

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

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	c.Path("/country/{country_identifier:[a-zA-Z][a-zA-Z]}").
		Queries("limit", "{limit}").
		HandlerFunc(cHandler).
		Name("Handel")
	c.Path("/country/{country_identifier:[a-zA-Z][a-zA-Z]}").
		HandlerFunc(cHandler)

	c.Path("/species/{speciesKey}").
		HandlerFunc(sHandler)

	c.Path("/diag/").
		HandlerFunc(dHandler)


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

Aksel Baardsen's avatar
works    
Aksel Baardsen committed
36
	http.Handle("/", r)
37
	log.Fatal(http.ListenAndServe(":5000", nil))
Aksel Baardsen's avatar
works    
Aksel Baardsen committed
38
39

}
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
40

Aksel Baardsen's avatar
Aksel Baardsen committed
41
42
43
44
45
/*
func helloHandle(w http.ResponseWriter, r *http.Request) {
	_, _ = fmt.Fprint(w, "Heisann!")
}
*/
46
47

func dHandler(w http.ResponseWriter, r *http.Request) {
Aksel Baardsen's avatar
Aksel Baardsen committed
48
49
50
51
52
53
54
	//_, _ = fmt.Fprintln(w, "aiaiai")

	var d pkg.Diag
	pkg.GetDiag(&d)

	marsh, _ := json.Marshal(d)
	fmt.Fprintln(w, string(marsh))
55

Aksel Baardsen's avatar
works    
Aksel Baardsen committed
56
}
Aksel Baardsen's avatar
fix  
Aksel Baardsen committed
57

58
func cHandler(w http.ResponseWriter, r *http.Request) {
59
	vars := mux.Vars(r)
60
61
62
63
	limit := r.FormValue("limit")
	if len(limit) == 0 {
		limit = "5"
	}
64

Aksel Baardsen's avatar
Aksel Baardsen committed
65
	country := pkg.GetCountryByCode(vars["country_identifier"], limit)
66

67
	c, _ := json.Marshal(country)
Aksel Baardsen's avatar
oops    
Aksel Baardsen committed
68
	fmt.Fprintln(w, string(c))
69

Aksel Baardsen's avatar
works    
Aksel Baardsen committed
70
}
71
72

func sHandler(w http.ResponseWriter, r *http.Request) {
Aksel Baardsen's avatar
Aksel Baardsen committed
73
74
75
76
	//_, _ = fmt.Fprintln(w, "aiaiai")

	vars := mux.Vars(r)

Aksel Baardsen's avatar
Aksel Baardsen committed
77
	specie := pkg.GetSpecies(vars["speciesKey"])
Aksel Baardsen's avatar
Aksel Baardsen committed
78

79
80
	s, _ := json.Marshal(specie)
	fmt.Fprintln(w, string(s))
81
82

}