forked from hugomd/parrot.live
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
98 lines (85 loc) · 1.91 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
package main
import (
"context"
"embed"
"log"
"net"
"net/http"
"os"
"path"
"strings"
"time"
)
var (
//go:embed static/index.html
indexHTML []byte
//go:embed frames/*.txt
frameFS embed.FS
frames [][]byte
ansiClear = []byte("\033[H\033[2J")
ansiReset = []byte("\033[0m")
ansiColors = [...][]byte{
[]byte("\033[1;31m"),
[]byte("\033[1;32m"),
[]byte("\033[1;33m"),
[]byte("\033[1;34m"),
[]byte("\033[1;35m"),
[]byte("\033[1;36m"),
}
)
func init() {
framesDir, err := frameFS.ReadDir("frames")
if err != nil {
log.Fatalln("unable to read frame dir:", err)
}
frames = make([][]byte, len(framesDir))
for i := range framesDir {
frame, err := frameFS.ReadFile(path.Join("frames", framesDir[i].Name()))
if err != nil {
log.Fatalln("unable to read frame file:", err)
}
frames[i] = frame
}
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
isCurl := strings.Contains(r.Header.Get("user-agent"), "curl")
log.Println("new req | is curl:", isCurl)
if isCurl {
idx := 0
ticker := time.NewTicker(time.Millisecond * 100)
reqCancelCtx, cancel := context.WithTimeout(r.Context(), time.Minute*2)
defer cancel()
for {
select {
case <-reqCancelCtx.Done():
w.Write([]byte("no more coffee :(\n"))
return
case <-ticker.C:
w.Write(ansiClear)
w.Write(ansiColors[idx%len(ansiColors)])
w.Write(frames[idx%len(frames)])
w.Write(ansiReset)
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
idx = idx + 1
}
}
} else {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(indexHTML)
}
})
host := "0.0.0.0"
if ehost := os.Getenv("HOST"); ehost != "" {
host = ehost
}
port := "8000"
if eport := os.Getenv("PORT"); eport != "" {
port = eport
}
addr := net.JoinHostPort(host, port)
log.Println("serving coffee on:", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}