-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest.go
More file actions
executable file
·55 lines (42 loc) · 1.14 KB
/
rest.go
File metadata and controls
executable file
·55 lines (42 loc) · 1.14 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package servers
import (
"context"
"errors"
"fmt"
"net/http"
"time"
)
// ErrRESTStart is returned when an error occurs the REST server.
var ErrRESTStart = errors.New("start REST server")
// REST is a listening HTTP server instance.
type REST struct {
*Server
httpServer *http.Server
}
// NewREST constructs a new rest Server.
func NewREST(config Config, handler http.Handler, opts ...Option) *REST {
srv := REST{}
srv.Server = NewServer(config, opts...)
srv.httpServer = &http.Server{
Handler: handler,
ReadTimeout: 400 * time.Millisecond,
}
return &srv
}
// Start starts serving the REST server.
func (srv *REST) Start() error {
if err := srv.Server.Start(); err != nil {
return err
}
if err := srv.serve(srv.httpServer, srv.listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("%w: %v addr %v", ErrRESTStart, err, srv.listener.Addr().String()) //nolint:errorlint
}
return nil
}
// Stop gracefully shuts down the REST server.
func (srv *REST) Stop() {
if err := srv.httpServer.Shutdown(context.Background()); err != nil {
srv.httpServer.Close() //nolint:errcheck,gosec
}
srv.Server.Stop()
}