Commit 6d3932b4 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Move webserver into its own package.

parent 21184556
...@@ -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(&staticRoot, "static", "./static/", flag.StringVar(&webserver.StaticRoot, "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
shutdown() webserver.Shutdown()
} }
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(staticRoot, "404.html")) f, err := os.Open(path.Join(StaticRoot, "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(staticRoot, "sfu.html")) http.ServeFile(w, r, filepath.Join(StaticRoot, "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 shutdown() { func Shutdown() {
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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment