This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: hyperdrive initial progress
- Loading branch information
Showing
10 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
|
||
# sst | ||
.sst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
# sst | ||
.sst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM cloudflare/cloudflared:latest | ||
|
||
CMD ["tunnel", "run"] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import postgres from "postgres"; | ||
import { Resource } from "sst"; | ||
|
||
const sql = postgres( | ||
`postgres://${Resource.Postgres.username}:${Resource.Postgres.password}@${Resource.Postgres.host}:${Resource.Postgres.port}/${Resource.Postgres.database}`, | ||
); | ||
|
||
export async function handler() { | ||
try { | ||
// A very simple test query | ||
const now = Date.now(); | ||
const result = await sql`select * from pg_tables limit 10`; | ||
const delay = Date.now() - now; | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
result, | ||
delay, | ||
}), | ||
}; | ||
} catch (e) { | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
error: e.message, | ||
}), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "hyperdrive", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"dependencies": { | ||
"@cloudflare/workers-types": "^4.20240919.0", | ||
"postgres": "^3.4.4", | ||
"sst": "3.1.29" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* This file is auto-generated by SST. Do not edit. */ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
import "sst" | ||
export {} | ||
import "sst" | ||
declare module "sst" { | ||
export interface Resource { | ||
"Cloudflared": { | ||
"service": string | ||
"type": "sst.aws.Service" | ||
} | ||
"Lambda": { | ||
"name": string | ||
"type": "sst.aws.Function" | ||
"url": string | ||
} | ||
"Postgres": { | ||
"clusterArn": string | ||
"database": string | ||
"host": string | ||
"password": string | ||
"port": number | ||
"secretArn": string | ||
"type": "sst.aws.Postgres" | ||
"username": string | ||
} | ||
"Vpc": { | ||
"type": "sst.aws.Vpc" | ||
} | ||
} | ||
} | ||
// cloudflare | ||
import * as cloudflare from "@cloudflare/workers-types"; | ||
declare module "sst" { | ||
export interface Resource { | ||
"Worker": cloudflare.Service | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/// <reference path="./.sst/platform/config.d.ts" /> | ||
export default $config({ | ||
app(input) { | ||
return { | ||
name: "hyperdrive", | ||
removal: input?.stage === "production" ? "retain" : "remove", | ||
home: "aws", | ||
providers: { cloudflare: "5.39.0", random: "4.16.5" }, | ||
}; | ||
}, | ||
async run() { | ||
// THIS EXAMPLE IS NOT READY YET | ||
|
||
const vpc = new sst.aws.Vpc("Vpc", { | ||
nat: "managed", | ||
}); | ||
const cluster = new sst.aws.Cluster("Cluster", { vpc }); | ||
const postgres = new sst.aws.Postgres("Postgres", { vpc }); | ||
const domain = "sst.cheap"; | ||
const zone = cloudflare.getZoneOutput({ name: domain }); | ||
|
||
const tunnelSecret = new random.RandomString("TunnelSecret", { | ||
length: 32, | ||
}); | ||
const tunnel = new cloudflare.Tunnel("Tunnel", { | ||
name: `${$app.name}-${$app.stage}-tunnel`, | ||
secret: tunnelSecret.result.apply((v) => | ||
Buffer.from(v).toString("base64"), | ||
), | ||
accountId: sst.cloudflare.DEFAULT_ACCOUNT_ID, | ||
}); | ||
new cloudflare.TunnelConfig("TunnelConfig", { | ||
accountId: sst.cloudflare.DEFAULT_ACCOUNT_ID, | ||
tunnelId: tunnel.id, | ||
config: { | ||
ingressRules: [ | ||
{ | ||
service: $interpolate`tcp://${postgres.host}:${postgres.port}`, | ||
}, | ||
], | ||
}, | ||
}); | ||
const record = new cloudflare.Record("TunnelRecord", { | ||
name: $interpolate`hypedrive.${domain}`, | ||
zoneId: zone.id, | ||
type: "CNAME", | ||
value: $interpolate`${tunnel.id}.cfargotunnel.com`, | ||
proxied: true, | ||
}); | ||
const hyperdriveConfig = new cloudflare.HyperdriveConfig( | ||
"HyperdriveConfig", | ||
{ | ||
name: `${$app.name}-${$app.stage}-config`, | ||
accountId: sst.cloudflare.DEFAULT_ACCOUNT_ID, | ||
origin: { | ||
host: record.name, | ||
user: postgres.username, | ||
password: postgres.password, | ||
database: postgres.database, | ||
accessClientId: "dummy", | ||
accessClientSecret: "dummy", | ||
scheme: "postgres", | ||
}, | ||
}, | ||
); | ||
cluster.addService("Cloudflared", { | ||
environment: { | ||
TUNNEL_TOKEN: tunnel.tunnelToken, | ||
}, | ||
}); | ||
const worker = new sst.cloudflare.Worker("Worker", { | ||
handler: "worker.ts", | ||
url: true, | ||
transform: { | ||
worker: { | ||
placements: [ | ||
{ | ||
mode: "smart", | ||
}, | ||
], | ||
hyperdriveConfigBindings: [ | ||
{ | ||
binding: "HYPERDRIVE", | ||
id: "7c064ebd005348329a38106b076d579d", | ||
}, | ||
], | ||
}, | ||
}, | ||
}); | ||
|
||
const lambda = new sst.aws.Function("Lambda", { | ||
handler: "lambda.handler", | ||
link: [postgres], | ||
vpc, | ||
url: true, | ||
}); | ||
|
||
return { | ||
worker: worker.url, | ||
lambda: lambda.url, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import postgres from "postgres"; | ||
import { Hyperdrive, ExecutionContext } from "@cloudflare/workers-types"; | ||
|
||
export interface Env { | ||
// If you set another name in wrangler.toml as the value for 'binding', | ||
// replace "HYPERDRIVE" with the variable name you defined. | ||
HYPERDRIVE: Hyperdrive; | ||
} | ||
|
||
export default { | ||
async fetch(request: Request, env: Env, ctx: ExecutionContext) { | ||
// NOTE: if `prepare: false` is passed when connecting, performance will | ||
// be slower but still correctly supported. | ||
const sql = postgres(env.HYPERDRIVE.connectionString); | ||
|
||
try { | ||
// A very simple test query | ||
const now = Date.now(); | ||
const result = await sql`select * from pg_tables limit 10`; | ||
const delay = Date.now() - now; | ||
|
||
// Clean up the client, ensuring we don't kill the worker before that is | ||
// completed. | ||
ctx.waitUntil(sql.end()); | ||
|
||
// Return result rows as JSON | ||
return Response.json({ delay, result }); | ||
} catch (e) { | ||
console.log(e); | ||
return Response.json({ error: e.message }, { status: 500 }); | ||
} | ||
}, | ||
}; |