forked from davemachado/public-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
113 lines (107 loc) · 3.44 KB
/
handlers.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
package main
import (
"encoding/json"
"io"
"math/rand"
"net/http"
)
type (
// SearchRequest describes an incoming search request.
SearchRequest struct {
Title string `schema:"title"`
Description string `schema:"description"`
Auth string `schema:"auth"`
HTTPS string `schema:"https"`
Cors string `schema:"cors"`
Category string `schema:"category"`
}
// Entries contains an array of API entries, and a count representing the length of that array.
Entries struct {
Count int `json:"count"`
Entries []Entry `json:"entries"`
}
// Entry describes a single API reference.
Entry struct {
API string `json:"API"`
Description string `json:"Description"`
Auth string `json:"Auth"`
HTTPS bool `json:"HTTPS"`
Cors string `json:"Cors"`
Link string `json:"Link"`
Category string `json:"Category"`
}
)
// getEntriesHandler returns an Entries object with the matching entries filtered
// by the search request
func getEntriesHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
results, err := processSearchRequestToMatchingEntries(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
err = json.NewEncoder(w).Encode(Entries{
Count: len(results),
Entries: results,
})
if err != nil {
http.Error(w, "server failed to encode response object: "+err.Error(), http.StatusInternalServerError)
return
}
})
}
// getCategoriesHandler returns a string slice object with all unique categories
func getCategoriesHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
err := json.NewEncoder(w).Encode(categories)
if err != nil {
http.Error(w, "server failed to encode response object: "+err.Error(), http.StatusInternalServerError)
return
}
})
}
// getRandomHandler returns an Entries object containing a random element from the Entries slice
func getRandomHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
results, err := processSearchRequestToMatchingEntries(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
err = json.NewEncoder(w).Encode(Entries{
Count: 1,
Entries: []Entry{results[rand.Intn(len(results))]},
})
if err != nil {
http.Error(w, "server failed to encode response object: "+err.Error(), http.StatusInternalServerError)
return
}
})
}
// healthCheckHandler returns a simple indication on whether or not the core http service is running
func healthCheckHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
io.WriteString(w, `{"alive": true}`)
})
}