BareRTC/pkg/server.go
Noah Petherbridge b966f85ecc Spit and polish
* Track the window focus/blur events. Leaving the tab while in a channel
  now means you may still hear sound effects in that channel.
* Add a CORS JSON API /v1/statistics to get details from the server
  about who is online. The CORSHosts whitelist in the settings.toml
  limits domain access to the endpoint.
2023-02-09 23:03:06 -08:00

48 lines
1.1 KiB
Go

package barertc
import (
"net/http"
"sync"
)
// Server is the primary back-end server struct for BareRTC, see main.go
type Server struct {
// HTTP router.
mux *http.ServeMux
// Max number of messages we'll buffer for a subscriber
subscriberMessageBuffer int
// Connected WebSocket subscribers.
subscribersMu sync.RWMutex
subscribers map[*Subscriber]struct{}
}
// NewServer initializes the Server.
func NewServer() *Server {
return &Server{
subscriberMessageBuffer: 16,
subscribers: make(map[*Subscriber]struct{}),
}
}
// Setup the server: configure HTTP routes, etc.
func (s *Server) Setup() error {
var mux = http.NewServeMux()
mux.Handle("/", IndexPage())
mux.Handle("/about", AboutPage())
mux.Handle("/ws", s.WebSocket())
mux.Handle("/api/statistics", s.Statistics())
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
s.mux = mux
return nil
}
// ListenAndServe starts the web server.
func (s *Server) ListenAndServe(address string) error {
return http.ListenAndServe(address, s.mux)
}