51 lines
650 B
Go
51 lines
650 B
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"sync"
|
||
|
|
||
|
"dynatron.me/x/blasphem/pkg/bus"
|
||
|
"dynatron.me/x/blasphem/pkg/config"
|
||
|
)
|
||
|
|
||
|
type Server struct {
|
||
|
*bus.Bus
|
||
|
*http.Server
|
||
|
wg sync.WaitGroup
|
||
|
cfg *config.Config
|
||
|
}
|
||
|
|
||
|
func New(cfg *config.Config) (*Server, error) {
|
||
|
mux := http.NewServeMux()
|
||
|
|
||
|
s := &Server{
|
||
|
Bus: bus.New(),
|
||
|
Server: &http.Server{
|
||
|
Addr: cfg.Server.Bind,
|
||
|
Handler: mux,
|
||
|
},
|
||
|
cfg: cfg,
|
||
|
}
|
||
|
|
||
|
mux.HandleFunc("/api/websocket", s.wsHandler)
|
||
|
|
||
|
return s, nil
|
||
|
}
|
||
|
|
||
|
func (s *Server) Go() error {
|
||
|
s.wg.Add(1)
|
||
|
go func() {
|
||
|
err := s.ListenAndServe()
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
s.wg.Done()
|
||
|
}()
|
||
|
|
||
|
s.wg.Wait()
|
||
|
|
||
|
return nil
|
||
|
}
|