-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
77 lines (62 loc) · 1.72 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
type Superhero struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Intelligence int `json:"intelligence"`
Strength int `json:"strength"`
Speed int `json:"speed"`
Durability int `json:"durability"`
Power int `json:"power"`
Combat int `json:"combat"`
Gender string `json:"gender"`
Race string `json:"race"`
Height string `json:"height"`
Weight string `json:"weight"`
FullName string `json:"fullName"`
PlaceOfBirth string `json:"placeOfBirth"`
FirstAppearance string `json:"firstAppearance"`
Publisher string `json:"publisher"`
Alignment string `json:"alignment"`
Sm string `json:"sm"`
}
var superheroes []Superhero
func handleError(err error) {
if err != nil {
log.Panic(err)
}
}
// all Get handlers
func getHeroes(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(superheroes)
}
func getJSON(jsonfile string) {
jsonFile, err := os.Open(jsonfile)
handleError(err)
// read our opened xml/json as a byte array.
byteValue, err := ioutil.ReadAll(jsonFile)
handleError(err)
err = json.Unmarshal(byteValue, &superheroes)
handleError(err)
fmt.Println(superheroes)
jsonFile.Close()
}
func main() {
r := mux.NewRouter()
getJSON("./superheroAPI.json")
fmt.Println()
//Route Handlers / Endpoints
r.HandleFunc("/api/superhero", getHeroes).Methods("GET")
fmt.Println("Server running on port 8080")
log.Fatal(http.ListenAndServe(":8080", r))
}