BareRTC/pkg/server.go

76 lines
2.1 KiB
Go
Raw Permalink Normal View History

2023-01-11 06:38:48 +00:00
package barertc
import (
2023-11-11 22:59:49 +00:00
"io"
2023-01-11 06:38:48 +00:00
"net/http"
"sync"
"git.kirsle.net/apps/barertc/pkg/config"
"git.kirsle.net/apps/barertc/pkg/log"
"git.kirsle.net/apps/barertc/pkg/models"
2023-01-11 06:38:48 +00:00
)
// 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{}
2023-11-11 22:59:49 +00:00
// Cached filehandles for channel logging.
logfh map[string]io.WriteCloser
2023-01-11 06:38:48 +00:00
}
// NewServer initializes the Server.
func NewServer() *Server {
return &Server{
subscriberMessageBuffer: 32,
2023-01-11 06:38:48 +00:00
subscribers: make(map[*Subscriber]struct{}),
}
}
// Setup the server: configure HTTP routes, etc.
func (s *Server) Setup() error {
// Enable the SQLite database for DM history?
if config.Current.DirectMessageHistory.Enabled {
if err := models.Initialize(config.Current.DirectMessageHistory.SQLiteDatabase); err != nil {
log.Error("Error initializing SQLite database: %s", err)
}
}
2023-01-11 06:38:48 +00:00
var mux = http.NewServeMux()
mux.Handle("/", IndexPage())
mux.Handle("/about", AboutPage())
mux.Handle("/logout", LogoutPage())
2023-01-11 06:38:48 +00:00
mux.Handle("/ws", s.WebSocket())
2023-12-11 02:43:18 +00:00
mux.Handle("/poll", s.PollingAPI())
mux.Handle("/api/statistics", s.Statistics())
mux.Handle("/api/blocklist", s.BlockList())
mux.Handle("/api/block/now", s.BlockNow())
2024-03-15 06:04:24 +00:00
mux.Handle("/api/disconnect/now", s.DisconnectNow())
2023-08-14 02:21:27 +00:00
mux.Handle("/api/authenticate", s.Authenticate())
mux.Handle("/api/shutdown", s.ShutdownAPI())
mux.Handle("/api/profile", s.UserProfile())
mux.Handle("/api/message/history", s.MessageHistory())
2024-04-12 06:28:35 +00:00
mux.Handle("/api/message/clear", s.ClearMessages())
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 {
2023-12-11 02:43:18 +00:00
// Run the polling user idle kicker.
go s.KickIdlePollUsers()
2023-01-11 06:38:48 +00:00
return http.ListenAndServe(address, s.mux)
}