Archived
Compare commits
11
Commits
4e401cbb7a
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9db57ad178 | ||
|
|
96798db689 | ||
|
|
12b5f9a75f | ||
|
|
366130e9f4 | ||
|
|
dcc58a331a | ||
|
|
3fa2760726 | ||
|
|
16a06ade68 | ||
|
|
82b78bc58c | ||
|
|
79f1771d35 | ||
|
|
09462288b1 | ||
|
|
f64e0a6457 |
@@ -5,6 +5,7 @@
|
|||||||
*.dll
|
*.dll
|
||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
|
main
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
# Test binary, built with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
module dev.ixab.de/da/kamojiserv
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require github.com/sirupsen/logrus v1.8.1
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||||
|
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||||
|
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||||
|
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
@@ -4,14 +4,16 @@
|
|||||||
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
|
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
|
||||||
<meta http-equiv="Pragma" content="no-cache" />
|
<meta http-equiv="Pragma" content="no-cache" />
|
||||||
<meta http-equiv="Expires" content="0" />
|
<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 property="og:description" content="{{.Kamoji}}" />
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>{{.Kamoji}}</title>
|
<title>{{.Kamoji}}</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
<p style="text-align: center; padding: 50px 0; font-size: 5vw">
|
||||||
{{.Kamoji}}
|
{{.Kamoji}}
|
||||||
|
</p>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -2,12 +2,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"flag"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Kamoji struct {
|
type Kamoji struct {
|
||||||
@@ -18,10 +20,10 @@ type Kamojis struct {
|
|||||||
Kamojis []Kamoji
|
Kamojis []Kamoji
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadKamojis() Kamojis {
|
func loadKamojis(path string) Kamojis {
|
||||||
kamojis := Kamojis{}
|
kamojis := Kamojis{}
|
||||||
|
log.Println("load kamojis from " + path + ".")
|
||||||
file, err := os.Open("kamojis.txt")
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -31,28 +33,48 @@ func loadKamojis() Kamojis {
|
|||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
kamojis.Kamojis = append(kamojis.Kamojis, Kamoji{Kamoji: scanner.Text()})
|
kamojis.Kamojis = append(kamojis.Kamojis, Kamoji{Kamoji: scanner.Text()})
|
||||||
}
|
}
|
||||||
|
log.Println("kamojis loaded.")
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
return kamojis
|
return kamojis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func randNum(i int) int {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
return rand.Intn(i)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tmpl, err := template.ParseFiles("kamoji_template.html")
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
allk := loadKamojis()
|
|
||||||
|
allk := loadKamojis(*kamojisPath)
|
||||||
|
|
||||||
timestamp := time.Now().Unix()
|
timestamp := time.Now().Unix()
|
||||||
randomNumber := rand.Intn(len(allk.Kamojis))
|
randomNumber := randNum(len(allk.Kamojis))
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if time.Now().Unix()-timestamp > 60 {
|
if time.Now().Unix()-timestamp > 60 {
|
||||||
randomNumber = rand.Intn(len(allk.Kamojis))
|
randomNumber = randNum(len(allk.Kamojis))
|
||||||
timestamp = time.Now().Unix()
|
timestamp = time.Now().Unix()
|
||||||
|
log.Println("rotating kamoji.")
|
||||||
}
|
}
|
||||||
|
log.Println("serving kamoji to " + r.Header.Get("x-forwarded-for") + ".")
|
||||||
k := allk.Kamojis[randomNumber]
|
k := allk.Kamojis[randomNumber]
|
||||||
tmpl.Execute(w, k)
|
tmpl.Execute(w, k)
|
||||||
})
|
})
|
||||||
http.ListenAndServe(":80", nil)
|
|
||||||
|
http.ListenAndServe(":"+*port, nil)
|
||||||
|
log.Println("starting webserver on port " + *port + ". press ctrl-c to exit.")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user