/
http.go
40 lines (34 loc) · 920 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
34
35
36
37
38
39
40
package app
import (
"fmt"
"net/http"
"strconv"
"github.com/spyzhov/safe"
"go.uber.org/zap"
)
// Start HTTP handler
func (app *Application) RunHttp(handler *http.ServeMux, port int, name string) error {
app.WaitGroup.Add(1)
go func() {
defer app.WaitGroup.Done()
app.Logger.Info("http handler started",
zap.String("url", fmt.Sprintf("http://localhost:%d/", port)),
zap.String("name", name),
zap.Int("port", port))
server := &http.Server{
Addr: ":" + strconv.Itoa(port),
Handler: handler,
}
server.RegisterOnShutdown(app.CtxCancel)
defer safe.Close(server, "http handler '"+name+"' close error")
app.WaitGroup.Add(1)
go func() {
defer app.WaitGroup.Done()
app.Error(server.ListenAndServe())
app.Logger.Debug("http handler stops serve", zap.String("name", name))
}()
<-app.Ctx.Done()
app.Logger.Debug("http stops", zap.String("name", name))
}()
return nil
}