-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (89 loc) · 2.09 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bufio"
"context"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"time"
)
var stop chan struct{}
var port = "8080"
func main() {
router := http.NewServeMux()
router.HandleFunc("/", stream)
http.Handle("/", router)
ctx, cancel := context.WithCancel(context.Background())
server := &http.Server{
Addr: ":" + port,
Handler: router,
BaseContext: func(_ net.Listener) context.Context { return ctx },
}
// Setting up capturing shutdown
stop = make(chan struct{})
// Run server
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalln(err)
}
}()
go func() {
cmd := exec.Command("xdg-open", "http://localhost:"+port) // Linux desktop
if err := cmd.Run(); err != nil {
log.Fatalln(err)
// return
}
}()
// Wait for ListenAndServe goroutine to close.
// Waiting for stop
<-stop
gracefullCtx, cancelShutdown := context.WithTimeout(context.Background(), time.Second)
defer cancelShutdown()
if err := server.Shutdown(gracefullCtx); err != nil {
log.Fatalln(err)
}
// manually cancel context if not using httpServer.RegisterOnShutdown(cancel)
cancel()
defer os.Exit(0)
}
func stream(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
endChan := make(chan struct{})
go writeOutput(w, os.Stdin, endChan)
select {
case <-endChan:
case <-ctx.Done():
err := ctx.Err()
log.Printf("Client disconnected: %s\n", err)
}
stop <- struct{}{}
close(stop)
}
func writeOutput(w http.ResponseWriter, input io.ReadCloser, endChan chan struct{}) {
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming not supported", http.StatusInternalServerError)
return
}
hdr := w.Header()
hdr.Set("Cache-Control", "no-cache")
hdr.Set("Connection", "keep-alive")
hdr.Set("X-Content-Type-Options", "nosniff")
reader := bufio.NewReader(input)
for {
in, _, err := reader.ReadLine()
if err != nil && err == io.EOF {
break
}
in = append(in, []byte("\n")...)
if _, err := w.Write(in); err != nil {
log.Fatalln(err)
}
flusher.Flush()
}
endChan <- struct{}{}
close(endChan)
}