forked from lifansama/plus1s.live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
77 lines (69 loc) · 1.54 KB
/
stream.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"time"
)
const MAX_FRAME = 360
var frames []string = make([]string, 0, MAX_FRAME)
func leftPad2Len(s string, padStr string, overallLen int) string {
var padCountInt int
padCountInt = 1 + ((overallLen - len(padStr)) / len(padStr))
var retStr = strings.Repeat(padStr, padCountInt) + s
return retStr[(len(retStr) - overallLen):]
}
func plus1s(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("User-Agent"), "curl") {
http.Redirect(w, r, "https://github.com/HFO4/plus1s.live", http.StatusFound)
return
}
i := 1
loop := 0
for {
if i == MAX_FRAME {
i = 0
loop++
}
if loop > 20 {
write(w, "You've waste too much time, we have to stop you.")
return
}
write(w, frames[i])
i++
}
}
func write(w http.ResponseWriter, s string) {
flusher, ok := w.(http.Flusher)
if !ok {
panic("Expected http.ResponseWriter to be an http.CloseNotifier")
}
fmt.Fprintf(w, s)
fmt.Fprintf(w, "\033[2J\033[H")
time.Sleep(100000000)
flusher.Flush()
}
func prepare() {
fmt.Println("Prepare frames...")
for i := 0; i < MAX_FRAME; i++ {
b, err := ioutil.ReadFile("pic/" + leftPad2Len(strconv.Itoa(i+1), "0", 3) + ".txt")
if err != nil {
fmt.Print(err)
}
str := string(b)
frames = append(frames, str)
}
fmt.Println("Frames are ready.")
}
func main() {
prepare()
fmt.Println("Ready to start server")
http.HandleFunc("/", plus1s)
err := http.ListenAndServe(":1926", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}