Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project fissure #438

Draft
wants to merge 80 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
0fee36b
monorepo apps
josefaidt Feb 6, 2023
3b151b6
single tsconfig at root
josefaidt Feb 6, 2023
c767a7d
upgraded sveltekit config
josefaidt Feb 6, 2023
076e663
add turbo
josefaidt Feb 6, 2023
0bd124b
mv to apps/discord-bot
josefaidt Feb 6, 2023
ba4d19a
mv discord lib to dedicated package
josefaidt Feb 7, 2023
492e776
improved bot build and dev
josefaidt Feb 7, 2023
087efd8
use prisma client, constants from package
josefaidt Feb 7, 2023
efe2204
rm discord alias
josefaidt Feb 7, 2023
52b11ff
add constants package
josefaidt Feb 7, 2023
60208d6
use constants, prisma from packages
josefaidt Feb 7, 2023
fa763ff
add prisma package
josefaidt Feb 7, 2023
ea50407
rm shared tsconfig dep
josefaidt Feb 7, 2023
65c3d6f
rm shared tsconfig dep
josefaidt Feb 7, 2023
73d7d83
turbocharge scripts
josefaidt Feb 7, 2023
b977507
update gitignore with comments, turbo
josefaidt Feb 7, 2023
57d22bb
remove $discord alias
josefaidt Feb 8, 2023
ed0efe9
split out github files
josefaidt Feb 10, 2023
8bdf7bd
improve prisma build
josefaidt Feb 10, 2023
cf0b174
split out features
josefaidt Feb 10, 2023
c03a818
fix imports from aliases, import from prisma package
josefaidt Feb 10, 2023
787914f
add tsconfig
josefaidt Feb 10, 2023
e421806
fix imports from old aliases
josefaidt Feb 10, 2023
d2a165e
rename to prisma-client
josefaidt Feb 11, 2023
f9999ee
extract applyRoles, fix github build
josefaidt Feb 11, 2023
263ae35
rm unecessary external value
josefaidt Feb 11, 2023
9692173
mv applyRoles to app code
josefaidt Feb 11, 2023
31fffac
fix tsup, add vitest, tsconfig
josefaidt Feb 11, 2023
a73113f
improve bot build
josefaidt Feb 11, 2023
0f3b746
fix discord build
josefaidt Feb 11, 2023
9e6f448
improve builds, exports
josefaidt Feb 11, 2023
07a6ae0
update lockfile
josefaidt Feb 11, 2023
c52174a
improve discord-bot build
josefaidt Feb 11, 2023
e746ce6
fix missing discord.js types, add devDep
josefaidt Feb 11, 2023
876afbe
update lockfile
josefaidt Feb 11, 2023
304d44c
fix frontend imports, types
josefaidt Feb 11, 2023
32bb41e
bump deps, commonjs pkg
josefaidt Feb 11, 2023
a4ae9b5
update lockfile
josefaidt Feb 11, 2023
6e3b9c1
migrate to moduleResolution nodenext
josefaidt Feb 13, 2023
ab82bd2
fix dynamic require issue where pkgs are importing cjs
josefaidt Feb 13, 2023
eea37a5
begin typing required process.env vars
josefaidt Feb 13, 2023
89f9ad2
working discord-bot build
josefaidt Feb 13, 2023
e03a739
migrate to nodenext module resolution for esm output
josefaidt Feb 13, 2023
62a8319
bump deps
josefaidt Feb 13, 2023
e07e92d
add a readme
josefaidt Feb 13, 2023
05b105d
migrate improper import of `constants` package to lib/constants
josefaidt Feb 14, 2023
22b4dcb
fix frontend build's prisma issue
josefaidt Feb 14, 2023
82560f7
update lockfile
josefaidt Feb 14, 2023
b668062
migrate (mostly) from next-auth to auth.js
josefaidt Feb 15, 2023
5adff27
fix envDir to load dotenv from project root
josefaidt Feb 15, 2023
7431164
add mariadb
josefaidt Feb 15, 2023
06251c9
working frontend build
josefaidt Feb 16, 2023
80540ce
rename cdk directories
josefaidt Feb 20, 2023
117b25c
update docker builds
josefaidt Feb 20, 2023
2ec0211
shuffle cdk directories, imports
josefaidt Feb 20, 2023
4ac8460
bump frontend deps, fix auth
josefaidt Feb 21, 2023
89b9c67
add db-stop to db command, dropping data after exit
josefaidt Feb 21, 2023
9878dbc
fix frontend RBAC
josefaidt Feb 21, 2023
07c2bd6
fix issue with ts resolving $env types from .sveltekit/ambient
josefaidt Feb 21, 2023
20188f1
add npm scripts for docker build
josefaidt Feb 22, 2023
7488242
parameterize node, pnpm, alpine versions
josefaidt Mar 16, 2023
a3c282f
add package engines
josefaidt Mar 16, 2023
d09b01c
Merge remote-tracking branch 'upstream/main' into upgrade-sveltekit
josefaidt Mar 17, 2023
075a7cb
update node, pnpm, add docker comments
josefaidt Mar 17, 2023
8c54b24
use sqlite, git mv migration
josefaidt Mar 17, 2023
5b65e4e
migrate hooks to lib/server, fix api auth
josefaidt Mar 21, 2023
7af8c16
migrate from locals.session.guild to locals.guild
josefaidt Mar 21, 2023
b838b98
appease typescript gods
josefaidt Mar 21, 2023
39103b9
deprecate old session handler
josefaidt Apr 4, 2023
f9cabc0
fix ssm types
josefaidt Apr 4, 2023
d478882
improve docker build
josefaidt Apr 4, 2023
6305e9c
add start command
josefaidt Apr 4, 2023
593a79b
fix docker build
josefaidt Apr 6, 2023
1111104
fix docker build
josefaidt Apr 7, 2023
ffbe88a
remove unused vite packages, upgrade dependencies
josefaidt Apr 7, 2023
e8108ed
upgrade tooling, update lockfile
josefaidt Apr 7, 2023
ab2d188
remove unused apps from docker-compose
josefaidt Apr 7, 2023
70eaa97
add comment for working with prisma and files
josefaidt Apr 7, 2023
f0620c4
fix prisma init/seeding
josefaidt Apr 7, 2023
5980870
as const
josefaidt Apr 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ node_modules
/cdk
/docs
/tests
/scripts
.vscode

# ignore git
.git*
.github
.git-hooks

# no dotfiles in general
.*

# no builds
**/build

Expand All @@ -20,4 +29,8 @@ node_modules
**/*.db

# ignore env files
.env*
.env*

# take .turbo and .svelte-kit for build cache
!.turbo
!.svelte-kit
4 changes: 2 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ VITE_DISCORD_GUILD_ID=$DISCORD_GUILD_ID
DISCORD_AUTH_CLIENT_ID=$DISCORD_APP_ID
DISCORD_AUTH_CLIENT_SECRET=oauthclientsecret

# Database URL for prisma (relative to prisma directory)
DATABASE_URL="file:data/local.db"
# Database URL for prisma (relative to prisma directory if using a file)
DATABASE_URL="mysql://root:super-top-secret@localhost:3306/hey-amplify"

# GitHub->Discord release webhook
DISCORD_WEBHOOK_URL_RELEASES=discordwebhookurl
Expand Down
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# general ignores
.DS_Store
node_modules
*.log

# ignore all env files except the sample
.env
.env.*
!.env.sample
cypress.env.json

# notes
notes.md

/prisma/data
/.svelte-kit
/build
# ignore all build directories
build

packages/**/build
# ignore all turbo dot-directories
.turbo
4 changes: 4 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node-version=18.14.1
engine-strict=true

init-license=Apache-2.0
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.10.0
v18.15.0
73 changes: 34 additions & 39 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
#syntax=docker/dockerfile:1.4
FROM --platform=linux/amd64 alpine as builder
# Download the static build of Litestream directly into the path & make it executable.
# This is done in the builder and copied as the chmod doubles the size.
ADD https://github.com/benbjohnson/litestream/releases/download/v0.3.8/litestream-v0.3.8-linux-amd64-static.tar.gz /tmp/litestream.tar.gz
RUN tar -C /usr/local/bin -xzf /tmp/litestream.tar.gz

FROM --platform=linux/amd64 node:18-alpine
WORKDIR /usr/src

# @todo - remove this after upgrading prisma to ~4.8.0
# https://github.com/prisma/prisma/issues/16553#issuecomment-1353302617
RUN apk add --update --no-cache openssl1.1-compat

# Copy executable & Litestream from builder.
COPY --from=builder /usr/local/bin/litestream /usr/local/bin/litestream
# Copy litestream config
COPY scripts/litestream.yml /etc/litestream.yml

# add bash
RUN apk add bash

# Create data directory (although this will likely be mounted too)
RUN mkdir -p /data

# ARG PLATFORM="linux/amd64"
ARG NODE_VERSION="18.14.2"
ARG ALPINE_VERSION="3.17"
FROM node:${NODE_VERSION}-alpine${ALPINE_VERSION} as pnpm-builder
ARG PNPM_VERSION="7.30.0"
# for turbo - https://turbo.build/repo/docs/handbook/deploying-with-docker#example
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /workspace
# Install pnpm
ARG PNPM_VERSION=7.9.5
RUN npm install --global pnpm@${PNPM_VERSION}
RUN corepack enable && \
corepack prepare pnpm@${PNPM_VERSION} --activate
# pnpm fetch only requires lockfile, but we'll need to build workspaces
COPY pnpm*.yaml ./
# RUN pnpm fetch
COPY patches ./patches
# mount pnpm store as cache & fetch dependencies
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm-store \
pnpm fetch

# build workspace
FROM pnpm-builder AS workspace
WORKDIR /workspace
ENV CI=true
# add project source to build
ADD . .
# install dependencies
RUN pnpm install --frozen-lockfile
COPY . ./
# mount .env file
RUN --mount=type=secret,id=env,required=true,target=/workspace/.env \
pnpm install --frozen-lockfile --offline --silent

# @todo remove in favor of mounting .env?
# expose arguments for VITE environment variables
ARG VITE_HOST=http://localhost:3000
ARG VITE_NEXTAUTH_URL=http://localhost:3000
ARG VITE_DISCORD_GUILD_ID=976838371383083068
ARG DATABASE_URL="file:/data/sqlite.db"
# expose necessary env vars
ENV PORT=3000
# run build
RUN pnpm run build:lib && pnpm run build
# install production dependencies
RUN pnpm install --frozen-lockfile --prod --ignore-scripts

EXPOSE 3000
CMD ["./scripts/start.sh"]
# run build
RUN --mount=type=secret,id=env,required=true,target=/workspace/.env \
pnpm run build
# RUN pnpm run build
# deploy app
RUN pnpm --filter ./apps/bot.amplify.aws deploy ./build/bot.amplify.aws
# deploy bot
RUN pnpm --filter ./apps/discord-bot deploy ./build/discord-bot
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ This repository contains the source code for the [AWS Amplify Discord Server](ht
4. Run the application with `pnpm dev`
5. As the server owner navigate to `http://localhost:3000/`, log in, and visit `/admin` to configure the instance

Once you are finished working with the Discord Bot you will want to stop the database container with `pnpm stop-db`

### Setting up a Discord Bot

<!-- TODO: screenshots -->
Expand Down
1 change: 1 addition & 0 deletions apps/bot.amplify.aws/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.svelte-kit
9 changes: 9 additions & 0 deletions apps/bot.amplify.aws/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# "workspace" is built from the project root, see "pnpm docker-build-workspace" in root package.json
FROM workspace
WORKDIR /app
# copy built files
COPY --from=workspace /workspace/build/bot.amplify.aws ./build
# expose port
EXPOSE 3000
# start the app
CMD ["pnpm", "run", "start"]
60 changes: 60 additions & 0 deletions apps/bot.amplify.aws/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "@aws-amplify/discord-bot-frontend",
"type": "module",
"version": "2.0.0-next.0",
"license": "Apache-2.0",
"private": true,
"scripts": {
"start": "node ./build/index.js",
"dev": "vite",
"build": "vite build",
"test": "vitest",
"coverage": "vitest run --coverage",
"prepare": "svelte-kit sync"
},
"dependencies": {
"@auth/core": "^0.5.1",
"@auth/sveltekit": "^0.3.0",
"@aws-crypto/sha256-js": "^4.0.0",
"@carbon/styles": "^1.26.0",
"@discordjs/rest": "^1.7.0",
"@hey-amplify/discord": "workspace:*",
"@hey-amplify/prisma-client": "workspace:*",
"@next-auth/prisma-adapter": "^1.0.5",
"@octokit/auth-app": "^4.0.9",
"@octokit/graphql": "^5.0.5",
"@octokit/rest": "^19.0.7",
"cookie": "^0.5.0",
"discord-api-types": "^0.37.37",
"discord.js": "^14.9.0",
"fast-glob": "^3.2.12",
"fuzzy": "^0.1.3",
"svelte": "3.58.0",
"uuid": "^9.0.0",
"zod": "^3.21.4"
},
"devDependencies": {
"@carbon/charts": "^1.6.14",
"@carbon/charts-svelte": "^1.6.14",
"@hey-amplify/constants": "workspace:*",
"@hey-amplify/features": "workspace:*",
"@hey-amplify/github": "workspace:*",
"@hey-amplify/support": "workspace:*",
"@sveltejs/adapter-node": "1.2.3",
"@sveltejs/kit": "1.15.2",
"@types/cookie": "^0.5.1",
"@vitest/ui": "^0.29.8",
"carbon-components-svelte": "^0.73.5",
"carbon-icons-svelte": "^11.4.0",
"carbon-preprocess-svelte": "^0.9.1",
"d3": "^7.8.4",
"jsdom": "^21.1.1",
"svelte-check": "^3.2.0",
"svelte-preprocess": "^5.0.3",
"tslib": "^2.5.0",
"typescript": "^5.0.4",
"vite": "^4.2.1",
"vite-node": "^0.29.8",
"vitest": "^0.29.8"
}
}
File renamed without changes
File renamed without changes
File renamed without changes.
14 changes: 8 additions & 6 deletions src/app.d.ts → apps/bot.amplify.aws/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable @typescript-eslint/no-empty-interface */
/// <reference types="@sveltejs/kit" />
import type * as NextAuth from 'next-auth'
import * as Auth from '@auth/core/types'

interface User extends NextAuth.User {
interface User extends Auth.User {
id: string
isAdmin: boolean
isStaff: boolean
isGuildOwner: boolean
}

interface AppSession extends NextAuth.Session {
user: User
// Guild ID of the guild the user is currently viewing
guild: string
interface AppSession extends Auth.Session {
user?: User
isGithubLinked?: boolean
}

// See https://kit.svelte.dev/docs/typescript
Expand All @@ -20,6 +20,8 @@ declare global {
namespace App {
interface Locals {
session: AppSession
// Guild ID of the guild the user is currently viewing
guild: string
}

interface Platform {}
Expand Down
File renamed without changes.
File renamed without changes.
91 changes: 91 additions & 0 deletions apps/bot.amplify.aws/src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { GUILD_COOKIE } from '$lib/constants'
import { getUserAccess } from '@hey-amplify/discord'
import { sequence } from '@sveltejs/kit/hooks'
import cookie from 'cookie'
import { prisma, init } from '$lib/db'
import type { Handle } from '@sveltejs/kit'
import { handleAuth } from '$lib/server/hooks/handle-auth'
import { handleApiAuth } from '$lib/server/hooks/handle-api-auth'
import { handleSavedGuild } from '$lib/server/hooks/handle-saved-guild'
import { handleSetSessionLocals } from '$lib/server/hooks/handle-set-session-locals'

/**
* Add additional user data to the session
* @deprecated
*/
const handleSessionUser: Handle = async ({ event, resolve }) => {
const { session } = event.locals

let savedGuild = null
const cookies = event.request.headers.get('cookie')
if (cookies) {
const parsed = cookie.parse(cookies)
savedGuild = parsed['hey-amplify.guild']
}

let activeGuild = import.meta.env.VITE_DISCORD_GUILD_ID
if (savedGuild) activeGuild = savedGuild

if (session?.user) {
if (!guild) guild = activeGuild
event.locals.session = session
const user = await prisma.user.findUnique({
where: {
id: session.user.id,
},
select: {
accounts: {
select: {
provider: true,
providerAccountId: true,
},
},
},
})

const storedUserGitHub = user!.accounts.some(
(account) => account.provider === 'github'
)
if (storedUserGitHub) session.user.github = true

const discordUserId = user!.accounts.filter(
(account) => account.provider === 'discord'
)[0].providerAccountId
let access
try {
access = await getUserAccess(discordUserId, guild)
} catch (error) {
console.error('Error getting access', error)
}
session.user = {
...session.user,
...(access || {}),
id: discordUserId,
}
}

const response = await resolve(event)

// set initial guild cookie
if (!savedGuild) {
response.headers.append(
'Set-Cookie',
cookie.serialize(GUILD_COOKIE, activeGuild, {
path: '/',
maxAge: 60 * 60 * 24 * 7, // 1 week
httpOnly: true,
})
)
}

return response
}

export const handle = sequence(
handleSavedGuild,
handleAuth,
handleSetSessionLocals,
// handleSession, // get session from NextAuth.js
// handleSessionUser, // add user details to session locals
handleApiAuth // protect API routes
)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
const redirect = import.meta.env.VITE_HOST
</script>

<form action="{`/api/auth/signin/${provider}`}" method="POST">
<form action="{`/auth/signin/${provider}`}" method="POST">
{#await getCsrfToken() then csrfToken}
<input type="hidden" name="csrfToken" value="{csrfToken}" />
{/await}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import { getCsrfToken } from './auth'
</script>

<form
action="{`${import.meta.env.VITE_NEXTAUTH_URL}/api/auth/signout`}"
method="POST"
>
<form action="{`/auth/signout`}" method="POST">
{#await getCsrfToken() then csrfToken}
<input type="hidden" name="csrfToken" value="{csrfToken}" />
{/await}
Expand Down
Loading