-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
36 lines (33 loc) · 1.06 KB
/
server.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
// Package server provides functions to set a webhook and start a server to process incoming requests.
package server
import (
"context"
"github.com/kozalosev/goSadTgBot/logconst"
log "github.com/sirupsen/logrus"
"net/http"
"time"
)
// Start a server listening on the specified port.
func Start(port string) *http.Server {
srv := &http.Server{Addr: ":" + port}
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.WithField(logconst.FieldFunc, "startServer").
WithField(logconst.FieldCalledObject, "Server").
WithField(logconst.FieldCalledMethod, "ListenAndServe").
Fatal(err)
}
}()
return srv
}
// StopListeningForIncomingRequests stops the server.
func StopListeningForIncomingRequests(srv *http.Server) {
ctx, c := context.WithTimeout(context.Background(), time.Minute)
defer c()
if err := srv.Shutdown(ctx); err != nil {
log.WithField(logconst.FieldFunc, "stopListeningForIncomingRequests").
WithField(logconst.FieldCalledObject, "Server").
WithField(logconst.FieldCalledMethod, "Shutdown").
Error(err)
}
}