forked from ekelen/tarot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (109 loc) · 3.59 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strings"
"math/rand"
"strconv"
"time"
"github.com/gorilla/mux"
)
type Card struct {
Type string `json:"type"`
NameShort string `json:"name_short"`
Name string `json:"name"`
Value string `json:"value"`
ValueInt int `json:"value_int"`
MeaningUp string `json:"meaning_up"`
MeaningRev string `json:"meaning_rev"`
Desc string `json:"desc"`
}
type Cards struct {
Nhits int `json:"nhits"`
Cards []Card `json:"cards"`
}
var cards Cards
func init() {
absPath, _ := filepath.Abs("static/card_data.json")
file, err := ioutil.ReadFile(absPath)
if err != nil {
log.Fatalf("File not found: %v", err)
}
err = json.Unmarshal(file, &cards)
if err != nil {
log.Fatalf("Failed to unmarshal: %v", err)
}
}
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", Index)
router.HandleFunc("/api/v1/cards", CardsIndex)
router.HandleFunc("/api/v1", CardsIndex)
router.HandleFunc("/api/v1/cards/random", func(w http.ResponseWriter, r *http.Request) {
r = mux.SetURLVars(r, map[string]string{"n": "1"})
RandomCards(w, r)
})
router.HandleFunc("/api/v1/cards/{name_short}", CardShow)
router.HandleFunc("/api/v1/cards/search/q={q}", CardSearch)
router.HandleFunc("/api/v1/cards/search/meaning={meaning}", CardSearch)
router.HandleFunc("/api/v1/cards/search/meaning_rev={meaning_rev}", CardSearch)
router.HandleFunc("/api/v1/cards/random/n={n:[0-9]+}", RandomCards)
staticFileDirectory := http.Dir("./static/")
staticFileHandler := http.StripPrefix("/static/", http.FileServer(staticFileDirectory))
router.PathPrefix("/static/").Handler(staticFileHandler).Methods("GET")
log.Println("http://localhost:8080/")
log.Fatal(http.ListenAndServe(":8080", router))
}
func Index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/index.html")
}
func CardsIndex(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(cards)
}
func CardShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
nameShort := vars["name_short"]
for _, card := range cards.Cards {
if card.NameShort == nameShort {
json.NewEncoder(w).Encode(card)
return
}
}
http.Error(w, "Card not found", http.StatusNotFound)
}
func CardSearch(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
meaning := strings.ToLower(vars["meaning"])
q := strings.ToLower(vars["q"])
meaning_rev := strings.ToLower(vars["meaning_rev"])
var results []Card
for _, card := range cards.Cards {
if meaning != "" && (strings.Contains(strings.ToLower(card.MeaningUp), meaning) || strings.Contains(strings.ToLower(card.MeaningRev), meaning)) {
results = append(results, card)
} else if q != "" && (strings.Contains(strings.ToLower(card.Name), q) || strings.Contains(strings.ToLower(card.MeaningRev), q)) {
results = append(results, card)
} else if meaning_rev != "" && strings.Contains(strings.ToLower(card.MeaningRev), meaning_rev) {
results = append(results, card)
}
}
if len(results) == 0 {
http.Error(w, "Card not found", http.StatusNotFound)
} else {
json.NewEncoder(w).Encode(results)
}
}
func RandomCards(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
nStr := vars["n"]
n, err := strconv.Atoi(nStr)
if err != nil || n <= 0 || n > len(cards.Cards) {
http.Error(w, "Invalid value for n", http.StatusBadRequest)
return
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(cards.Cards), func(i, j int) { cards.Cards[i], cards.Cards[j] = cards.Cards[j], cards.Cards[i] })
json.NewEncoder(w).Encode(cards.Cards[:n])
}