-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (70 loc) · 2.11 KB
/
Copy pathmain.go
File metadata and controls
80 lines (70 loc) · 2.11 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
)
type Magic struct {
Id int `json:"id"`
Reply string `json:"reply"`
Type string `json:"type"`
}
var MagicAnswers []Magic
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}
func setJson(w *http.ResponseWriter) {
(*w).Header().Set("Content-Type", "application/json")
}
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Println("/ reached")
fmt.Fprintf(w, "Magic 8-ball API!")
}
func returnRandomAnswer(w http.ResponseWriter, r *http.Request) {
randomIndex := rand.Intn(len(MagicAnswers))
fmt.Printf("/magic answered with %d: %s\n", randomIndex, MagicAnswers[randomIndex].Reply)
setJson(&w)
enableCors(&w)
json.NewEncoder(w).Encode(MagicAnswers[randomIndex])
}
func returnAnswers(w http.ResponseWriter, r *http.Request) {
fmt.Println("/completemagic answered")
setJson(&w)
enableCors(&w)
json.NewEncoder(w).Encode(MagicAnswers)
}
func handleRequests() {
http.HandleFunc("/", homePage)
http.HandleFunc("/magic", returnRandomAnswer)
http.HandleFunc("/completemagic", returnAnswers)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func main() {
MagicAnswers = []Magic{
{0, "It is certain.", "affirmative"},
{1, "It is decidedly so.", "affirmative"},
{2, "Without a doubt.", "affirmative"},
{3, "Yes – definitely.", "affirmative"},
{4, "You may rely on it.", "affirmative"},
{5, "As I see it, yes.", "affirmative"},
{6, "Most likely.", "affirmative"},
{7, "Outlook good.", "affirmative"},
{8, "Yes.", "affirmative"},
{9, "Signs point to yes.", "affirmative"},
{10, "Reply hazy, try again.", "neutral"},
{11, "Ask again later.", "neutral"},
{12, "Better not tell you now.", "neutral"},
{13, "Cannot predict now.", "neutral"},
{14, "Concentrate and ask again.", "neutral"},
{15, "Don't count on it.", "negative"},
{16, "My reply is no.", "negative"},
{17, "My sources say no.", "negative"},
{18, "Outlook not so good.", "negative"},
{19, "Very doubtful. ", "negative"},
{20, "Signs point to no.", "negative"},
}
fmt.Println("Started")
handleRequests()
}