-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
33 lines (29 loc) · 845 Bytes
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package stop
import (
"context"
"log"
"net/http"
"strconv"
)
//This method registers a handler on the /shutdown path, with the given HTTP server
func OnHttpCall(mux *http.ServeMux, f http.HandlerFunc) {
mux.HandleFunc("/shutdown", f)
}
//This method starts a background HTTP server on the given host and port.
//The given handler is registered on the /shutdown path.
//Once handling the call, the background HTTP server is shutdown
func OnHttpServerCall(host string, port int, f http.HandlerFunc) {
m := http.NewServeMux()
s := http.Server{Addr: host + ":" + strconv.Itoa(port), Handler: m}
OnHttpCall(m, func(writer http.ResponseWriter, request *http.Request) {
f(writer, request)
go func() {
s.Shutdown(context.Background())
}()
})
go func() {
if err := s.ListenAndServe(); err != nil {
log.Fatalln(err)
}
}()
}