diff --git a/main.go b/main.go new file mode 100644 index 0000000000000000000000000000000000000000..e0f3c3b40565626a633062c665e9b417561bf109 --- /dev/null +++ b/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "html" + "log" + "net/http" + "os" +) + +//Server that prints halli + +func helloHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) +} + +func main() { + port := os.Getenv("PORT") + port = "8080" + + if port == "" { + log.Fatal("$PORT must be set") + } + + //This is code for tsting receiving get req + //From YT:Simple GET req example + //Get country by name + country := getCountryName() + fmt.Println("Country name is s%",country) + + + http.HandleFunc("/", helloHandler) + + log.Fatal(http.ListenAndServe(":"+port, nil)) +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000000000000000000000000000000000000..e3825b40ebf0d96082d255428ede4793768fe53f --- /dev/null +++ b/main_test.go @@ -0,0 +1,9 @@ +package main + +/*func TestMain(t *testing.T) { + want := "Hello, world." + if got := Hello(); got != want { + t.Errorf("Hello() = %q, want %q", got, want) + } +} +*/ diff --git a/webapp.go b/webapp.go new file mode 100644 index 0000000000000000000000000000000000000000..40087adf58a7af87ad8176165828c0df1c18ee00 --- /dev/null +++ b/webapp.go @@ -0,0 +1,47 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" +) + +const apiRoot = "https://restcountries.eu/rest/v2/alpha/" + +//We receive JSON code and we parse it +//CntrInfo contains a response for restcountries endpoint +type CntrInfo struct { + CallingCode int32 //"47" + Name string `json:"name"` //"country":"norway" + NameCode string //"NO" + +} +//Should return the name Norway +func getCountryName() string { + //Change hardcoded to func parameters "We pass them" + countryCode := "NO" + + url := apiRoot + countryCode + + //resp is json, so we need to parse it + resp, err := http.Get(url) + + + + if err !=nil { + fmt.Println("Error:we have a problem", err) + return "" + } + //Closes when surrounded functions are finished + defer resp.Body.Close() + + //Pointer to struct + ci := &CntrInfo{} + + //resp.Body JSON + //NewDecoder returns a new decoder that reads from r + json.NewDecoder(resp.Body).Decode(ci) + + + return "ci.Name" +}