forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
161 lines (131 loc) · 3.85 KB
/
server.go
File metadata and controls
161 lines (131 loc) · 3.85 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package rapi
import (
"context"
"fmt"
"net"
"net/http"
"github.com/go-chi/chi/v5"
"go.amzn.com/lambda/appctx"
"go.amzn.com/lambda/core"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapi/rendering"
"go.amzn.com/lambda/telemetry"
log "github.com/sirupsen/logrus"
)
const version20180601 = "/2018-06-01"
const version20200101 = "/2020-01-01"
const version20200815 = "/2020-08-15"
const version20210423 = "/2021-04-23"
const version20220701 = "/2022-07-01"
// Server is a Runtime API server
type Server struct {
host string
port int
server *http.Server
listener net.Listener
exit chan error
}
func SaveConnInContext(ctx context.Context, c net.Conn) context.Context {
return context.WithValue(ctx, interop.HTTPConnKey, c)
}
// NewServer creates a new Runtime API Server
//
// Unlike net/http server's ListenAndServe, we separate Listen()
// and Serve(), this is done to guarantee order: call to Listen()
// should happen before provided runtime is started.
//
// When port is 0, OS will dynamically allocate the listening port.
func NewServer(
host string,
port int,
appCtx appctx.ApplicationContext,
registrationService core.RegistrationService,
renderingService *rendering.EventRenderingService,
telemetryAPIEnabled bool,
logsSubscriptionAPI telemetry.SubscriptionAPI,
telemetrySubscriptionAPI telemetry.SubscriptionAPI,
credentialsService core.CredentialsService,
) *Server {
exitErrors := make(chan error, 1)
router := chi.NewRouter()
router.Mount(version20180601, NewRouter(appCtx, registrationService, renderingService))
router.Mount(version20200101, ExtensionsRouter(appCtx, registrationService, renderingService))
if telemetryAPIEnabled {
router.Mount(version20200815, LogsAPIRouter(registrationService, logsSubscriptionAPI))
router.Mount(version20220701, TelemetryAPIRouter(registrationService, telemetrySubscriptionAPI))
} else {
router.Mount(version20200815, LogsAPIStubRouter())
router.Mount(version20220701, TelemetryAPIStubRouter())
}
if appctx.LoadInitType(appCtx) == appctx.InitCaching {
router.Mount(version20210423, CredentialsAPIRouter(credentialsService))
}
return &Server{
host: host,
port: port,
server: &http.Server{Handler: router, ConnContext: SaveConnInContext},
listener: nil,
exit: exitErrors,
}
}
// Listen on port
func (s *Server) Listen() error {
addr := fmt.Sprintf("%s:%d", s.host, s.port)
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
s.listener = ln
if s.port == 0 {
s.port = ln.Addr().(*net.TCPAddr).Port
log.WithField("port", s.port).Info("Listening port was dynamically allocated")
}
log.Debugf("Runtime API Server listening on %s:%d", s.host, s.port)
return nil
}
// Serve requests and close on cancelation signals
func (s *Server) Serve(ctx context.Context) error {
defer s.Close()
select {
case err := <-s.serveAsync():
return err
case err := <-s.exit:
log.Errorf("Error triggered exit: %s", err)
return err
case <-ctx.Done():
return ctx.Err()
}
}
func (s *Server) serveAsync() chan error {
errors := make(chan error)
go func() {
errors <- s.server.Serve(s.listener)
}()
return errors
}
// Host is server's host
func (s *Server) Host() string {
return s.host
}
// Port is server's port
func (s *Server) Port() int {
return s.port
}
// URL is full server url for specified endpoint
func (s *Server) URL(endpoint string) string {
return fmt.Sprintf("http://%s:%d%s%s", s.Host(), s.Port(), version20180601, endpoint)
}
// Close forcefully closes listeners & connections
func (s *Server) Close() error {
err := s.server.Close()
if err == nil {
log.Info("Runtime API Server closed")
}
return err
}
// Shutdown gracefully shuts down server
func (s *Server) Shutdown() error {
return s.server.Shutdown(context.Background())
}