Select Git revision
-
Mathilde Hertaas authoredMathilde Hertaas authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
main.go 3.55 KiB
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
type Flags struct{
PNG string `json:"png"`
}
// Struktur for data fra REST Countries API
type CountryInfo struct {
Name struct {
Common string `json:"common"`
Official string `json:"official"`
} `json:"name"`
Capital []string `json:"capital"`
Languages map[string]string `json:"languages"`
Continents []string `json:"continents"`
Borders []string `json:"borders"`
Population int `json:"population"`
Flags Flags `json:"flags"`
Cities []string `json:"cities"`
}
// Struktur for kombinert respons
type CombinedInfo struct {
Name string `json:"name"`
Continents []string `json:"continenents"`
Population int `json:"population"`
Languages map[string]string `json:"languages"`
Borders []string `json:"borders"`
Flags string `json:"flags"`
Capital string `json:"capital"`
Cities string `json:"cities"`
}
// Henter landdata fra REST Countries API
func getCountryDataByCode(countryCode string) (*CountryInfo, error) {
url := fmt.Sprintf("http://129.241.150.113:8080/v3.1/alpha/%s", countryCode)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error retrieving country data, status code: %d", resp.StatusCode)
}
var countries []CountryInfo
if err := json.NewDecoder(resp.Body).Decode(&countries); err != nil {
return nil, err
}
if len(countries) == 0 {
return nil, fmt.Errorf("country not found for code: %s", countryCode)
}
return &countries[0], nil
}
// Handler som kombinerer data fra begge API-er
func countryInfoHandler(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 5 {
http.Error(w, "Invalid format. Use: /countryinfo/v1/info/{countryCode}", http.StatusBadRequest)
return
}
countryCode := parts[4]
// Hent data fra begge API-er
country, err := getCountryDataByCode(countryCode)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Bygg kombinert respons
response := CombinedInfo{
Name: country.Name.Common,
Continents: country.Continents,
Population: country.Population,
Languages: country.Languages,
Borders: country.Borders,
Flags: country.Flags.PNG,
Capital: "",
Cities: "",
}
if len(country.Capital) > 0 {
response.Capital = country.Capital[0]
}
if len(country.Cities) > 0 {
response.Cities = country.Cities[0]
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/countryinfo/v1/info/", countryInfoHandler)
fmt.Println("Server running on port 8080...")
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Printf("Server failed to start: %s\n", err)
}
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
response := map[string]interface{}{
"WELCOME": "Welcome to the Country Info API",
"endpoints": map[string]interface{}{
"Get Country Info": map[string]string{
"Description": "Get country details including cities, population, and more.",
"URL": "http://localhost:8080/countryinfo/v1/info/{country_code}",
"Example": "http://localhost:8080/countryinfo/v1/info/NO",
},
},
}
json.NewEncoder(w).Encode(response)
}