alrm/server/server.go

65 lines
1.1 KiB
Go
Raw Normal View History

package server
import (
"context"
"fmt"
"git.binarythought.com/cdramey/alrm/config"
"net"
"net/http"
"time"
)
type Server struct {
workers []*worker
config *config.Config
shutdownc chan bool
http http.Server
}
func (srv *Server) Start() (bool, error) {
listen, err := net.Listen("tcp", srv.config.Listen)
if err != nil {
return false, err
}
for _, w := range srv.workers {
2021-02-14 14:22:30 -09:00
go w.start()
}
srv.http = http.Server{Handler: srv}
go srv.http.Serve(listen)
t := time.NewTicker(srv.config.Interval)
defer t.Stop()
for {
select {
case r := <-t.C:
if srv.config.DebugLevel > 0 {
fmt.Printf("interval check at %s\n", r)
}
for _, w := range srv.workers {
2021-02-14 14:22:30 -09:00
w.wake()
}
case b := <-srv.shutdownc:
srv.http.Shutdown(context.Background())
for _, w := range srv.workers {
w.shutdown()
}
return b, nil
}
}
}
2021-02-28 04:51:35 -09:00
func NewServer(cfg *config.Config) *Server {
srv := &Server{
config: cfg,
shutdownc: make(chan bool, 1),
}
for _, g := range cfg.Groups {
srv.workers = append(
2021-02-28 04:51:35 -09:00
srv.workers, makeworker(g, cfg.DebugLevel),
)
}
return srv
}