-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
166 lines (136 loc) · 3.97 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"os/signal"
"slices"
"strconv"
"syscall"
"github.com/syncstreamer/server/params"
"github.com/syncstreamer/server/processor"
"github.com/syncstreamer/server/timeframe/eventframe"
"github.com/syncstreamer/server/timestamp"
"github.com/syncstreamer/server/types"
)
func startInServer(proc *processor.Processor) *http.Server {
muxIn := http.NewServeMux()
muxIn.HandleFunc("/event/{channel}", func(resp http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
log.Printf("Wrong method %v for /event/{channel} endpoint", req.Method)
resp.WriteHeader(http.StatusBadRequest)
return
}
channelId := req.PathValue("channel")
if channelId == "" {
log.Println("Input event channel name is empty")
resp.WriteHeader(http.StatusBadRequest)
return
}
buf := make([]byte, req.ContentLength)
req.Body.Read(buf)
req.Body.Close()
contentType := req.Header.Get("Content-Type")
proc.AddEvent(&eventframe.Event{
ChannelId: types.Id(channelId),
EventType: types.ChannelType(contentType),
EventData: buf,
})
resp.WriteHeader(http.StatusOK)
})
serverIn := http.Server{
Addr: params.InAddr,
Handler: muxIn,
}
go serverIn.ListenAndServe()
return &serverIn
}
func startOutServer(proc *processor.Processor) *http.Server {
muxIn := http.NewServeMux()
if params.ServeStatic {
const staticDir = "./static"
log.Printf("Serving static from %s", staticDir)
muxIn.Handle("/", http.FileServer(http.Dir(staticDir)))
}
muxIn.HandleFunc("/frame", func(resp http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
log.Printf("Wrong method %v for /frame endpoint", req.Method)
resp.WriteHeader(http.StatusBadRequest)
return
}
type ResponseItem struct {
StartAt int
EndAt int
Id string
}
items := proc.GetTimeframes()
its := make([]*ResponseItem, len(items))
for i, x := range items {
its[i] = &ResponseItem{
StartAt: int(x.StartAt),
EndAt: int(x.EndAt),
Id: strconv.Itoa(int(x.StartAt)),
}
}
b, err := json.Marshal(its)
if err != nil {
log.Fatalf("Marshaling JSON error: %v", err)
}
resp.Header().Add("Content-Type", "application/json")
resp.Write(b)
})
muxIn.HandleFunc("/frame/{frameId}", func(resp http.ResponseWriter, req *http.Request) {
frameIdRaw := req.PathValue("frameId")
frameId, err := strconv.ParseInt(frameIdRaw, 0, 0)
if req.Method != http.MethodGet || err != nil {
log.Printf("Wrong method %v for /frame %v endpoint, error: %v", req.Method, frameIdRaw, err)
resp.WriteHeader(http.StatusBadRequest)
return
}
frames := proc.GetTimeframes()
i := slices.IndexFunc(frames, func(frm *processor.TimeframeItem) bool {
return frm.StartAt == timestamp.Timestamp(frameId)
})
if i < 0 {
resp.WriteHeader(http.StatusNotFound)
return
} else {
resp.Header().Add("Content-Type", "application/octet-stream")
resp.WriteHeader(http.StatusOK)
dt := frames[i].Data
resp.Write(dt)
}
})
serverIn := http.Server{
Addr: params.OutAddr,
Handler: muxIn,
}
if params.UseTLS {
go serverIn.ListenAndServeTLS(params.CertPath, params.CertPrivateKeyPath)
} else {
go serverIn.ListenAndServe()
}
return &serverIn
}
func main() {
params.ReadParams()
servingContext, cancelServingContext := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancelServingContext()
processorContext, cancelProcessorContext := context.WithCancel(context.Background())
defer cancelProcessorContext()
proc := processor.StartNewProcessor(processorContext)
inServer := startInServer(proc)
outServer := startOutServer(proc)
<-servingContext.Done()
log.Println("Shutting down outbound HTTP endpoints")
err := outServer.Shutdown(context.Background())
if err != nil {
log.Fatalf("%v", err)
}
log.Println("Shutting down inbound HTTP endpoints")
err = inServer.Shutdown(context.Background())
if err != nil {
log.Fatalf("%v", err)
}
}