-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
151 lines (119 loc) · 3.28 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/gorilla/mux"
)
func getFortune(filename string, fortunes int) string {
// filename : the file the fortune is being selected from
// fortunes : the total number of fortunes in the file
// Returns : the fortune in the form of a formatted string
if filename == "datfile/favicon.ico" {
return ""
}
file, err := os.Open(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
rand.Seed(time.Now().UnixNano())
b := rand.Intn(fortunes - 1)
fmt.Println("\nChose fortune", b, "of", fortunes, "total fortunes")
count := 0
fortune := ""
for scanner.Scan() {
if scanner.Text() != "%" && count == b {
fortune = fortune + scanner.Text() + "\n"
} else if scanner.Text() == "%" && (count > b) {
break
} else if scanner.Text() == "%" {
count++
}
}
return fortune
}
func getRandomFortune(w http.ResponseWriter, r *http.Request) {
// Selects a random datfile to return a random fortune (not including offensive)
// Returns : the fortune in a formatted string
var files []string
var datfiles []string
// Get list of all fortune files in datfiles
root := "datfiles"
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
files = append(files, path)
return nil
})
if err != nil {
panic(err)
}
for _, file := range files {
if !strings.Contains(file, "CMakeLists.txt") && !strings.Contains(file, "off") && !strings.Contains(file, "README.md") && file != "datfiles" {
datfiles = append(datfiles, file)
}
}
// Choose a random datfile
rand.Seed(time.Now().UnixNano())
a := rand.Intn(len(datfiles))
fmt.Println("\nChose datfile", a, "of", len(datfiles), "total datfiles")
// Select random file
randFile := datfiles[a]
fmt.Println("\ndatfile chosen:", randFile)
file, err := os.Open(randFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
fortunes := 0
for scanner.Scan() {
if scanner.Text() == "%" {
fortunes++
}
}
fortune := getFortune(randFile, fortunes)
json.NewEncoder(w).Encode(fortune)
}
func getSpecificFortuneType(w http.ResponseWriter, r *http.Request) {
// Get a specific genre of fortune; must be a file within datfiles
params := mux.Vars(r)
if params["genre"] == "favicon.ico" {
return
}
filePath := fmt.Sprintf("%s%s", "datfiles/", params["genre"])
file, err := os.Open(filePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
fortunes := 0
for scanner.Scan() {
if scanner.Text() == "%" {
fortunes++
}
}
fortune := getFortune(filePath, fortunes)
json.NewEncoder(w).Encode(fortune)
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", getRandomFortune).Methods("GET")
router.HandleFunc("/{genre}", getSpecificFortuneType).Methods("GET")
port := ":" + os.Getenv("PORT")
log.Fatal(http.ListenAndServe(port, router))
}