From 815d8053c2013206c4ad13d57f87a65c29b80371 Mon Sep 17 00:00:00 2001
From: andmag <andmag@stud.ntnu.no>
Date: Thu, 26 Sep 2019 13:19:31 +0200
Subject: [PATCH] main

---
 main.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 main.go

diff --git a/main.go b/main.go
new file mode 100644
index 0000000..2626d0f
--- /dev/null
+++ b/main.go
@@ -0,0 +1,69 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"log"
+	"net/http"
+	"os"
+	"strings"
+)
+
+// Coutry comment endpoint
+type Country struct {
+	Code        string `json:"alpha2Code"`
+	Countryname string `json:"name"`
+	Countryflag string `json:"flag"`
+	//Categorycodes string			`json:"category"`
+	//Speciescategorycount string 	`json:"`
+}
+
+func handlerNil(w http.ResponseWriter, r *http.Request) {
+	fmt.Println("Default Handler: Invalid request received.")
+	http.Error(w, "Invalid request", http.StatusBadRequest)
+}
+
+func handlerCountry(w http.ResponseWriter, r *http.Request) {
+
+	APIURL := "https://restcountries.eu/rest/v2/alpha/"
+
+	//SJEKK LENGDE 4
+
+	parts := strings.Split(r.URL.Path, "/")
+	APIURL += parts[4]
+
+	http.Header.Add(w.Header(), "content-type", "application/json")
+
+	req, err := http.NewRequest(http.MethodGet, APIURL, nil)
+	if err != nil {
+		fmt.Println("Error", err)
+	}
+
+	client := http.DefaultClient
+	resp, err := client.Do(req)
+	if err != nil {
+		fmt.Println("Error", err)
+	}
+
+	defer resp.Body.Close()
+	country := &Country{}
+
+	json.NewDecoder(resp.Body).Decode(country)
+
+	json.NewEncoder(w).Encode(country)
+
+}
+
+func main() {
+
+	port := os.Getenv("PORT")
+	if port == "" {
+		port = "8080"
+	}
+
+	http.HandleFunc("/", handlerNil)
+	http.HandleFunc("/conservation/v1/country/", handlerCountry)
+	fmt.Println("Listening on port " + port)
+	log.Fatal(http.ListenAndServe(":"+port, nil))
+
+}
-- 
GitLab