-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
43 lines (35 loc) · 1.07 KB
/
search.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
package lodestone
import (
"encoding/json"
"log"
"net/http"
"fmt"
"github.com/xivapi/godestone/v2"
)
// SearchCharacter searches a character by name and returns a list of results.
func LodestoneCharacterSearch(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
}
name := r.URL.Query().Get("name")
server := r.URL.Query().Get("server")
if name == "" || server == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
opts := godestone.CharacterOptions{
Name: name,
World: server,
}
characters := scraper.SearchCharacters(opts)
cJSON, err := json.Marshal(characters)
if err != nil {
log.Fatalln(err)
}
fmt.Fprint(w, string(cJSON))
}