Archived
Compare commits
2
Commits
4e401cbb7a
...
09462288b1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09462288b1 | ||
|
|
f64e0a6457 |
@@ -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=
|
||||||
@@ -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{}
|
||||||
|
|
||||||
file, err := os.Open("kamojis.txt")
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -39,20 +41,27 @@ func loadKamojis() Kamojis {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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()
|
||||||
|
|
||||||
|
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 := rand.Intn(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 = rand.Intn(len(allk.Kamojis))
|
||||||
timestamp = time.Now().Unix()
|
timestamp = time.Now().Unix()
|
||||||
|
log.Println("rotating kamoji")
|
||||||
}
|
}
|
||||||
|
log.Println("served kamoji to " + r.RemoteAddr)
|
||||||
k := allk.Kamojis[randomNumber]
|
k := allk.Kamojis[randomNumber]
|
||||||
tmpl.Execute(w, k)
|
tmpl.Execute(w, k)
|
||||||
})
|
})
|
||||||
http.ListenAndServe(":80", nil)
|
http.ListenAndServe(":"+*port, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user