-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
209 lines (162 loc) · 4.31 KB
/
handlers.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"bytes"
"fmt"
"log"
"mime/multipart"
"net/http"
"net/textproto"
)
// IndexHandler shows main page of the site and does not require authentication
func IndexHandler(w http.ResponseWriter, r *http.Request) {
data := make(map[string]interface{})
cookie, err := r.Cookie("sid")
if err == nil {
session := sessionStore.Get(cookie.Value)
if session.isNew == false {
data["user"] = session.value["user"]
}
}
renderTemplate(w, "index.html", data)
}
// VideoHandler shows live video from uav
func VideoHandler(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("sid")
if err != nil {
http.Redirect(w, r, "/login", 302)
return
}
session := sessionStore.Get(cookie.Value)
if session.isNew {
http.Redirect(w, r, "/login", 302)
return
}
session.Update()
data := make(map[string]interface{})
data["user"] = session.value["user"]
renderTemplate(w, "video.html", data)
}
// SnapshotHandler show a page with snapshot
func SnapshotHandler(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("sid")
//fmt.Println(cookie)
//fmt.Println(cookie.Value)
if err != nil {
http.Redirect(w, r, "/login", 302)
return
}
session := sessionStore.Get(cookie.Value)
if session.isNew {
http.Redirect(w, r, "/login", 302)
return
}
// update timeAccessed
session.Update()
data := make(map[string]interface{})
data["user"] = session.value["user"]
renderTemplate(w, "snapshot.html", data)
}
// LoginHandler handles user login
func LoginHandler(w http.ResponseWriter, r *http.Request) {
data := make(map[string]interface{})
if r.Method == "GET" {
renderTemplate(w, "login.html", data)
}
if r.Method == "POST" {
username := r.FormValue("username")
password := r.FormValue("password")
loggedin := false
if user, ok := FindUser(username, password); ok {
fmt.Println(user)
session := NewSession(map[interface{}]interface{}{"user": user})
//fmt.Println(session)
session.isNew = false
sessionStore.Set(session.sid, session)
cookie := &http.Cookie{Name: "sid", Value: session.sid}
http.SetCookie(w, cookie)
loggedin = true
http.Redirect(w, r, "/", 302)
}
data["username"] = username
data["errorMessage"] = "Kullanıcı adı veya Şifresi hatalı"
if !loggedin {
renderTemplate(w, "login.html", data)
}
}
}
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("sid")
if err == nil {
// Delete session for given session id (sid)
sessionStore.Delete(cookie.Value)
cookie := &http.Cookie{Name: "sid", Value: "", MaxAge: -1}
http.SetCookie(w, cookie)
http.Redirect(w, r, "/", 302)
return
}
http.Redirect(w, r, "/", 302)
}
func JpegHandler(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("sid")
if err != nil {
http.Redirect(w, r, "/login", 302)
return
}
session := sessionStore.Get(cookie.Value)
if session.isNew {
http.Redirect(w, r, "/login", 302)
return
}
session.Update()
w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("cache-control", "priviate, max-age=0, no-cache")
w.Header().Set("pragma", "no-cache")
w.Header().Set("expires", "-1")
w.Write(jpegImage)
}
func MotionJpegHandler(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("sid")
//fmt.Println(cookie)
//fmt.Println(cookie.Value)
if err != nil {
http.Redirect(w, r, "/login", 302)
return
}
session := sessionStore.Get(cookie.Value)
if session.isNew {
http.Redirect(w, r, "/login", 302)
return
}
session.Update()
log.Println("Serve streaming")
m := multipart.NewWriter(w)
w.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary="+m.Boundary())
w.Header().Set("Connection", "close")
w.Header().Set("cache-control", "priviate, max-age=0, no-cache")
w.Header().Set("pragma", "no-cache")
w.Header().Set("expires", "-1")
header := textproto.MIMEHeader{}
var buf bytes.Buffer
for {
mutex.RLock()
buf.Reset()
_, err := buf.Write(jpegImage)
//err = jpeg.Encode(&buf, img, nil)
//w.Write(jpegImage)
mutex.RUnlock()
if err != nil {
break
}
header.Set("Content-Type", "image/jpeg")
header.Set("Content-Length", fmt.Sprint(buf.Len()))
mw, err := m.CreatePart(header)
if err != nil {
break
}
mw.Write(buf.Bytes())
if flusher, ok := mw.(http.Flusher); ok {
flusher.Flush()
}
}
log.Println("Stop streaming")
}