Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
oblig1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Vladimir Haug
oblig1
Commits
35d288ad
Commit
35d288ad
authored
4 years ago
by
Vladimir Haug
Browse files
Options
Downloads
Patches
Plain Diff
Delete v1
parent
80c4d29e
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
v1
+0
-129
0 additions, 129 deletions
v1
with
0 additions
and
129 deletions
v1
deleted
100644 → 0
+
0
−
129
View file @
80c4d29e
package main
import (
"fmt"
"html"
"log"
"net/http"
"os"
"io/ioutil"
"strings"
"encoding/json"
// "time"
)
const COUNTRIES = "https://restcountries.eu/rest/v2/alpha/"
const SPECIES = "http://api.gbif.org/v1/species/"
const OCCURENCE = "http://api.gbif.org/v1/occurrence/search?publishingCountry="
type Country struct {
Code string `json:"alpha2Code"`
CountryName string `json:"name"`
CountryFlag string `json:"flag"`
Species string `json:"species"`
SpeciesKey string `json:"speciesKey"`
}
type Species struct {
Key int `json:"key"`
Kingdom string `json:"kingdom"`
Phylum string `json:"phylum"`
Family string `json:"family"`
Genus string `json:"genus"`
ScientificName string `json:"scientificName"`
CanonicalName string `json:"canonicalName"`
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}
func countryHandler(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/conservation/v1/country/")
var1 := parts[1]
url := COUNTRIES + var1
response, err := http.Get(url)
body, readErr := ioutil.ReadAll(response.Body)
if response.StatusCode >= 400 && response.StatusCode <= 499 {
fmt.Fprintf(w, "Country code not found")
}
if readErr != nil {
log.Fatal(readErr)
}
Country1 := Country{}
jsonErr := json.Unmarshal(body, &Country1)
if jsonErr != nil {
log.Fatal(jsonErr)
}
fmt.Fprintf(w, Country1.CountryName + "\n" + Country1.Code + "\n" + Country1.CountryFlag + "\n" + Country1.Species + "\n")
if err != nil {
fmt.Fprintf(w, "Dårlig")
} else {
cData, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(cData))
}
}
func speciesHandler(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/conservation/v1/species/")
// fmt.Fprintf(w, "Se om det funker", parts)
var1 := parts[1]
url := SPECIES + var1
response, err := http.Get(url)
body, readErr := ioutil.ReadAll(response.Body)
if response.StatusCode >= 400 && response.StatusCode <= 499 {
fmt.Fprintf(w, "Species code not found")
}
if readErr != nil {
log.Fatal(readErr)
}
Species1 := Species{}
jsonErr := json.Unmarshal(body, &Species1)
if jsonErr != nil {
log.Fatal(jsonErr)
}
fmt.Fprintf(w, "%d" + "\n" + Species1.Kingdom + "\n" + Species1.Phylum + "\n" + Species1.Family + "\n" + Species1.Genus + "\n" + Species1.ScientificName + "\n" + Species1.CanonicalName, Species1.Key)
if err != nil {
fmt.Println("Dårlig")
} else {
cData, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(cData))
}
}
func diagHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Her blir det tull", html.EscapeString(r.URL.Path))
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
http.HandleFunc("/", helloHandler)
http.HandleFunc("/conservation/v1/country/", countryHandler)
http.HandleFunc("/conservation/v1/species/", speciesHandler)
http.HandleFunc("/conservation/v1/diag/", diagHandler)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment