From cd7bcd5d55547db002caef0e94cbb48f41386bf2 Mon Sep 17 00:00:00 2001 From: Christopher Ramey Date: Sun, 14 Feb 2021 16:55:56 -0900 Subject: [PATCH] server now opens a web server on the specified addr/port --- main.go | 24 +++++++++++++++++------- server/http.go | 19 +++++++++++++++++++ server/server.go | 37 +++++++++++++++++++++++++++++++------ server/worker.go | 6 +++--- 4 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 server/http.go diff --git a/main.go b/main.go index 2450367..8b5e261 100644 --- a/main.go +++ b/main.go @@ -111,14 +111,24 @@ func main() { fmt.Printf("check successful\n") case "server": - cfg, err := config.ReadConfig(*cfgpath, *debuglvl) - if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err.Error()) - os.Exit(1) - } + for { + cfg, err := config.ReadConfig(*cfgpath, *debuglvl) + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err.Error()) + os.Exit(1) + } + + srv := server.NewServer(cfg, *debuglvl) - srv := server.NewServer(cfg, *debuglvl) - srv.Start() + r, err := srv.Start() + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err.Error()) + os.Exit(1) + } + if !r { + return + } + } case "help", "": printUsage() diff --git a/server/http.go b/server/http.go new file mode 100644 index 0000000..2fcb110 --- /dev/null +++ b/server/http.go @@ -0,0 +1,19 @@ +package server + +import ( + "fmt" + "net/http" +) + +func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/shutdown": + fmt.Fprintf(w, "shutting down .. ") + s.shutdownc <- false + case "/restart": + fmt.Fprintf(w, "restarting .. ") + s.shutdownc <- true + default: + fmt.Fprintf(w, "Hello, world!") + } +} diff --git a/server/server.go b/server/server.go index fb4353a..958843b 100644 --- a/server/server.go +++ b/server/server.go @@ -1,22 +1,35 @@ package server import ( + "context" "fmt" "git.binarythought.com/cdramey/alrm/config" + "net" + "net/http" "time" ) type Server struct { - workers []*worker - cfg *config.Config - debuglvl int + workers []*worker + cfg *config.Config + shutdownc chan bool + debuglvl int + http http.Server } -func (srv *Server) Start() { +func (srv *Server) Start() (bool, error) { + listen, err := net.Listen("tcp", srv.cfg.Listen) + if err != nil { + return false, err + } + for _, w := range srv.workers { go w.start() } + srv.http = http.Server{Handler: srv} + go srv.http.Serve(listen) + t := time.NewTicker(srv.cfg.Interval) defer t.Stop() for { @@ -28,14 +41,26 @@ func (srv *Server) Start() { for _, w := range srv.workers { w.wake() } + case b := <-srv.shutdownc: + srv.http.Shutdown(context.Background()) + for _, w := range srv.workers { + w.shutdown() + } + return b, nil } } } func NewServer(cfg *config.Config, debuglvl int) *Server { - srv := &Server{cfg: cfg, debuglvl: debuglvl} + srv := &Server{ + cfg: cfg, + debuglvl: debuglvl, + shutdownc: make(chan bool, 1), + } for _, g := range cfg.Groups { - srv.workers = append(srv.workers, makeworker(g, debuglvl)) + srv.workers = append( + srv.workers, makeworker(g, debuglvl), + ) } return srv } diff --git a/server/worker.go b/server/worker.go index 0d5fd8f..be43092 100644 --- a/server/worker.go +++ b/server/worker.go @@ -53,11 +53,11 @@ func (w *worker) shutdown() { func makeworker(g *config.Group, d int) *worker { return &worker{ - group: g, - debuglvl: d, + group: g, + debuglvl: d, // This channel is unbuffered so that checks that take // over the set interval don't backlog - wakec: make(chan bool), + wakec: make(chan bool), // This channel is buffered because we want it to remember // an order to shutdown shutdownc: make(chan bool, 1),