Skip to content
Snippets Groups Projects
Commit 815d8053 authored by andmag's avatar andmag
Browse files

main

parent 52a0c375
No related branches found
No related tags found
No related merge requests found
main.go 0 → 100644
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))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment