Skip to content

Commit a33a6ec

Browse files
committed
chore: wip
1 parent cee9efd commit a33a6ec

File tree

5 files changed

+99
-69
lines changed

5 files changed

+99
-69
lines changed

app/Schedule.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { run } from '@stacksjs/scheduler'
22
import type { Scheduler } from '@stacksjs/types'
33

44
export default function scheduler() {
5-
run().command('bun /home/some/script.js').everySecond()
6-
run.command('bun /home/some/other/script.ts').everyMinute()
7-
run.action('./actions/SomeAction.ts').everyFiveMinutes() // could use a better dummy example 😅
8-
run.job('./jobs/DummyJob.ts').everyTenMinutes()
9-
run.exec('bun /home/some/script.ts').everyMinute()
10-
run.call(() => {
11-
// ...
12-
}).weekly().mondays().at('13:00').timezone('America/Los_Angeles')
5+
// run.command('bun /home/some/script.js').everySecond()
6+
// run.command('bun /home/some/other/script.ts').everyMinute()
7+
// run.action('./actions/SomeAction.ts').everyFiveMinutes() // could use a better dummy example 😅
8+
// run.job('./jobs/DummyJob.ts').everyTenMinutes()
9+
// run.exec('bun /home/some/script.ts').everyMinute()
10+
// run.call(() => {
11+
// // ...
12+
// }).weekly().mondays().at('13:00').timezone('America/Los_Angeles')
1313
}

storage/framework/core/cloud/src/cloud/compute.ts

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-new */
22
import type { aws_certificatemanager as acm, aws_ec2 as ec2, aws_efs as efs, aws_route53 as route53 } from 'aws-cdk-lib'
3-
import { Duration, CfnOutput as Output, aws_lambda as lambda, aws_logs as logs, aws_secretsmanager as secretsmanager } from 'aws-cdk-lib'
3+
import { Duration, CfnOutput as Output, aws_ecs as ecs, aws_ecs_patterns as ecs_patterns, aws_lambda as lambda, aws_logs as logs, aws_secretsmanager as secretsmanager } from 'aws-cdk-lib'
44
import type { Construct } from 'constructs'
55
import { path as p } from '@stacksjs/path'
66
import { env } from '@stacksjs/env'
@@ -29,43 +29,78 @@ export class ComputeStack {
2929
// directory: p.cloudPath('src/server'),
3030
// })
3131

32-
this.apiServer = new lambda.Function(scope, 'WebServer', {
33-
functionName: `${props.slug}-${props.appEnv}-web-server`,
34-
description: 'The web server for the Stacks application',
35-
code: lambda.Code.fromAssetImage(p.frameworkPath('server')),
36-
handler: lambda.Handler.FROM_IMAGE,
37-
runtime: lambda.Runtime.FROM_IMAGE,
32+
const cluster = new ecs.Cluster(scope, 'StacksCluster', {
3833
vpc,
39-
memorySize: 512, // replace with your actual memory size
40-
timeout: Duration.minutes(5), // replace with your actual timeout
41-
logRetention: logs.RetentionDays.ONE_WEEK,
42-
architecture: lambda.Architecture.ARM_64,
43-
// filesystem: lambda.FileSystem.fromEfsAccessPoint(props.accessPoint, '/mnt/efs'),
4434
})
4535

46-
const keysToRemove = ['_HANDLER', '_X_AMZN_TRACE_ID', 'AWS_REGION', 'AWS_EXECUTION_ENV', 'AWS_LAMBDA_FUNCTION_NAME', 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'AWS_LAMBDA_FUNCTION_VERSION', 'AWS_LAMBDA_INITIALIZATION_TYPE', 'AWS_LAMBDA_LOG_GROUP_NAME', 'AWS_LAMBDA_LOG_STREAM_NAME', 'AWS_ACCESS_KEY', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_SESSION_TOKEN', 'AWS_LAMBDA_RUNTIME_API', 'LAMBDA_TASK_ROOT', 'LAMBDA_RUNTIME_DIR', '_']
47-
keysToRemove.forEach(key => delete env[key as EnvKey])
36+
const taskDefinition = new ecs.FargateTaskDefinition(scope, 'TaskDef', {
37+
memoryLimitMiB: 512, // Match your Lambda memory size
38+
cpu: 256, // Choose an appropriate value
39+
})
4840

49-
const secrets = new secretsmanager.Secret(scope, 'StacksSecrets', {
50-
secretName: `${props.slug}-${props.appEnv}-secrets`,
51-
description: 'Secrets for the Stacks application',
52-
generateSecretString: {
53-
secretStringTemplate: JSON.stringify(env),
54-
generateStringKey: Object.keys(env).join(',').length.toString(),
55-
},
41+
const container = taskDefinition.addContainer('WebServerContainer', {
42+
image: ecs.ContainerImage.fromAsset(p.frameworkPath('server')),
43+
// You can add environment variables, logging, etc., here
5644
})
5745

58-
secrets.grantRead(this.apiServer)
59-
this.apiServer.addEnvironment('SECRETS_ARN', secrets.secretArn)
46+
const fargateService = new ecs_patterns.ApplicationLoadBalancedFargateService(scope, 'FargateService', {
47+
cluster,
48+
taskDefinition,
49+
desiredCount: 1, // Start with 1 task instance
50+
// Other configurations like public load balancer, domain name, etc.
51+
})
6052

61-
this.apiServerUrl = new lambda.FunctionUrl(scope, 'StacksServerUrl', {
62-
function: this.apiServer,
63-
authType: lambda.FunctionUrlAuthType.NONE, // becomes a public API
64-
cors: {
65-
allowedOrigins: ['*'],
53+
const volumeName = `${props.slug}-${props.appEnv}-efs`
54+
taskDefinition.addVolume({
55+
name: volumeName,
56+
efsVolumeConfiguration: {
57+
fileSystemId: props.fileSystem.fileSystemId,
6658
},
6759
})
6860

61+
container.addMountPoints({
62+
sourceVolume: volumeName,
63+
containerPath: '/mnt/efs',
64+
readOnly: false,
65+
})
66+
67+
// this.apiServer = new lambda.Function(scope, 'WebServer', {
68+
// functionName: `${props.slug}-${props.appEnv}-web-server`,
69+
// description: 'The web server for the Stacks application',
70+
// code: lambda.Code.fromAssetImage(p.frameworkPath('server')),
71+
// handler: lambda.Handler.FROM_IMAGE,
72+
// runtime: lambda.Runtime.FROM_IMAGE,
73+
// vpc,
74+
// memorySize: 512, // replace with your actual memory size
75+
// timeout: Duration.minutes(5), // replace with your actual timeout
76+
// logRetention: logs.RetentionDays.ONE_WEEK,
77+
// architecture: lambda.Architecture.ARM_64,
78+
// // filesystem: lambda.FileSystem.fromEfsAccessPoint(props.accessPoint, '/mnt/efs'),
79+
// })
80+
81+
const keysToRemove = ['_HANDLER', '_X_AMZN_TRACE_ID', 'AWS_REGION', 'AWS_EXECUTION_ENV', 'AWS_LAMBDA_FUNCTION_NAME', 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'AWS_LAMBDA_FUNCTION_VERSION', 'AWS_LAMBDA_INITIALIZATION_TYPE', 'AWS_LAMBDA_LOG_GROUP_NAME', 'AWS_LAMBDA_LOG_STREAM_NAME', 'AWS_ACCESS_KEY', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_SESSION_TOKEN', 'AWS_LAMBDA_RUNTIME_API', 'LAMBDA_TASK_ROOT', 'LAMBDA_RUNTIME_DIR', '_']
82+
keysToRemove.forEach(key => delete env[key as EnvKey])
83+
84+
// const secrets = new secretsmanager.Secret(scope, 'StacksSecrets', {
85+
// secretName: `${props.slug}-${props.appEnv}-secrets`,
86+
// description: 'Secrets for the Stacks application',
87+
// generateSecretString: {
88+
// secretStringTemplate: JSON.stringify(env),
89+
// generateStringKey: Object.keys(env).join(',').length.toString(),
90+
// },
91+
// })
92+
93+
// secrets.grantRead(this.apiServer)
94+
// this.apiServer.addEnvironment('SECRETS_ARN', secrets.secretArn)
95+
96+
// this.apiServerUrl = new lambda.FunctionUrl(scope, 'StacksServerUrl', {
97+
// function: this.apiServer,
98+
// authType: lambda.FunctionUrlAuthType.NONE, // becomes a public API
99+
// cors: {
100+
// allowedOrigins: ['*'],
101+
// },
102+
// })
103+
69104
const apiPrefix = 'api'
70105
new Output(scope, 'ApiUrl', {
71106
value: `https://${props.domain}/${apiPrefix}`,
Binary file not shown.

storage/framework/server/Dockerfile

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
# Builder image
2-
FROM oven/bun:latest AS builder
3-
WORKDIR /tmp
1+
FROM oven/bun
2+
WORKDIR /srv
43

5-
COPY runtime.ts index.ts package.json .
4+
# Add the package manifest and install packages
5+
ADD package.json .
6+
RUN bun install
67

7-
RUN bun install @stacksjs/router aws4fetch
8-
RUN bun build --compile runtime.ts --outfile bootstrap
9-
RUN bun build --target=bun index.ts --outfile index --external @stacksjs/router
8+
# Add the application code
9+
ADD index.ts .
1010

11-
# Runtime image, includes Lambda RIC
12-
FROM public.ecr.aws/lambda/provided:al2
13-
14-
COPY --from=builder /tmp/index ${LAMBDA_TASK_ROOT}
15-
COPY --from=builder /tmp/node_modules ${LAMBDA_TASK_ROOT}
16-
# Copy bun + runtime.ts into the runtime directory
17-
COPY --from=builder /tmp/bootstrap ${LAMBDA_RUNTIME_DIR}
18-
19-
# Set our handler method
20-
CMD ["index.fetch"]
11+
# Specify the command to run when launching the container
12+
CMD bun index.ts

storage/framework/server/index.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import type { Server } from 'bun'
1+
import type { Server, ServerWebSocket } from 'bun'
22
import { serverResponse } from '@stacksjs/router'
33

4-
export default {
4+
const server = Bun.serve({
5+
port: 3000,
56
async fetch(request: Request, server: Server): Promise<Response | undefined> {
67
// eslint-disable-next-line no-console
78
console.log('Request', {
@@ -19,21 +20,23 @@ export default {
1920

2021
return serverResponse(request)
2122
},
22-
2323
websocket: {
24-
// async open(ws: ServerWebSocket): Promise<void> {
25-
// // eslint-disable-next-line no-console
26-
// console.log('WebSocket opened')
27-
// },
24+
async open(ws: ServerWebSocket): Promise<void> {
25+
// eslint-disable-next-line no-console
26+
console.log('WebSocket opened')
27+
},
2828

29-
// async message(ws: ServerWebSocket, message: string): Promise<void> {
30-
// // eslint-disable-next-line no-console
31-
// console.log('WebSocket message', message)
32-
// },
29+
async message(ws: ServerWebSocket, message: string): Promise<void> {
30+
// eslint-disable-next-line no-console
31+
console.log('WebSocket message', message)
32+
},
3333

34-
// async close(ws: ServerWebSocket, code: number, reason?: string): Promise<void> {
35-
// // eslint-disable-next-line no-console
36-
// console.log('WebSocket closed', { code, reason })
37-
// },
34+
async close(ws: ServerWebSocket, code: number, reason?: string): Promise<void> {
35+
// eslint-disable-next-line no-console
36+
console.log('WebSocket closed', { code, reason })
37+
},
3838
},
39-
}
39+
})
40+
41+
// eslint-disable-next-line no-console
42+
console.log(`Listening on http://localhost:${server.port} ...`)

0 commit comments

Comments
 (0)