Skip to content

Commit 9e2ff4e

Browse files
authored
Merge pull request #12 from mococa/types/enhanced-types-and-added-configurations-to-zipping-util
🔍 types: enhanced types and added configurations to zipping util
2 parents 95750c5 + f588f74 commit 9e2ff4e

File tree

10 files changed

+245
-116
lines changed

10 files changed

+245
-116
lines changed

infrastructure/README.md

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
11
## Infrastructure folder
22

33
Here, it lays the CDK App. Where the magic happens.
4-
There is a a `src/index.ts` file, there is a CDK app being instantiated and the nullstack stack being called passing some props to it.
4+
There is a a `testing/index.ts` file, there is a CDK app being instantiated and creating 3 nullstack apps.
55

6-
What the stack, that is located at `lib/null-stack.ts`, does is:
6+
What the construct, that is located at `lib/nullstack-construct.ts`, does is:
77

8-
1. It creates an s3 bucket to host all the public files
9-
2. It creates a private s3 bucket to host the build files. in it, there will be a folder, with an index.js, where it uses [@vendia/serverless-express](https://github.com/vendia/serverless-express), where it gets the Express server in Nullstack and converts it into a lambda function; in this folder there is also the node_modules of this external package, and a .production folder from nullstack (changed server.js in it removing the crossorigin attribute - [explained here](https://github.com/nullstack/nullstack/pull/355))
10-
3. It deploys the necessary files into these buckets
11-
4. It creates a lambda that will run the files in the build bucket
12-
5. It creates an API Gateway with the route `/` and the route `/{proxy+}` for the rest of the routes.
13-
6. It creates a lambda function url for it to be accessible.
14-
15-
### Scripts
16-
17-
You can run `yarn deploy` and it will do everything it takes to deploy the application.
18-
19-
# Welcome to your CDK TypeScript project
20-
21-
This is a blank project for CDK development with TypeScript.
22-
23-
The `cdk.json` file tells the CDK Toolkit how to execute your app.
24-
25-
## Useful commands
26-
27-
- `npm run build` compile typescript to js
28-
- `npm run watch` watch for changes and compile
29-
- `npm run test` perform the jest unit tests
30-
- `cdk deploy` deploy this stack to your default AWS account/region
31-
- `cdk diff` compare deployed stack with current state
32-
- `cdk synth` emits the synthesized CloudFormation template
8+
1. It creates an s3 bucket to host all the public files.
9+
2. (SSR only) It creates a private s3 bucket to host the build files. in it, there will be a folder, with an index.js, where it uses [@vendia/serverless-express](https://github.com/vendia/serverless-express), and gets the Express server in Nullstack and converts it into a lambda function; in this folder there is also the node_modules of this external package, and a .production folder from nullstack (changed server.js in it removing the crossorigin attribute - [explained here](https://github.com/nullstack/nullstack/pull/355), but got merged on [Nullstack v0.19.2](https://github.com/nullstack/nullstack/releases/tag/v0.19.2)).
10+
3. It deploys the necessary files into these buckets.
11+
4. (SSR only) It creates a lambda that will run the files in the build bucket
12+
5. (SSR only) It creates an API Gateway with the route `/` and the route `/{proxy+}` for the rest of the routes.
13+
6. (SSR only) It creates a lambda function url for it to be accessible.
14+
7. (SSG and SPA only) It creates an s3 website url for it to be accessible.

infrastructure/cdk.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"app": "npx ts-node --prefer-ts-exts test/index.ts",
2+
"app": "npx ts-node --prefer-ts-exts testing/index.ts",
33
"watch": {
44
"include": [
55
"**"

infrastructure/lib/null-stack.ts renamed to infrastructure/lib/nullstack-construct.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface GetBucket extends s3.BucketProps {
1717
environment: string;
1818
}
1919

20-
interface Bucket {
20+
export interface AppBucket {
2121
/**
2222
* Bucket resource ID
2323
*/
@@ -36,7 +36,7 @@ type SSG = {
3636
/**
3737
* Bucket to host all files generated in the SSG build
3838
*/
39-
bucket: Bucket;
39+
bucket: AppBucket;
4040

4141
/**
4242
* Built Nullstack application SSG directory
@@ -54,7 +54,7 @@ type SPA = {
5454
/**
5555
* Bucket to host all files generated in the SPA build
5656
*/
57-
bucket: Bucket;
57+
bucket: AppBucket;
5858

5959
/**
6060
* Built Nullstack application SPA directory
@@ -72,12 +72,12 @@ type SSR = {
7272
/**
7373
* Bucket to host a zip file containing all the build assets
7474
*/
75-
build_bucket: Bucket;
75+
build_bucket: AppBucket;
7676

7777
/**
7878
* Bucket to host all the Nullstack public folder
7979
*/
80-
public_bucket: Bucket;
80+
public_bucket: AppBucket;
8181

8282
/**
8383
* Nullstack application directory
@@ -89,7 +89,9 @@ type SSR = {
8989
app_dir: string;
9090
};
9191

92-
type Props = cdk.StackProps & {
92+
export type BuildType = SSG | SSR | SPA;
93+
94+
export type StackProps = BuildType & {
9395
/**
9496
* Environment
9597
*
@@ -99,13 +101,20 @@ type Props = cdk.StackProps & {
99101
*/
100102
environment: string;
101103

104+
/**
105+
* Region
106+
*
107+
* Example: "sa-east-1"
108+
*/
109+
region: string;
110+
102111
/**
103112
* Nullstack application enviroment variables
104113
*/
105114
app_env: Record<string, string>;
106-
} & (SSG | SSR | SPA);
115+
} & BuildType;
107116

108-
export class NullstackAppStack extends cdk.Stack {
117+
export class NullstackAppConstruct extends Construct {
109118
/* ---------- Helpers ---------- */
110119
getBucket({
111120
bucket_name,
@@ -145,8 +154,8 @@ export class NullstackAppStack extends cdk.Stack {
145154
*/
146155
lambda_function: cdk.aws_lambda.Function;
147156

148-
constructor(scope: Construct, id: string, props: Props) {
149-
super(scope, id, props);
157+
constructor(scope: Construct, id: string, props: StackProps) {
158+
super(scope, id);
150159

151160
/* ---------- Constants ---------- */
152161
const { environment, build_type, app_env } = props;
@@ -240,7 +249,7 @@ export class NullstackAppStack extends cdk.Stack {
240249
/* ----------
241250
* Lambdas
242251
* -------- */
243-
const cdn = `https://${props.public_bucket.name}-${environment}.s3.${props.env?.region}.amazonaws.com`;
252+
const cdn = `https://${props.public_bucket.name}-${environment}.s3.${props.region}.amazonaws.com`;
244253

245254
// Create lambda that will the build bucket assets
246255
const lambda_function = new lambda.Function(
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
const serverless = require("@vendia/serverless-express");
22
const { server } = require("./.production/server").default;
33

4-
server.less = Boolean(process.env.LAMBDA);
5-
6-
if (!process.env.LAMBDA)
7-
return server.listen(3000, () => {
8-
console.log("Server is listening on port 3000");
9-
});
4+
server.less = Boolean(process.env.LAMBDA === "true");
105

116
exports.handler = serverless({ app: server, trimStageFromRequestPath: true });
7+
8+
if (!server.less) server.listen(3000, () => console.log("Listening on :3000"));

infrastructure/package.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,13 @@
2525
"scripts": {
2626
"build": "tsc --build tsconfig.json",
2727
"watch": "tsc -w",
28-
"test": "echo \"Error: no test specified\" && exit 1",
29-
"prepublishOnly": "npm run build",
30-
"prebootstrap": "yarn synth",
31-
"bootstrap": "cdk bootstrap",
32-
"predeploy": "yarn bootstrap",
33-
"deploy": "cdk deploy --require-approval never",
34-
"destroy": "cdk destroy --force true",
35-
"prereup": "yarn destroy",
36-
"reup": "yarn deploy"
28+
"prepublishOnly": "npm run build"
3729
},
3830
"devDependencies": {
3931
"@types/adm-zip": "^0.5.0",
4032
"@types/node": "^16.14.0",
4133
"aws-cdk": "2.82.0",
42-
"ts-node": "^10.9.1",
43-
"typescript": "~4.4.2"
34+
"ts-node": "^10.9.1"
4435
},
4536
"peerDependencies": {
4637
"aws-cdk": "2.82.0",

infrastructure/src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
import { NullstackAppStack } from "../lib/null-stack";
1+
import {
2+
NullstackAppConstruct,
3+
AppBucket,
4+
BuildType as StackBuildType,
5+
StackProps as Props,
6+
} from "../lib/null-stack";
7+
28
import { zip_nullstack } from "../utils/zip_nullstack";
39

4-
export { NullstackAppStack, zip_nullstack };
10+
export namespace NullstackProps {
11+
export type NullstackAppBucket = AppBucket;
12+
export type BuildType = StackBuildType;
13+
export type StackProps = Props;
14+
}
15+
16+
export { zip_nullstack, NullstackAppConstruct };

infrastructure/test/index.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

infrastructure/testing/index.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as cdk from "aws-cdk-lib";
2+
import path from "path";
3+
4+
import { NullstackAppConstruct, NullstackProps, zip_nullstack } from "../src";
5+
6+
const app = new cdk.App();
7+
const app_dir = path.join("..", "web");
8+
9+
const ssg_bucket: NullstackProps.NullstackAppBucket = {
10+
id: "Nullstack-App-Test-SSG-Build",
11+
name: "nullstack-app-test-ssg-build",
12+
};
13+
14+
const ssr_buckets = {
15+
public_bucket: {
16+
id: "Nullstack-App-Test-SSR-CDN",
17+
name: "nullstack-app-test-ssr-cdn",
18+
},
19+
build_bucket: {
20+
id: "Nullstack-App-Test-SSR-Build",
21+
name: "nullstack-app-test-ssr-build",
22+
},
23+
};
24+
25+
const spa_bucket: NullstackProps.NullstackAppBucket = {
26+
id: "Nullstack-App-Test-SPA-Build",
27+
name: "nullstack-app-test-spa-build",
28+
};
29+
30+
const ssg_props: NullstackProps.StackProps = {
31+
environment: "development",
32+
bucket: ssg_bucket,
33+
region: "sa-east-1",
34+
build_dir: path.join("..", "web", "ssg"),
35+
app_env: {
36+
NULLSTACK_PROJECT_NAME: "[dev] Web",
37+
NULLSTACK_PROJECT_COLOR: "#D22365",
38+
},
39+
build_type: "ssg",
40+
};
41+
42+
const ssr_props: NullstackProps.StackProps = {
43+
environment: "development",
44+
app_dir,
45+
...ssr_buckets,
46+
region: "sa-east-1",
47+
app_env: {
48+
NULLSTACK_PROJECT_NAME: "[dev] Web",
49+
NULLSTACK_PROJECT_COLOR: "#D22365",
50+
},
51+
build_type: "ssr",
52+
};
53+
54+
const spa_props: NullstackProps.StackProps = {
55+
environment: "development",
56+
bucket: spa_bucket,
57+
region: "sa-east-1",
58+
build_dir: path.join("..", "web", "spa"),
59+
app_env: {
60+
NULLSTACK_PROJECT_NAME: "[dev] Web",
61+
NULLSTACK_PROJECT_COLOR: "#D22365",
62+
},
63+
build_type: "spa",
64+
};
65+
66+
(async () => {
67+
await zip_nullstack(app_dir);
68+
69+
const stack = new cdk.Stack(app, "NullstackAppStack", {
70+
env: { region: "sa-east-1" },
71+
});
72+
73+
new NullstackAppConstruct(stack, "NullstackAppStackTestSSG", ssg_props);
74+
new NullstackAppConstruct(stack, "NullstackAppStackTestSSR", ssr_props);
75+
new NullstackAppConstruct(stack, "NullstackAppStackTestSPA", spa_props);
76+
})();

0 commit comments

Comments
 (0)