/
server.go
75 lines (63 loc) · 2.22 KB
/
server.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
package piplayer
import (
"context"
"log"
"net/http"
"time"
)
// NewServer returns a new http.Server for the piplayer interface.
func NewServer(p *Player, addr string) *http.Server {
mux := setupRoutes(p.conf.Mount.Dir, p)
serv := http.Server{Addr: addr, Handler: mux}
return &serv
}
// setupRoutes registers the routes for the server, accepting a new directory where
// the content can be found. This will be called whenever there is a change of content
// given from the settings page.
func setupRoutes(content string, p *Player) *http.ServeMux {
mux := http.NewServeMux()
mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.FS(p.api.statAssets))))
// mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("pkg/piplayer/assets"))))
mux.HandleFunc("/content/", etagWrapper(content))
mux.HandleFunc("/login", LoginHandler(p))
mux.HandleFunc("/logout", LogoutHandler)
mux.HandleFunc("/control", p.HandleControl)
mux.HandleFunc("/settings", p.conf.SettingsHandler(p))
mux.HandleFunc("/viewer", p.HandleViewer)
mux.HandleFunc("/ws/viewer", p.ConnViewer.HandlerWebsocket(p))
mux.HandleFunc("/ws/control", p.ConnControl.HandlerWebsocket(p))
mux.HandleFunc("/api", p.api.Handle(p))
mux.HandleFunc("/", p.api.handlerHome)
return mux
}
// etagWrapper calculates an Etag value for the requested content.
func etagWrapper(content string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
fs := http.StripPrefix("/content/", http.FileServer(http.Dir(content)))
// TODO: Everything
fs.ServeHTTP(w, r)
}
}
// Restart the http server.
func restart(plr *Player) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if err := plr.Server.Shutdown(ctx); err != nil {
log.Printf("Error shutting down server for re-registration of routes: %v\n", err)
}
go func() {
time.Sleep(5 * time.Second)
cancel()
}()
}
// Start the http server.
func Start(plr *Player) {
log.Printf("Listening on port %s\n", plr.Server.Addr)
err := plr.Server.ListenAndServe()
if err != nil {
log.Println("ListenAndServe: ", err)
}
// Restart the server.
// TODO: This needs to become a config option
plr.Server = NewServer(plr, ":8080")
Start(plr)
}