From 0284f1f6d747d8fbd2334db85a0dd87a7f826398 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Tue, 2 Oct 2018 15:22:13 +0200 Subject: [PATCH] separate signal handling --- server.go | 66 +++++++++++++++++++++++++++---------------------------- signal.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 34 deletions(-) create mode 100644 signal.go diff --git a/server.go b/server.go index bd50a52..381e0de 100644 --- a/server.go +++ b/server.go @@ -29,10 +29,7 @@ import ( "encoding/json" "io" "net/http" - "os" - "os/signal" "strings" - "syscall" "gitlab.weird-web-workers.org/golang/logger" ) @@ -49,8 +46,9 @@ type version struct { type Server struct { server *http.Server + mux *http.ServeMux log *logger.Logger - Stop chan bool + ctx context.Context } func (handler *ApiHandler) ServeHTTP( @@ -80,45 +78,45 @@ func (handler *ApiHandler) ServeHTTP( func NewServer(addr string, log *logger.Logger) (server *Server) { handler := ApiHandler{log: log} - - http.Handle("/api/0.0.1/", http.StripPrefix("/api/0.0.1/", &handler)) + mux := http.NewServeMux() server = &Server{ - server: &http.Server{Addr: addr}, + server: &http.Server{ + Addr: addr, + Handler: mux, + }, + mux: mux, log: log, - Stop: make(chan bool), + ctx: context.Background(), } - go func() { - server.log.Info("Listening on %s", addr) + + server.mux.Handle( + "/api/0.0.1/", + http.StripPrefix("/api/0.0.1/", &handler), + ) + + return +} + +func (server *Server) Start() <-chan struct{} { + done := make(chan struct{}) + go func(done chan struct{}) { + server.log.Info("Listening on %s", server.server.Addr) if err := server.server.ListenAndServe(); err != http.ErrServerClosed { logger.Default.LogError(err, "Stop listening") } - }() - - interrupt := make(chan os.Signal, 1) - signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) - go func() { - for run, ok := true, true; run && ok; { - select { - case <-interrupt: - server.log.Info("API service Interrupted by signal") - run = false - case run, ok = <-server.Stop: - server.log.Info("API service stopped") - } - } - - ctx := context.Background() - if err := server.server.Shutdown(ctx); err != nil { - server.log.LogError(err, "Unclean server shutdown") - } else { - server.log.Info("Server stopped") - } - server.Stop <- true - }() + close(done) + }(done) + return done +} - return +func (server *Server) Finish() { + if err := server.server.Shutdown(server.ctx); err != nil { + server.log.LogError(err, "Unclean server shutdown") + } else { + server.log.Info("Server stopped") + } } // vim: ts=4 sts=4 sw=4 noet tw=72: diff --git a/signal.go b/signal.go new file mode 100644 index 0000000..8fc1c24 --- /dev/null +++ b/signal.go @@ -0,0 +1,64 @@ +/* +This is some stuff... + +Authors: +Georg Hopp + +Changes: +2018-09-30 [Georg Hopp] File created. + +Copyright © 2018 Georg Hopp + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +package test + +import ( + "os" + "os/signal" + "syscall" + + "gitlab.weird-web-workers.org/golang/logger" +) + +type Signal interface { + Hangup() + Shutdown() +} + +func Wait(sig Signal) { + interrupt := make(chan os.Signal, 1) + signal.Notify( + interrupt, + os.Interrupt, + syscall.SIGTERM, + syscall.SIGINT, + syscall.SIGHUP, + ) + for run, ok := true, true; run && ok; { + select { + case s := <-interrupt: + logger.Default.Info("Interrupted by signal: %s", sig) + switch s { + case syscall.SIGHUP: + sig.Hangup() + default: + sig.Shutdown() + run = false + } + } + } +} + +// vim: ts=4 sts=4 sw=4 noet tw=72: