-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathserver.go
166 lines (127 loc) · 3.07 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"github.com/rs/cors"
"io/ioutil"
"log"
"net/http"
"os"
)
type Error struct {
Message string
}
type BankJSON struct {
Name string `json:"name"`
Slug string `json:"slug"`
Code string `json:"code"`
USSD string `json:"ussd"`
}
type Bank struct {
Name string `json:"name"`
Slug string `json:"slug"`
Code string `json:"code"`
USSD string `json:"ussd"`
Logo string `json:"logo"`
}
func main() {
if err := godotenv.Load(); err != nil {
log.Fatal("No .env file found")
}
port, exist := os.LookupEnv("PORT")
if !exist {
log.Fatal("PORT not set in .env")
}
host, exist := os.LookupEnv("HOST")
if !exist {
log.Fatal("HOST not set in .env")
}
bankJson, err := ioutil.ReadFile("./banks.json")
if err != nil {
log.Fatal(err)
}
var banks []BankJSON
if err := json.Unmarshal(bankJson, &banks); err != nil {
log.Fatal(err)
}
route := mux.NewRouter()
route.PathPrefix("/logo/").Handler(http.StripPrefix("/logo/", http.FileServer(http.Dir("./logos"))))
route.NotFoundHandler = http.HandlerFunc(notFound)
route.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
code := request.URL.Query().Get("code")
slug := request.URL.Query().Get("slug")
if code != "" || slug != "" {
// Perform an SQL "and" query to find the exact match
for _, bank := range banks {
if (code == "" || code == bank.Code) && (slug == "" || slug == bank.Slug) {
_ = json.NewEncoder(writer).Encode(Bank{
Name: bank.Name,
Slug: bank.Slug,
Code: bank.Code,
USSD: bank.USSD,
Logo: host + "/logo/" + getUrl(bank.Slug) + ".png",
})
return
}
}
_ = json.NewEncoder(writer).Encode(nil)
return
}
// No code and slug provided, return all banks
var newBanks []Bank
for _, bank := range banks {
newBanks = append(newBanks, Bank{
Name: bank.Name,
Slug: bank.Slug,
Code: bank.Code,
USSD: bank.USSD,
Logo: host + "/logo/" + getUrl(bank.Slug) + ".png",
})
}
_ = json.NewEncoder(writer).Encode(newBanks)
})
handler := cors.AllowAll().Handler(route)
if err := http.ListenAndServe(":"+port, handler); err != nil {
log.Fatal(err)
}
}
func notFound(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusNotFound)
writer.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(writer).Encode(Error{
Message: "endpoint not found",
})
}
func getUrl(slug string) string {
var files []string
f, err := os.Open("./logos")
if err != nil {
log.Fatal(err)
}
fileInfo, err := f.Readdir(0)
_ = f.Close()
if err != nil {
log.Fatal(err)
}
for _, file := range fileInfo {
if file.Name() == ".DS_Store" {
continue
}
files = append(files, file.Name())
}
_, found := find(files, slug+".png")
if found {
return slug
}
return "default-image"
}
func find(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}