Showing
2 changed files
with
96 additions
and
34 deletions
@@ -29,10 +29,7 @@ import ( | @@ -29,10 +29,7 @@ import ( | ||
29 | "encoding/json" | 29 | "encoding/json" |
30 | "io" | 30 | "io" |
31 | "net/http" | 31 | "net/http" |
32 | - "os" | ||
33 | - "os/signal" | ||
34 | "strings" | 32 | "strings" |
35 | - "syscall" | ||
36 | 33 | ||
37 | "gitlab.weird-web-workers.org/golang/logger" | 34 | "gitlab.weird-web-workers.org/golang/logger" |
38 | ) | 35 | ) |
@@ -49,8 +46,9 @@ type version struct { | @@ -49,8 +46,9 @@ type version struct { | ||
49 | 46 | ||
50 | type Server struct { | 47 | type Server struct { |
51 | server *http.Server | 48 | server *http.Server |
49 | + mux *http.ServeMux | ||
52 | log *logger.Logger | 50 | log *logger.Logger |
53 | - Stop chan bool | 51 | + ctx context.Context |
54 | } | 52 | } |
55 | 53 | ||
56 | func (handler *ApiHandler) ServeHTTP( | 54 | func (handler *ApiHandler) ServeHTTP( |
@@ -80,45 +78,45 @@ func (handler *ApiHandler) ServeHTTP( | @@ -80,45 +78,45 @@ func (handler *ApiHandler) ServeHTTP( | ||
80 | 78 | ||
81 | func NewServer(addr string, log *logger.Logger) (server *Server) { | 79 | func NewServer(addr string, log *logger.Logger) (server *Server) { |
82 | handler := ApiHandler{log: log} | 80 | handler := ApiHandler{log: log} |
83 | - | ||
84 | - http.Handle("/api/0.0.1/", http.StripPrefix("/api/0.0.1/", &handler)) | 81 | + mux := http.NewServeMux() |
85 | 82 | ||
86 | server = &Server{ | 83 | server = &Server{ |
87 | - server: &http.Server{Addr: addr}, | 84 | + server: &http.Server{ |
85 | + Addr: addr, | ||
86 | + Handler: mux, | ||
87 | + }, | ||
88 | + mux: mux, | ||
88 | log: log, | 89 | log: log, |
89 | - Stop: make(chan bool), | 90 | + ctx: context.Background(), |
90 | } | 91 | } |
91 | - go func() { | ||
92 | - server.log.Info("Listening on %s", addr) | 92 | + |
93 | + server.mux.Handle( | ||
94 | + "/api/0.0.1/", | ||
95 | + http.StripPrefix("/api/0.0.1/", &handler), | ||
96 | + ) | ||
97 | + | ||
98 | + return | ||
99 | +} | ||
100 | + | ||
101 | +func (server *Server) Start() <-chan struct{} { | ||
102 | + done := make(chan struct{}) | ||
103 | + go func(done chan struct{}) { | ||
104 | + server.log.Info("Listening on %s", server.server.Addr) | ||
93 | if err := server.server.ListenAndServe(); | 105 | if err := server.server.ListenAndServe(); |
94 | err != http.ErrServerClosed { | 106 | err != http.ErrServerClosed { |
95 | logger.Default.LogError(err, "Stop listening") | 107 | logger.Default.LogError(err, "Stop listening") |
96 | } | 108 | } |
97 | - }() | ||
98 | - | ||
99 | - interrupt := make(chan os.Signal, 1) | ||
100 | - signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) | ||
101 | - go func() { | ||
102 | - for run, ok := true, true; run && ok; { | ||
103 | - select { | ||
104 | - case <-interrupt: | ||
105 | - server.log.Info("API service Interrupted by signal") | ||
106 | - run = false | ||
107 | - case run, ok = <-server.Stop: | ||
108 | - server.log.Info("API service stopped") | ||
109 | - } | ||
110 | - } | ||
111 | - | ||
112 | - ctx := context.Background() | ||
113 | - if err := server.server.Shutdown(ctx); err != nil { | ||
114 | - server.log.LogError(err, "Unclean server shutdown") | ||
115 | - } else { | ||
116 | - server.log.Info("Server stopped") | ||
117 | - } | ||
118 | - server.Stop <- true | ||
119 | - }() | 109 | + close(done) |
110 | + }(done) | ||
111 | + return done | ||
112 | +} | ||
120 | 113 | ||
121 | - return | 114 | +func (server *Server) Finish() { |
115 | + if err := server.server.Shutdown(server.ctx); err != nil { | ||
116 | + server.log.LogError(err, "Unclean server shutdown") | ||
117 | + } else { | ||
118 | + server.log.Info("Server stopped") | ||
119 | + } | ||
122 | } | 120 | } |
123 | 121 | ||
124 | // vim: ts=4 sts=4 sw=4 noet tw=72: | 122 | // vim: ts=4 sts=4 sw=4 noet tw=72: |
signal.go
0 → 100644
1 | +/* | ||
2 | +This is some stuff... | ||
3 | + | ||
4 | +Authors: | ||
5 | +Georg Hopp <georg@steffers.org> | ||
6 | + | ||
7 | +Changes: | ||
8 | +2018-09-30 [Georg Hopp] File created. | ||
9 | + | ||
10 | +Copyright © 2018 Georg Hopp | ||
11 | + | ||
12 | +This program is free software: you can redistribute it and/or modify | ||
13 | +it under the terms of the GNU General Public License as published by | ||
14 | +the Free Software Foundation, either version 3 of the License, or | ||
15 | +(at your option) any later version. | ||
16 | + | ||
17 | +This program is distributed in the hope that it will be useful, | ||
18 | +but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
20 | +GNU General Public License for more details. | ||
21 | + | ||
22 | +You should have received a copy of the GNU General Public License | ||
23 | +along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
24 | +*/ | ||
25 | +package test | ||
26 | + | ||
27 | +import ( | ||
28 | + "os" | ||
29 | + "os/signal" | ||
30 | + "syscall" | ||
31 | + | ||
32 | + "gitlab.weird-web-workers.org/golang/logger" | ||
33 | +) | ||
34 | + | ||
35 | +type Signal interface { | ||
36 | + Hangup() | ||
37 | + Shutdown() | ||
38 | +} | ||
39 | + | ||
40 | +func Wait(sig Signal) { | ||
41 | + interrupt := make(chan os.Signal, 1) | ||
42 | + signal.Notify( | ||
43 | + interrupt, | ||
44 | + os.Interrupt, | ||
45 | + syscall.SIGTERM, | ||
46 | + syscall.SIGINT, | ||
47 | + syscall.SIGHUP, | ||
48 | + ) | ||
49 | + for run, ok := true, true; run && ok; { | ||
50 | + select { | ||
51 | + case s := <-interrupt: | ||
52 | + logger.Default.Info("Interrupted by signal: %s", sig) | ||
53 | + switch s { | ||
54 | + case syscall.SIGHUP: | ||
55 | + sig.Hangup() | ||
56 | + default: | ||
57 | + sig.Shutdown() | ||
58 | + run = false | ||
59 | + } | ||
60 | + } | ||
61 | + } | ||
62 | +} | ||
63 | + | ||
64 | +// vim: ts=4 sts=4 sw=4 noet tw=72: |
Please
register
or
login
to post a comment