forked from davemachado/public-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
102 lines (96 loc) · 2.96 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
package main
import (
"encoding/json"
"io"
"net/http"
"github.com/gorilla/schema"
)
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
Description string
Auth string
HTTPS bool
Cors string
Link string
Category string
}
)
// 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
}
var err error
searchReq := new(SearchRequest)
// Only check query parameters if the request's Body is not nil
if req.Body != nil {
// Decode incoming search request off the query parameters map.
err = schema.NewDecoder().Decode(searchReq, req.URL.Query())
if err != nil {
http.Error(w, "server failed to parse request: "+err.Error(), http.StatusBadRequest)
return
}
defer req.Body.Close()
}
var results []Entry
for _, e := range apiList.Entries {
if checkEntryMatches(e, searchReq) {
results = append(results, e)
}
}
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
}
})
}
// 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}`)
})
}