-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (57 loc) · 2.85 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
package main
import (
"embed"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"log"
"math/rand"
"net/http"
"strings"
)
type Rejection struct {
Id string `json:"id"`
Message string `json:"message"`
}
var rejections []Rejection
var banner string = `
██████╗ ███████╗ ██╗███████╗ ██████╗████████╗██╗ ██████╗ ███╗ ██╗
██╔══██╗██╔════╝ ██║██╔════╝██╔════╝╚══██╔══╝██║██╔═══██╗████╗ ██║
██████╔╝█████╗ ██║█████╗ ██║ ██║ ██║██║ ██║██╔██╗ ██║
██╔══██╗██╔══╝ ██ ██║██╔══╝ ██║ ██║ ██║██║ ██║██║╚██╗██║
██║ ██║███████╗╚█████╔╝███████╗╚██████╗ ██║ ██║╚██████╔╝██║ ╚████║
╚═╝ ╚═╝╚══════╝ ╚════╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
█████╗ ██████╗ ██╗
██╔══██╗██╔══██╗██║
███████║██████╔╝██║
██╔══██║██╔═══╝ ██║
██║ ██║██║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝
`
//go:embed rejections.csv
var content embed.FS
func parse_csv() {
data, _ := content.ReadFile("rejections.csv") // read from embedded file
stringdata := string(data)
lines := strings.Split(stringdata, "\n")
for _, line := range lines {
id, message, _ := strings.Cut(line, ",") // split string on first instance of separator (comma).
message = strings.Replace(message, "\"", "", -1) // replace double quotes in the string with nothing, if the final int input is < 0 then it replaces an infinite amount of quotes.
rejections = append(rejections, Rejection{Id: id, Message: message})
}
}
func getRejection(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") // JSON header so it displays properly in browsers
random_message := rejections[rand.Intn(len(rejections))] // random index
json.NewEncoder(w).Encode(random_message) // JSON response
log.Println("Response: ", random_message)
return
}
func main() {
fmt.Println(banner)
parse_csv()
r := mux.NewRouter()
r.HandleFunc("/rejections", getRejection).Methods("GET")
fmt.Println("Listening for requests at http://localhost:80/rejections\n")
log.Fatal(http.ListenAndServe(":80", r))
}