Archived
Compare commits
9
Commits
09462288b1
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9db57ad178 | ||
|
|
96798db689 | ||
|
|
12b5f9a75f | ||
|
|
366130e9f4 | ||
|
|
dcc58a331a | ||
|
|
3fa2760726 | ||
|
|
16a06ade68 | ||
|
|
82b78bc58c | ||
|
|
79f1771d35 |
@@ -5,6 +5,7 @@
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
main
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
@@ -4,14 +4,16 @@
|
||||
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
|
||||
<meta http-equiv="Pragma" content="no-cache" />
|
||||
<meta http-equiv="Expires" content="0" />
|
||||
<meta property="og:title" content="{{.Kamoji}}" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta property="og:description" content="{{.Kamoji}}" />
|
||||
<meta charset="UTF-8">
|
||||
<title>{{.Kamoji}}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<p style="text-align: center; padding: 50px 0; font-size: 5vw">
|
||||
{{.Kamoji}}
|
||||
</p>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
@@ -22,7 +22,7 @@ type Kamojis struct {
|
||||
|
||||
func loadKamojis(path string) Kamojis {
|
||||
kamojis := Kamojis{}
|
||||
|
||||
log.Println("load kamojis from " + path + ".")
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -33,35 +33,48 @@ func loadKamojis(path string) Kamojis {
|
||||
for scanner.Scan() {
|
||||
kamojis.Kamojis = append(kamojis.Kamojis, Kamoji{Kamoji: scanner.Text()})
|
||||
}
|
||||
|
||||
log.Println("kamojis loaded.")
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return kamojis
|
||||
}
|
||||
|
||||
func randNum(i int) int {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
return rand.Intn(i)
|
||||
}
|
||||
|
||||
func main() {
|
||||
port := flag.String("port", "80", "http listening port")
|
||||
kamojisPath := flag.String("kamojis", "kamojis.txt", "path to file with kamojis")
|
||||
templatePath := flag.String("template", "kamoji_template.html", "path to HTML template file")
|
||||
flag.Parse()
|
||||
|
||||
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
|
||||
|
||||
log.Println("parsing template file from " + *templatePath + ".")
|
||||
tmpl, err := template.ParseFiles(*templatePath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
allk := loadKamojis(*kamojisPath)
|
||||
|
||||
timestamp := time.Now().Unix()
|
||||
randomNumber := rand.Intn(len(allk.Kamojis))
|
||||
randomNumber := randNum(len(allk.Kamojis))
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if time.Now().Unix()-timestamp > 60 {
|
||||
randomNumber = rand.Intn(len(allk.Kamojis))
|
||||
randomNumber = randNum(len(allk.Kamojis))
|
||||
timestamp = time.Now().Unix()
|
||||
log.Println("rotating kamoji")
|
||||
log.Println("rotating kamoji.")
|
||||
}
|
||||
log.Println("served kamoji to " + r.RemoteAddr)
|
||||
log.Println("serving kamoji to " + r.Header.Get("x-forwarded-for") + ".")
|
||||
k := allk.Kamojis[randomNumber]
|
||||
tmpl.Execute(w, k)
|
||||
})
|
||||
|
||||
http.ListenAndServe(":"+*port, nil)
|
||||
log.Println("starting webserver on port " + *port + ". press ctrl-c to exit.")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user