-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (65 loc) · 1.53 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
package main
import (
"bytes"
"fmt"
"image/jpeg"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
state := 1
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
for i := 0; i < 6; i++ {
go func(i int) {
stream := NewStream()
router := http.NewServeMux()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html;charset=utf-8")
_, _ = w.Write([]byte("<img width='100%' height='100%' src='/download'>"))
})
router.HandleFunc("/upload", func(_ http.ResponseWriter, r *http.Request) {
origin, _ := io.ReadAll(r.Body)
// fmt.Println(len(origin))
// stream.UpdateJPEG(origin)
inputImage, err := jpeg.Decode(bytes.NewBuffer(origin))
if err != nil {
log.Fatalln(err)
}
var compress bytes.Buffer
err = jpeg.Encode(&compress, inputImage, &jpeg.Options{Quality: 50})
if err != nil {
log.Fatalln(err)
}
stream.UpdateJPEG(compress.Bytes())
})
router.HandleFunc("/download", stream.ServeHTTP)
port := 8000 + i
log.Println("server listening on", port)
server := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%d", port),
Handler: router,
}
log.Fatalln(server.ListenAndServe())
}(i)
}
EXIT:
for {
sig := <-sc
switch sig {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
state = 0
break EXIT
case syscall.SIGHUP:
default:
break EXIT
}
}
time.Sleep(time.Second)
os.Exit(state)
}