forked from davemachado/public-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
84 lines (76 loc) · 2.62 KB
/
util.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/gorilla/schema"
)
func encodeURL(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
encoded := strings.Replace(r.URL.String(), "%20&%20", "%20%26%20", -1)
r.URL, _ = url.Parse(encoded)
next(rw, r)
}
// getList initializes an Entries struct filled from the public-apis project
func getList(jsonFile string) {
file, err := os.OpenFile(jsonFile, os.O_RDONLY, 0644)
if err != nil {
panic("failed to open file: " + err.Error())
}
err = json.NewDecoder(file).Decode(&apiList)
if err != nil {
panic("failed to decode JSON from file: " + err.Error())
}
file.Close()
}
// getCategories initializes a string slice containing
// all unique categories from a given slice of Entries
func parseCategories(entries []Entry) []string {
var cats []string
set := make(map[string]struct{})
for _, entry := range entries {
if _, exists := set[entry.Category]; !exists {
cats = append(cats, entry.Category)
set[entry.Category] = struct{}{}
}
}
return cats
}
// checkEntryMatches checks if the given entry matches the given request's parameters.
// it returns true if the entry matches, and returns false otherwise.
func checkEntryMatches(entry Entry, request *SearchRequest) bool {
if strings.Contains(strings.ToLower(entry.API), strings.ToLower(request.Title)) &&
strings.Contains(strings.ToLower(entry.Description), strings.ToLower(request.Description)) &&
((strings.ToLower(request.Auth) == "null" && entry.Auth == "") || strings.Contains(strings.ToLower(entry.Auth), strings.ToLower(request.Auth))) &&
strings.Contains(strings.ToLower(entry.Cors), strings.ToLower(request.Cors)) &&
strings.Contains(strings.ToLower(entry.Category), strings.ToLower(request.Category)) {
if request.HTTPS == "" {
return true
}
if value, err := strconv.ParseBool(request.HTTPS); err == nil {
if entry.HTTPS == value {
return true
}
}
}
return false
}
// processSearchRequestToMatchingEntries decodes the request body into a SearchRequest struct that can
// be processed in a call to checkEntryMatches to return all matching entries.
func processSearchRequestToMatchingEntries(req *http.Request) ([]Entry, error) {
searchReq := new(SearchRequest)
// Decode incoming search request off the query parameters map.
if err := schema.NewDecoder().Decode(searchReq, req.URL.Query()); err != nil {
return nil, fmt.Errorf("server failed to parse request: %v", err)
}
var results []Entry
for _, e := range apiList.Entries {
if checkEntryMatches(e, searchReq) {
results = append(results, e)
}
}
return results, nil
}