forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
141 lines (106 loc) · 5.3 KB
/
router.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
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package rapi
import (
"net/http"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/appctx"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/rapi/handler"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/rapi/middleware"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/telemetry"
"github.com/go-chi/chi"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/core"
"github.com/rymurr/aws-lambda-runtime-interface-emulator/lambda/rapi/rendering"
)
// NewRouter returns a new instance of chi router implementing
// Runtime API specification.
func NewRouter(appCtx appctx.ApplicationContext, registrationService core.RegistrationService, renderingService *rendering.EventRenderingService) http.Handler {
router := chi.NewRouter()
router.Use(middleware.AppCtxMiddleware(appCtx))
router.Use(middleware.AccessLogMiddleware())
router.Use(middleware.RuntimeReleaseMiddleware())
// To respect Hyrum's Law, keeping /ping API even though
// we no longer use it ourselves.
// http://www.hyrumslaw.com/
router.Get("/ping", handler.NewPingHandler().ServeHTTP)
router.Get("/runtime/invocation/next",
handler.NewInvocationNextHandler(registrationService, renderingService).ServeHTTP)
// Note, request validation must happen before state
// transition. State machine transitions are irreversible
// at the moment.
router.Post("/runtime/invocation/{awsrequestid}/response",
middleware.AwsRequestIDValidator(
handler.NewInvocationResponseHandler(registrationService)).ServeHTTP)
router.Post("/runtime/invocation/{awsrequestid}/error",
middleware.AwsRequestIDValidator(
handler.NewInvocationErrorHandler(registrationService)).ServeHTTP)
router.Post("/runtime/init/error", handler.NewInitErrorHandler(registrationService).ServeHTTP)
if appctx.LoadInitType(appCtx) == appctx.InitCaching {
router.Get("/runtime/restore/next", handler.NewRestoreNextHandler(registrationService, renderingService).ServeHTTP)
router.Post("/runtime/restore/error", handler.NewRestoreErrorHandler(registrationService).ServeHTTP)
}
return router
}
// ExtensionsRouter returns a new instance of chi router implementing
// Extensions Runtime API specification.
func ExtensionsRouter(appCtx appctx.ApplicationContext, registrationService core.RegistrationService, renderingService *rendering.EventRenderingService) http.Handler {
router := chi.NewRouter()
router.Use(middleware.AccessLogMiddleware())
router.Use(middleware.AllowIfExtensionsEnabled)
router.Use(middleware.AppCtxMiddleware(appCtx))
registerHandler := handler.NewAgentRegisterHandler(registrationService)
router.Post("/extension/register",
registerHandler.ServeHTTP)
router.Get("/extension/event/next",
middleware.AgentUniqueIdentifierHeaderValidator(
handler.NewAgentNextHandler(registrationService, renderingService)).ServeHTTP)
router.Post("/extension/init/error",
middleware.AgentUniqueIdentifierHeaderValidator(
handler.NewAgentInitErrorHandler(registrationService)).ServeHTTP)
router.Post("/extension/exit/error",
middleware.AgentUniqueIdentifierHeaderValidator(
handler.NewAgentExitErrorHandler(registrationService)).ServeHTTP)
return router
}
// LogsAPIRouter returns a new instance of chi router implementing
// Logs API specification.
func LogsAPIRouter(registrationService core.RegistrationService, logsSubscriptionAPI telemetry.SubscriptionAPI) http.Handler {
router := chi.NewRouter()
router.Use(middleware.AccessLogMiddleware())
router.Use(middleware.AllowIfExtensionsEnabled)
router.Put("/logs",
middleware.AgentUniqueIdentifierHeaderValidator(
handler.NewRuntimeTelemetrySubscriptionHandler(registrationService, logsSubscriptionAPI)).ServeHTTP)
return router
}
// LogsAPIStubRouter returns a new instance of chi router implementing
// a stub of Logs API that always returns a non-committal response to
// prevent customer code from crashing when Logs API is disabled locally
func LogsAPIStubRouter() http.Handler {
router := chi.NewRouter()
router.Put("/logs", handler.NewRuntimeLogsAPIStubHandler().ServeHTTP)
return router
}
// TelemetryRouter returns a new instance of chi router implementing
// Telemetry API specification.
func TelemetryAPIRouter(registrationService core.RegistrationService, telemetrySubscriptionAPI telemetry.SubscriptionAPI) http.Handler {
router := chi.NewRouter()
router.Use(middleware.AccessLogMiddleware())
router.Use(middleware.AllowIfExtensionsEnabled)
router.Put("/telemetry",
middleware.AgentUniqueIdentifierHeaderValidator(
handler.NewRuntimeTelemetrySubscriptionHandler(registrationService, telemetrySubscriptionAPI)).ServeHTTP)
return router
}
// TelemetryStubRouter returns a new instance of chi router implementing
// a stub of Telemetry API that always returns a non-committal response to
// prevent customer code from crashing when Telemetry API is disabled locally
func TelemetryAPIStubRouter() http.Handler {
router := chi.NewRouter()
router.Put("/telemetry", handler.NewRuntimeTelemetryAPIStubHandler().ServeHTTP)
return router
}
func CredentialsAPIRouter(credentialsService core.CredentialsService) http.Handler {
router := chi.NewRouter()
router.Get("/credentials", handler.NewCredentialsHandler(credentialsService).ServeHTTP)
return router
}