-
Notifications
You must be signed in to change notification settings - Fork 14
/
http.go
75 lines (72 loc) · 1.9 KB
/
http.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
package main
import (
"log"
"net/http"
"sort"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
func serveHTTP() {
router := gin.Default()
gin.SetMode(gin.DebugMode)
router.LoadHTMLGlob("web/templates/*")
router.GET("/", func(c *gin.Context) {
fi, all := Config.list()
sort.Strings(all)
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"port": Config.Server.HTTPPort,
"suuid": fi,
"suuidMap": all,
"version": time.Now().String(),
})
})
router.GET("/player/:suuid", func(c *gin.Context) {
_, all := Config.list()
sort.Strings(all)
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"port": Config.Server.HTTPPort,
"suuid": c.Param("suuid"),
"suuidMap": all,
"version": time.Now().String(),
})
})
router.GET("/play/mjpeg/:suuid", PlayMjpeg)
router.StaticFS("/static", http.Dir("web/static"))
err := router.Run(Config.Server.HTTPPort)
if err != nil {
log.Fatalln(err)
}
}
func PlayMjpeg(c *gin.Context) {
suuid := c.Param("suuid")
if !Config.ext(suuid) {
return
}
Config.RunIFNotRun(suuid)
cuuid, ch := Config.clAd(suuid)
defer Config.clDe(suuid, cuuid)
c.Header("Content-Type", "multipart/x-mixed-replace;boundary=myboundary")
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
c.Header("Connection", "close")
c.Header("Pragma", "no-cache")
c.Header("Expires", "0")
c.Header("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
noVideo := time.NewTimer(10 * time.Second)
for {
select {
case <-noVideo.C:
log.Println("noVideo")
return
case pck := <-ch:
noVideo.Reset(10 * time.Second)
header := "\r\n" + "--" + "myboundary" + "\r\n" + "Content-Type: image/jpeg\r\n" + "Content-Length: " + strconv.Itoa(len(*pck)) + "\r\n" + "X-Timestamp: 0.000000\r\n" + "\r\n"
if n, err := c.Writer.Write([]byte(header)); err != nil || n < 1 {
return
}
if n, err := c.Writer.Write(*pck); err != nil || n < 1 {
return
}
}
}
}