Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
sfu
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alain Takoudjou
sfu
Commits
6d3932b4
Commit
6d3932b4
authored
Sep 18, 2020
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move webserver into its own package.
parent
21184556
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
19 deletions
+20
-19
sfu.go
sfu.go
+5
-8
webserver/webserver.go
webserver/webserver.go
+15
-11
No files found.
sfu.go
View file @
6d3932b4
...
@@ -17,17 +17,14 @@ import (
...
@@ -17,17 +17,14 @@ import (
"sfu/disk"
"sfu/disk"
"sfu/group"
"sfu/group"
"sfu/webserver"
)
)
var
httpAddr
string
var
staticRoot
string
var
dataDir
string
func
main
()
{
func
main
()
{
var
cpuprofile
,
memprofile
,
mutexprofile
string
var
cpuprofile
,
memprofile
,
mutexprofile
,
httpAddr
,
dataDir
string
flag
.
StringVar
(
&
httpAddr
,
"http"
,
":8443"
,
"web server `address`"
)
flag
.
StringVar
(
&
httpAddr
,
"http"
,
":8443"
,
"web server `address`"
)
flag
.
StringVar
(
&
s
taticRoot
,
"static"
,
"./static/"
,
flag
.
StringVar
(
&
webserver
.
S
taticRoot
,
"static"
,
"./static/"
,
"web server root `directory`"
)
"web server root `directory`"
)
flag
.
StringVar
(
&
dataDir
,
"data"
,
"./data/"
,
flag
.
StringVar
(
&
dataDir
,
"data"
,
"./data/"
,
"data `directory`"
)
"data `directory`"
)
...
@@ -84,10 +81,10 @@ func main() {
...
@@ -84,10 +81,10 @@ func main() {
group
.
IceFilename
=
filepath
.
Join
(
dataDir
,
"ice-servers.json"
)
group
.
IceFilename
=
filepath
.
Join
(
dataDir
,
"ice-servers.json"
)
go
group
.
ReadPublicGroups
()
go
group
.
ReadPublicGroups
()
webserver
(
)
webserver
.
Serve
(
httpAddr
,
dataDir
)
terminate
:=
make
(
chan
os
.
Signal
,
1
)
terminate
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
terminate
,
syscall
.
SIGINT
,
syscall
.
SIGTERM
)
signal
.
Notify
(
terminate
,
syscall
.
SIGINT
,
syscall
.
SIGTERM
)
<-
terminate
<-
terminate
s
hutdown
()
webserver
.
S
hutdown
()
}
}
webserver.go
→
webserver
/webserver
.go
View file @
6d3932b4
package
main
package
webserver
import
(
import
(
"bufio"
"bufio"
...
@@ -27,8 +27,10 @@ import (
...
@@ -27,8 +27,10 @@ import (
var
server
*
http
.
Server
var
server
*
http
.
Server
func
webserver
()
{
var
StaticRoot
string
http
.
Handle
(
"/"
,
mungeHandler
{
http
.
FileServer
(
http
.
Dir
(
staticRoot
))})
func
Serve
(
address
string
,
dataDir
string
)
{
http
.
Handle
(
"/"
,
mungeHandler
{
http
.
FileServer
(
http
.
Dir
(
StaticRoot
))})
http
.
HandleFunc
(
"/group/"
,
groupHandler
)
http
.
HandleFunc
(
"/group/"
,
groupHandler
)
http
.
HandleFunc
(
"/recordings"
,
http
.
HandleFunc
(
"/recordings"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
@@ -44,10 +46,12 @@ func webserver() {
...
@@ -44,10 +46,12 @@ func webserver() {
filepath
.
Join
(
dataDir
,
"ice-servers.json"
))
filepath
.
Join
(
dataDir
,
"ice-servers.json"
))
})
})
http
.
HandleFunc
(
"/public-groups.json"
,
publicHandler
)
http
.
HandleFunc
(
"/public-groups.json"
,
publicHandler
)
http
.
HandleFunc
(
"/stats"
,
statsHandler
)
http
.
HandleFunc
(
"/stats"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
statsHandler
(
w
,
r
,
dataDir
)
})
server
=
&
http
.
Server
{
server
=
&
http
.
Server
{
Addr
:
httpAddr
,
Addr
:
address
,
ReadHeaderTimeout
:
60
*
time
.
Second
,
ReadHeaderTimeout
:
60
*
time
.
Second
,
IdleTimeout
:
120
*
time
.
Second
,
IdleTimeout
:
120
*
time
.
Second
,
}
}
...
@@ -100,7 +104,7 @@ func notFound(w http.ResponseWriter) {
...
@@ -100,7 +104,7 @@ func notFound(w http.ResponseWriter) {
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html; charset=utf-8"
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html; charset=utf-8"
)
w
.
WriteHeader
(
http
.
StatusNotFound
)
w
.
WriteHeader
(
http
.
StatusNotFound
)
f
,
err
:=
os
.
Open
(
path
.
Join
(
s
taticRoot
,
"404.html"
))
f
,
err
:=
os
.
Open
(
path
.
Join
(
S
taticRoot
,
"404.html"
))
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Fprintln
(
w
,
"<p>Not found</p>"
)
fmt
.
Fprintln
(
w
,
"<p>Not found</p>"
)
return
return
...
@@ -162,7 +166,7 @@ func groupHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -162,7 +166,7 @@ func groupHandler(w http.ResponseWriter, r *http.Request) {
return
return
}
}
http
.
ServeFile
(
w
,
r
,
filepath
.
Join
(
s
taticRoot
,
"sfu.html"
))
http
.
ServeFile
(
w
,
r
,
filepath
.
Join
(
S
taticRoot
,
"sfu.html"
))
}
}
func
publicHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
publicHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
@@ -179,7 +183,7 @@ func publicHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -179,7 +183,7 @@ func publicHandler(w http.ResponseWriter, r *http.Request) {
return
return
}
}
func
getPassword
()
(
string
,
string
,
error
)
{
func
getPassword
(
dataDir
string
)
(
string
,
string
,
error
)
{
f
,
err
:=
os
.
Open
(
filepath
.
Join
(
dataDir
,
"passwd"
))
f
,
err
:=
os
.
Open
(
filepath
.
Join
(
dataDir
,
"passwd"
))
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
""
,
err
return
""
,
""
,
err
...
@@ -207,8 +211,8 @@ func failAuthentication(w http.ResponseWriter, realm string) {
...
@@ -207,8 +211,8 @@ func failAuthentication(w http.ResponseWriter, realm string) {
http
.
Error
(
w
,
"Haha!"
,
http
.
StatusUnauthorized
)
http
.
Error
(
w
,
"Haha!"
,
http
.
StatusUnauthorized
)
}
}
func
statsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
statsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
dataDir
string
)
{
u
,
p
,
err
:=
getPassword
()
u
,
p
,
err
:=
getPassword
(
dataDir
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"Passwd: %v"
,
err
)
log
.
Printf
(
"Passwd: %v"
,
err
)
failAuthentication
(
w
,
"stats"
)
failAuthentication
(
w
,
"stats"
)
...
@@ -472,7 +476,7 @@ func serveGroupRecordings(w http.ResponseWriter, r *http.Request, f *os.File, gr
...
@@ -472,7 +476,7 @@ func serveGroupRecordings(w http.ResponseWriter, r *http.Request, f *os.File, gr
fmt
.
Fprintf
(
w
,
"</body></html>
\n
"
)
fmt
.
Fprintf
(
w
,
"</body></html>
\n
"
)
}
}
func
s
hutdown
()
{
func
S
hutdown
()
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
2
*
time
.
Second
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
2
*
time
.
Second
)
defer
cancel
()
defer
cancel
()
server
.
Shutdown
(
ctx
)
server
.
Shutdown
(
ctx
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment