-
Notifications
You must be signed in to change notification settings - Fork 1
/
character.go
41 lines (35 loc) · 1.04 KB
/
character.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
package lodestone
import (
"encoding/json"
"log"
"strconv"
"net/http"
"fmt"
)
// LodestoneCharacter fetches a Character by ID.
func LodestoneCharacter(w http.ResponseWriter, r *http.Request) {
// Comment this to disable CORS
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Methods", "GET")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Max-Age", "3600")
w.WriteHeader(http.StatusNoContent)
return
}
id, err := strconv.ParseUint(r.URL.Query().Get("id"), 10, 32)
if err != nil {
log.Fatalln(err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
c, err := scraper.FetchCharacter(uint32(id))
if err != nil {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
log.Fatalln(err)
}
cJSON, err := json.Marshal(c)
if err != nil {
log.Fatalln(err)
}
fmt.Fprint(w, string(cJSON))
}