2023-01-11 06:38:48 +00:00
|
|
|
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())
|
2023-02-06 04:26:00 +00:00
|
|
|
mux.Handle("/about", AboutPage())
|
2023-01-11 06:38:48 +00:00
|
|
|
mux.Handle("/ws", s.WebSocket())
|
2023-02-10 07:03:06 +00:00
|
|
|
mux.Handle("/api/statistics", s.Statistics())
|
2023-07-30 17:32:08 +00:00
|
|
|
mux.Handle("/api/blocklist", s.BlockList())
|
2023-08-14 02:21:27 +00:00
|
|
|
mux.Handle("/api/authenticate", s.Authenticate())
|
2023-08-29 00:49:50 +00:00
|
|
|
mux.Handle("/api/shutdown", s.ShutdownAPI())
|
2023-09-07 00:15:02 +00:00
|
|
|
mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("dist/assets"))))
|
|
|
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("dist/static"))))
|
2023-01-11 06:38:48 +00:00
|
|
|
|
|
|
|
s.mux = mux
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenAndServe starts the web server.
|
|
|
|
func (s *Server) ListenAndServe(address string) error {
|
|
|
|
return http.ListenAndServe(address, s.mux)
|
|
|
|
}
|