Commit 7e2b24a5 authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

added handlers to separate folder

parent 7b24d460
package handler
import (
"assignment-1/pkg"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
)
func Chandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
limit := r.FormValue("limit")
if len(limit) == 0 {
limit = "5"
}
country := pkg.GetCountryByCode(vars["country_identifier"], limit)
c, _ := json.Marshal(country)
fmt.Fprintln(w, string(c))
}
\ No newline at end of file
package handler
import (
"assignment-1/pkg"
"encoding/json"
"fmt"
"net/http"
)
func Dhandler(w http.ResponseWriter, r *http.Request) {
var d pkg.Diag
pkg.GetDiag(&d)
marsh, _ := json.Marshal(d)
fmt.Fprintln(w, string(marsh))
}
package handler
import (
"assignment-1/pkg"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
)
func Shandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
specie := pkg.GetSpecies(vars["speciesKey"])
s, _ := json.Marshal(specie)
fmt.Fprintln(w, string(s))
}
package main
import (
"assignment-1/pkg"
"encoding/json"
"fmt"
"assignment-1/handler"
"github.com/gorilla/mux"
"log"
"net/http"
......@@ -20,48 +18,19 @@ func main() {
// two handlerpaths makes sure that the caller does not HAVE to specify limit
c.Path("/country/{country_identifier:[a-zA-Z][a-zA-Z]}").
Queries("limit", "{limit}").
HandlerFunc(cHandler).
HandlerFunc(handler.Chandler).
Name("Handel")
c.Path("/country/{country_identifier:[a-zA-Z][a-zA-Z]}").
HandlerFunc(cHandler)
HandlerFunc(handler.Chandler)
c.Path("/species/{speciesKey}").
HandlerFunc(sHandler)
HandlerFunc(handler.Shandler)
c.Path("/diag/").
HandlerFunc(dHandler)
HandlerFunc(handler.Dhandler)
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func dHandler(w http.ResponseWriter, r *http.Request) {
var d pkg.Diag
pkg.GetDiag(&d)
marsh, _ := json.Marshal(d)
fmt.Fprintln(w, string(marsh))
}
func cHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
limit := r.FormValue("limit")
if len(limit) == 0 {
limit = "5"
}
country := pkg.GetCountryByCode(vars["country_identifier"], limit)
c, _ := json.Marshal(country)
fmt.Fprintln(w, string(c))
}
func sHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
specie := pkg.GetSpecies(vars["speciesKey"])
s, _ := json.Marshal(specie)
fmt.Fprintln(w, string(s))
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment