-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
apiserver.go
82 lines (65 loc) · 1.75 KB
/
apiserver.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package apiserver
import (
"context"
"errors"
"net/http"
"time"
"github.com/gorilla/mux"
"go.uber.org/fx"
"github.com/DataDog/datadog-agent/cmd/process-agent/api"
"github.com/DataDog/datadog-agent/comp/core/log"
ddconfig "github.com/DataDog/datadog-agent/pkg/config"
)
var _ Component = (*apiserver)(nil)
type apiserver struct {
server *http.Server
}
type dependencies struct {
fx.In
Lc fx.Lifecycle
Log log.Component
APIServerDeps api.APIServerDeps
}
//nolint:revive // TODO(PROC) Fix revive linter
func newApiServer(deps dependencies) Component {
r := mux.NewRouter()
api.SetupAPIServerHandlers(deps.APIServerDeps, r) // Set up routes
addr, err := ddconfig.GetProcessAPIAddressPort()
if err != nil {
return err
}
deps.Log.Infof("API server listening on %s", addr)
timeout := time.Duration(ddconfig.Datadog.GetInt("server_timeout")) * time.Second
apiserver := &apiserver{
server: &http.Server{
Handler: r,
Addr: addr,
ReadTimeout: timeout,
WriteTimeout: timeout,
IdleTimeout: timeout,
},
}
deps.Lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
go func() {
err := apiserver.server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
_ = deps.Log.Error(err)
}
}()
return nil
},
OnStop: func(ctx context.Context) error {
err := apiserver.server.Shutdown(ctx)
if err != nil {
_ = deps.Log.Error("Failed to properly shutdown api server:", err)
}
return nil
},
})
return apiserver
}