-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtfaws.w
165 lines (141 loc) · 5 KB
/
tfaws.w
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
162
163
164
165
bring cloud;
bring aws;
bring ui;
bring util;
bring fs;
bring "@cdktf/provider-aws" as awsProvider;
bring "./types.w" as types;
struct BuildServiceResult {
routesFile: str;
specFile: str;
}
class Function impl std.IInflightHost {
pub var _getCodeLines: (cloud.IFunctionHandler): Array<str>;
pub var addEnvironment: (str, str): void;
new() {
this._getCodeLines = (handler) => {return Array<str>[];};
this.addEnvironment = (name, key) => {};
}
}
class TSOAFunction {
_env: MutMap<str>;
pub fn: Function;
requires: Map<str>;
new(requires: Map<str>, handler: inflight (Json?, Json?, Map<std.Resource>?): Json?) {
this.requires = requires;
this._env = MutMap<str>{};
this.fn = unsafeCast(new cloud.Function(unsafeCast(handler)));
this.fn._getCodeLines = unsafeCast(this._getCodeLines)?.bind(this);
this.fn.addEnvironment = this.addEnvironment;
}
pub addEnvironment(name: str, value: str) {
this._env.set(name, value);
}
protected _getCodeLines(handler: cloud.IFunctionHandler): Array<str> {
let inflightClient = unsafeCast(handler)?._toInflight();
let lines = MutArray<str>[];
let client = "$handler";
lines.push("\"use strict\";");
for req in this.requires.entries() {
lines.push("var {req.key} = require(\"{req.value}\");");
lines.push("global[\"{req.key}\"] = {req.key};");
}
lines.push("var {client} = undefined;");
lines.push("exports.handler = async function(event, context) \{");
lines.push(" {client} = {client} ?? ({inflightClient});");
lines.push(" return await {client}.handle(event, context);");
lines.push("};");
return lines.copy();
}
}
/**
* Starts a new TSOA service.
*/
pub class Service_tfaws impl types.IService {
func: TSOAFunction;
api: awsProvider.apiGatewayRestApi.ApiGatewayRestApi;
pub url: str;
pub specFile: str;
clients: MutMap<std.Resource>;
new(props: types.ServiceProps) {
let currentdir = Service_tfaws.dirname();
let entrypointDir = nodeof(this).app.entrypointDir;
let workDir = nodeof(this).app.workdir;
let homeEnv = util.tryEnv("HOME") ?? "";
let pathEnv = util.tryEnv("PATH") ?? "";
this.clients = MutMap<std.Resource>{};
let res = Service_tfaws.build(
currentdir: currentdir,
basedir: entrypointDir,
workdir: workDir,
options: props,
homeEnv: homeEnv,
pathEnv: pathEnv,
clients: this.clients.copy(),
);
this.func = new TSOAFunction({ RegisterRoutes: res.routesFile }, inflight (event, context) => {
return Service_tfaws.runHandler(event!, context!, this.clients);
});
let awsFn = aws.Function.from(this.func.fn)!;
let handlerInvokeArn: str = unsafeCast(awsFn)?.invokeArn;
this.api = new awsProvider.apiGatewayRestApi.ApiGatewayRestApi(
name: "wing-tsoa-api-{nodeof(this).addr.substring(42-8)}",
endpointConfiguration: {
types: ["EDGE"]
}
);
let proxy = new awsProvider.apiGatewayResource.ApiGatewayResource(
restApiId: this.api.id,
parentId: this.api.rootResourceId,
pathPart: "\{proxy+}"
) as "proxy resource";
let proxyMethod = new awsProvider.apiGatewayMethod.ApiGatewayMethod(
restApiId: this.api.id,
resourceId: proxy.id,
authorization: "NONE",
httpMethod: "ANY"
) as "proxy method";
let proxyIntegration = new awsProvider.apiGatewayIntegration.ApiGatewayIntegration(
httpMethod: proxyMethod.httpMethod,
resourceId: proxy.id,
restApiId: this.api.id,
type: "AWS_PROXY",
integrationHttpMethod: "POST",
uri: handlerInvokeArn,
) as "proxy integration";
let rootMethod = new awsProvider.apiGatewayMethod.ApiGatewayMethod(
restApiId: this.api.id,
resourceId: this.api.rootResourceId,
authorization: "NONE",
httpMethod: "ANY"
) as "root method";
let rootIntegration = new awsProvider.apiGatewayIntegration.ApiGatewayIntegration(
httpMethod: rootMethod.httpMethod,
resourceId: rootMethod.resourceId,
restApiId: this.api.id,
type: "AWS_PROXY",
integrationHttpMethod: "POST",
uri: handlerInvokeArn
) as "root integration";
let deploy = new awsProvider.apiGatewayDeployment.ApiGatewayDeployment(
restApiId: this.api.id,
dependsOn: [proxyIntegration, rootIntegration],
stageName: "prod",
);
new awsProvider.lambdaPermission.LambdaPermission(
action: "lambda:InvokeFunction",
functionName: awsFn.functionName,
principal: "apigateway.amazonaws.com",
sourceArn: "{this.api.executionArn}/*/*"
);
this.url = deploy.invokeUrl;
this.specFile = res.specFile;
}
pub lift(client: std.Resource, ops: types.LiftOptions) {
client.onLift(this.func.fn, ops.allow);
this.clients.set(ops.id, client);
}
extern "./lib.js" static build(options: types.StartServiceOptions): BuildServiceResult;
extern "./lib.js" static dirname(): str;
extern "./app-aws.js" inflight static runHandler(event: Json, context: Json, clients: Map<std.Resource>): Json;
}