forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trail_api_handler.go
89 lines (80 loc) · 1.95 KB
/
trail_api_handler.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
package uadmin
import (
"fmt"
"math"
"net/http"
"strings"
"time"
)
func trailAPIHandler(w http.ResponseWriter, r *http.Request) {
// Check if the user is an admin
session := IsAuthenticated(r)
if session == nil {
w.WriteHeader(403)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "access denied",
})
return
}
Preload(session)
if !session.User.Admin {
w.WriteHeader(403)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "access denied",
})
return
}
f, ok := w.(http.Flusher)
w.Header().Set("Connection", "Keep-Alive")
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Content-type", "text/html; charset=utf-8")
limit := 4096
if len(trailBytes) < limit {
temp := append(trailBytes, []byte(strings.Repeat(" ", (limit)-len(trailBytes)))...)
// temp = append(temp, '\r', '\n')
// temp = append([]byte(fmt.Sprintf("%x\r\n", len(temp))), temp...)
w.Write(temp)
} else {
// temp := append(trailBytes, '\r', '\n')
// w.Write(append([]byte(fmt.Sprintf("%x\r\n", len(temp))), temp...))
// w.Write(trailBytes)
for i := 0; i < len(trailBytes); i += limit {
temp := trailBytes[i:int(math.Min(float64(i+limit), float64(len(trailBytes)-1)))]
if len(temp) < 4096 {
w.Write(append(temp, []byte(strings.Repeat(" ", limit-len(temp)))...))
} else {
w.Write(temp)
}
}
}
if ok {
f.Flush()
} else {
fmt.Println("Not flusher")
}
// time.Sleep(time.Second * 1)
c := make(chan string, 250)
RegisterTrailChan(c)
msg := ""
for {
t := time.After(time.Second * 1)
select {
case msg = <-c:
// fmt.Fprintln(w, msg)
if len(msg) < (limit) {
msg += strings.Repeat(" ", (limit)-len(msg))
}
// msg += "\r\n"
// w.Write(append([]byte(fmt.Sprintf("%x\r\n", len(msg)-2)), msg...))
w.Write([]byte(msg))
if ok {
f.Flush()
}
// fmt.Println("flushed " + msg)
case <-t:
}
}
}