Skip to content

Commit

Permalink
sst stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Dec 9, 2024
1 parent bc25161 commit dad6d45
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 6 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/deploy_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
NEON_PROJECT_ID: ${{ secrets.NEON_PROJECT_ID }}
ELECTRIC_API: ${{ secrets.ELECTRIC_API }}
ELECTRIC_ADMIN_API: ${{ secrets.ELECTRIC_ADMIN_API }}
LINEARLITE_SUPABASE_PROJECT_ID: ${{ secrets.LINEARLITE_SUPABASE_PROJECT_ID }}
LINEARLITE_SUPABASE_PROJECT_PASSWORD: ${{ secrets.LINEARLITE_SUPABASE_PROJECT_PASSWORD }}
# HONEYCOMB_API_KEY: ${{ secrets.HONEYCOMB_API_KEY }} TODO

steps:
Expand All @@ -48,6 +50,18 @@ jobs:
restore-keys: |
sst-cache-main-${{ runner.os }}
# - name: Deploy Linearlite
# working-directory: examples/linearlite
# run: |
# pnpm sst deploy --stage ${{ env.DEPLOY_ENV }}
# if [ -f ".sst/outputs.json" ]; then
# linearlite=$(jq -r '.website' .sst/outputs.json)
# echo "linearlite=$linearlite" >> $GITHUB_ENV
# else
# echo "sst outputs file not found. Exiting."
# exit 1
# fi

- name: Deploy Linearlite Read Only
working-directory: examples/linearlite-read-only
run: |
Expand All @@ -60,7 +74,6 @@ jobs:
exit 1
fi
- name: Deploy NextJs example
working-directory: examples/nextjs-example
run: |
Expand All @@ -79,11 +92,13 @@ jobs:
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const linearlite = process.env.linearlite;
const linearlite_read_only = process.env.linearlite_read_only;
const nextjs = process.env.nextjs;
const prNumber = context.issue.number;
const commentBody = `## Examples
- linearlite: ${linearlite}
- linearlite-read-only: ${linearlite_read_only}
- nextjs: ${nextjs}
`;
Expand Down
8 changes: 4 additions & 4 deletions examples/linearlite-read-only/.sst/platform/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import "./src/global.d.ts"
import "../types.generated"
import { AppInput, App, Config } from "./src/config"
import * as _neon from "@sst-provider/neon";
import * as _cloudflare from "@pulumi/cloudflare";
import * as _aws from "@pulumi/aws";
import * as _cloudflare from "@pulumi/cloudflare";


declare global {
// @ts-expect-error
export import neon = _neon
// @ts-expect-error
export import cloudflare = _cloudflare
// @ts-expect-error
export import aws = _aws
// @ts-expect-error
export import cloudflare = _cloudflare
interface Providers {
providers?: {
"neon"?: (_neon.ProviderArgs & { version?: string }) | boolean | string;
"cloudflare"?: (_cloudflare.ProviderArgs & { version?: string }) | boolean | string;
"aws"?: (_aws.ProviderArgs & { version?: string }) | boolean | string;
"cloudflare"?: (_cloudflare.ProviderArgs & { version?: string }) | boolean | string;
}
}
export const $config: (
Expand Down
1 change: 1 addition & 0 deletions examples/linearlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"fs-extra": "^10.0.0",
"globals": "^15.13.0",
"postcss": "^8.4.39",
"sst": "3.3.7",
"supabase": "^1.226.3",
"tailwindcss": "^3.4.4",
"tsx": "^4.19.1",
Expand Down
10 changes: 10 additions & 0 deletions examples/linearlite/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
/* deno-fmt-ignore-file */
import "sst"
export {}
declare module "sst" {
export interface Resource {
}
}
98 changes: 98 additions & 0 deletions examples/linearlite/sst.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="./.sst/platform/config.d.ts" />

import { execSync } from 'child_process'

const isProduction = (stage: string) =>
stage.toLocaleLowerCase() === `production`

export default $config({
app(input) {
return {
name: `linearlite`,
removal: isProduction(input?.stage) ? `retain` : `remove`,
home: `aws`,
providers: {
cloudflare: `5.42.0`,
aws: {
version: `6.57.0`,
},
},
}
},
async run() {
try {
const databaseUri = $interpolate`postgresql://postgres:${process.env.LINEARLITE_SUPABASE_PROJECT_PASSWORD}@db.${process.env.LINEARLITE_SUPABASE_PROJECT_ID}.supabase.co:5432/postgres`

databaseUri.apply(applyMigrations)

const electricInfo = databaseUri.apply((uri) =>
addDatabaseToElectric(uri)
)

if (!process.env.ELECTRIC_API) {
throw new Error(`ELECTRIC_API environment variable is required`)
}

const website = new sst.aws.StaticSite('linearlite-website', {
build: {
command: 'npm run build',
output: 'dist',
},
environment: {
VITE_ELECTRIC_URL: process.env.ELECTRIC_API,
VITE_ELECTRIC_TOKEN: electricInfo.token,
VITE_ELECTRIC_DATABASE_ID: electricInfo.id,
},
domain: {
name: `linearlite${isProduction($app.stage) ? `` : `-stage-${$app.stage}`}.examples.electric-sql.com`,
dns: sst.cloudflare.dns(),
},
dev: {
command: 'npm run vite',
},
})

return {
databaseUri,
database_id: electricInfo.id,
electric_token: electricInfo.token,
website: website.url,
}
} catch (e) {
console.error(`Failed to deploy todo app ${$app.stage} stack`, e)
}
},
})

function applyMigrations(uri: string) {
execSync(`pnpm exec pg-migrations apply --directory ./db/migrations`, {
env: {
...process.env,
DATABASE_URL: uri,
},
})
}

async function addDatabaseToElectric(
uri: string
): Promise<{ id: string; token: string }> {
const adminApi = process.env.ELECTRIC_ADMIN_API

const result = await fetch(`${adminApi}/v1/databases`, {
method: `PUT`,
headers: { 'Content-Type': `application/json` },
body: JSON.stringify({
database_url: uri,
region: `us-east-1`,
}),
})

if (!result.ok) {
throw new Error(
`Could not add database to Electric (${result.status}): ${await result.text()}`
)
}

return await result.json()
}
3 changes: 2 additions & 1 deletion examples/linearlite/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
"include": ["src"],
"exclude": ["sst.config.ts"]
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dad6d45

Please sign in to comment.