Skip to content

Commit

Permalink
Release/1.1 (#39)
Browse files Browse the repository at this point in the history
* test

* Wrap location hook

* Use wrapped hook

* Tidy up forgot password process

* Return 200 when user does not exist as well

* Update build action

* Add curl to docker file

* Update build action

* Fix seed migration and add database persistance

* Remove database reset

* Ensure multiple accounts don't get provisioned

* Remove database reset

* Remove push from PR action

* Add useForm hook

* Move accounts forms to useForm hook

* Add useForm to admin forms

* Add useForm hook to command forms

* Add useForm hook to user forms

* Remove html qualifier

* Remove debug if statement short circuit

* Fix build errors

* Add develop branch workflow

* Fix token expiry calculation

* Enhance command schema (#34)

* Enable strict no console eslint rule

* Add textarea to command edit form

* Add button replacement for interpolation

* Tidy up to fix compile errors

* Fix missed token calculation

* Get working interpolation replacement

* Add data back to internaldata store

* Implement change handler properly and remove old code

* Add functioning change handler for response element

* Add button margin and initialise buttons on first load

* Remove comments

* Add semi working selector

* Add resize property to textarea

* Add popup for interpolation input

* Make popup mobile friendly

* Ensure button converts correctly when inserted

* Fix compile warnings

* Add modal component

* Implement form to edit complex interpolation

* Move change handler out to hook

* Helps if you save everything before committing

* Fix linting errors

* Add more commands

* Move styles out

* Move modal styles out

* Add multiple action capability (#36)

* Add new migration and change logic around inserting global admins

* Fix migration and add order column to actions

* Gracefully handle specifically connection refused errors on webhook

* Get pages compiling again

* Fix delete route and start working on create route

* Implement add new action button on frontend

* Add saving new or existing command with action support

* Fix up errors around saving

* Ensure actions save and load by order property

* Ensure getCommands includes actions according to argument again

* Get reordering working

* Add delete action logic

* Fix build errors and tidy up

* Update key logic

* Remove unused variables

* Add dynamic provisioning (#37)

* Use id as action key when possible

* Break out webhook logic to generic hook and add edit account webhook post

* Update vulnerable libraries and add build test for yarn audit (#38)

* Update vulnerable libraries and add build test for yarn audit

* Update build test

* .

* .

* .

* .

* .

* .

* .

* Update qwik

* Update yarn

* Update typescript and vite

* Update eslint

* Update dockerfile and build dependencies

* Update yarn audit test

* Update lock file

* They done changed the cli name on me

* Update readme

* Update version number and pipeline
  • Loading branch information
LachlanCourt authored Apr 21, 2024
1 parent 2b6bdb5 commit 6b2dea9
Show file tree
Hide file tree
Showing 60 changed files with 9,217 additions and 4,320 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
!tsconfig.json
!vite.config.ts
!yarn.lock
!.yarnrc.yml
!.yarn
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
"@typescript-eslint/ban-ts-comment": "off",
"prefer-spread": "off",
"no-case-declarations": "off",
"no-console": "off",
"no-console": ["error"],
"@typescript-eslint/no-unused-vars": ["error"],
},
};
1 change: 1 addition & 0 deletions .github/workflows/build-and-push-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- release/1.1

jobs:
build-and-push:
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ jobs:
uses: actions/checkout@v3
- name: Build Docker Image
run: make build
yarn-audit:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Run yarn audit
run: yarn npm audit --severity high
893 changes: 893 additions & 0 deletions .yarn/releases/yarn-4.1.1.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.1.1.cjs
14 changes: 9 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM node:18-alpine3.17 as base
ARG NODE_IMAGE_NAME=20-alpine3.18

FROM node:${NODE_IMAGE_NAME} as base

COPY . ./code
WORKDIR /code
Expand All @@ -10,23 +12,25 @@ RUN yarn build
# Set up database
RUN yarn prisma generate

FROM node:18-alpine3.17 as module-installation
FROM node:${NODE_IMAGE_NAME} as module-installation

COPY package.json ./code/package.json
COPY yarn.lock ./code/yarn.lock
COPY .yarnrc.yml ./code/.yarnrc.yml
COPY .yarn ./code/.yarn
WORKDIR /code

# Install production dependencies
RUN apk add --no-cache jq
RUN apk add --no-cache jq
RUN jq 'del(.devDependencies)' package.json > package.json.temp && mv package.json.temp package.json
RUN yarn install --production --no-optional
RUN yarn install

# Copy database client data
COPY --from=base ./code/node_modules/@prisma ./node_modules/@prisma
COPY --from=base ./code/node_modules/.prisma ./node_modules/.prisma
COPY --from=base ./code/node_modules/prisma ./node_modules/prisma

FROM node:18-alpine3.17 as final
FROM node:${NODE_IMAGE_NAME} as final

RUN apk add curl

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fetch-dev-db:
docker pull postgres && docker tag postgres dev-db:latest
docker pull postgres:15 && docker tag postgres:15 dev-db:latest
start-dev-db:
docker run --name dev-db -e POSTGRES_PASSWORD=password --hostname docker --network qwik-network -d -v $(shell pwd)/database:/var/lib/postgresql/data dev-db
remove-dev-db:
Expand Down
80 changes: 20 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,35 @@
# Qwik City App ⚡️
# Qwik Webapp

- [Qwik Docs](https://qwik.builder.io/)
- [Discord](https://qwik.builder.io/chat)
- [Qwik GitHub](https://github.com/BuilderIO/qwik)
- [@QwikDev](https://twitter.com/QwikDev)
- [Vite](https://vitejs.dev/)
This webapp is designed as a web interface for a separate bot that connects to the twitch API as a chatbot.

---
## Installation

## Project Structure
1. Ensure postgres, node 20+, and yarn are installed
2. Create a `.env` file with the following data

This project is using Qwik with [QwikCity](https://qwik.builder.io/qwikcity/overview/). QwikCity is just a extra set of tools on top of Qwik to make it easier to build a full site, including directory-based routing, layouts, and more.
```env
JWT_SECRET= <Random values for jwt encryption>
Inside your project, you'll see the following directory structure:
SENDGRID_API_KEY= <Sendgrid API Key (Only needed in production)>
```
├── public/
│ └── ...
└── src/
├── components/
│ └── ...
└── routes/
└── ...
```

- `src/routes`: Provides the directory based routing, which can include a hierarchy of `layout.tsx` layout files, and an `index.tsx` file as the page. Additionally, `index.ts` files are endpoints. Please see the [routing docs](https://qwik.builder.io/qwikcity/routing/overview/) for more info.

- `src/components`: Recommended directory for components.

- `public`: Any static assets, like images, can be placed in the public directory. Please see the [Vite public directory](https://vitejs.dev/guide/assets.html#the-public-directory) for more info.
SENDGRID_FROM_ADDRESS= <From email address>
## Add Integrations and deployment
DATABASE_URL= postgresql://<databasename>:[email protected]:PORT
Use the `yarn qwik add` command to add additional integrations. Some examples of integrations include: Cloudflare, Netlify or Express server, and the [Static Site Generator (SSG)](https://qwik.builder.io/qwikcity/static-site-generation/static-site-config/).
ADMIN_EMAIL= <your-email>
```shell
yarn qwik add # or `yarn qwik add`
```

## Development

Development mode uses [Vite's development server](https://vitejs.dev/). During development, the `dev` command will server-side render (SSR) the output.
ADMIN_PASSWORD= <your-password>
```shell
npm start # or `yarn start`
ADMIN_NAME= <your-name>
```

> Note: during dev mode, Vite may request a significant number of `.js` files. This does not represent a Qwik production build.
3. Run `yarn install`
4. Run `yarn prisma migrate dev` to populate your database with empty tables from the migration
5. Run `node add-global-admin.js` to seed the data provided as admin env vars as a global admin in the db
6. Run `yarn dev`

## Preview
## Helpful commands

The preview command will create a production build of the client modules, a production build of `src/entry.preview.tsx`, and run a local server. The preview server is only for convenience to locally preview a production build, and it should not be used as a production server.

```shell
yarn preview # or `yarn preview`
```

## Production

The production build will generate client and server modules by running both client and server build commands. Additionally, the build command will use Typescript to run a type check on the source code.

```shell
yarn build # or `yarn build`
```

## Express Server

This app has a minimal [Express server](https://expressjs.com/) implementation. After running a full build, you can preview the build using the command:

```
yarn serve
```
The makefile contains a number of helpful commands for running in docker. Builds to this repo build the docker image and run an audit on npm package security vulnerabilities before being able to merge. To test this will pass locally you can run `npm run build` to build the app baremetal, or `make build` to build the docker image locally. Once built locally you can run either `yarn serve` to run baremetal, or `make run` to run in docker.

Then visit [http://localhost:8080/](http://localhost:8080/)
To inspect the database locally you can run `yarn prisma studio` to open an in browser edit tool
53 changes: 53 additions & 0 deletions deployment-artifacts/add-global-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const PrismaClient = require("@prisma/client").PrismaClient;
const fs = require("fs");
const crypto = require("crypto");

const insertGlobalAdmin = async () => {
// Parse env variables
fs.readFileSync(".env", "utf8")
.split("\n")
.forEach((envVar) => {
const [key, value] = envVar.split("=");
process.env[key] = value;
});

const email = process.env.ADMIN_EMAIL;
const password = process.env.ADMIN_PASSWORD;
const name = process.env.ADMIN_NAME;

const salt = crypto.randomUUID().replace(/-/g, "");

const hashedPassword = Buffer.from(
crypto.createHash("sha256").update(`${salt}${password}`).digest("hex")
).toString();

const savePassword = `${salt}$${hashedPassword}`;

const client = new PrismaClient();

console.log(email);

await client.$executeRaw`
INSERT INTO "User" (
"email",
"name",
"password",
"isGlobalAdmin"
)
SELECT ${email}, ${name}, ${savePassword}, 'true'
WHERE NOT EXISTS (
SELECT 1
FROM "User"
WHERE "email" = ${email}
);`;

await client.$disconnect();
};

insertGlobalAdmin()
.then(() => {
console.log("Global admin inserted successfully");
})
.catch((error) => {
console.error("Error inserting global admin user:", error);
});
59 changes: 0 additions & 59 deletions deployment-artifacts/add-seed-migration.js

This file was deleted.

3 changes: 1 addition & 2 deletions deployment-artifacts/start.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#! /bin/sh

# Run pending migrations
node add-seed-migration.js
node node_modules/prisma/build migrate deploy
rm -rf $(find . -type d -name "*seed_user*")
node add-global-admin.js

# Start application
node server/entry.express
Expand Down
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "webapp",
"description": "Web interface for twitch chatbot",
"version": "1.1.0",
"engines": {
"node": ">=18.0.0"
"node": ">=20.0.0"
},
"private": true,
"scripts": {
Expand All @@ -23,34 +24,35 @@
"qwik": "qwik"
},
"devDependencies": {
"@builder.io/qwik": "^1.1.4",
"@builder.io/qwik-city": "^1.1.4",
"@builder.io/qwik": "^1.5.2",
"@builder.io/qwik-city": "^1.5.2",
"@types/compression": "^1.7.2",
"@types/eslint": "8.4.10",
"@types/express": "4.17.17",
"@types/node": "^18.11.16",
"@types/node-fetch": "latest",
"@typescript-eslint/eslint-plugin": "5.46.1",
"@typescript-eslint/parser": "5.46.1",
"@typescript-eslint/eslint-plugin": "6.21.0",
"@typescript-eslint/parser": "6.21.0",
"compression": "^1.7.4",
"eslint": "8.30.0",
"eslint-plugin-qwik": "0.16.1",
"prettier": "2.8.1",
"preview-email": "^3.0.7",
"prisma": "^4.15.0",
"typescript": "4.9.4",
"typescript": "5.4.5",
"util": "^0.12.5",
"vite": "4.0.1",
"vite-tsconfig-paths": "3.5.0"
"vite": "5.2.10",
"vite-tsconfig-paths": "4.3.2"
},
"dependencies": {
"@prisma/client": "^4.15.0",
"@sendgrid/mail": "^7.7.0",
"@vanilla-extract/css": "^1.9.2",
"@vanilla-extract/vite-plugin": "^3.7.0",
"crypto-js": "^4.1.1",
"crypto-js": "^4.2.0",
"express": "4.18.2",
"jose": "^4.11.1",
"undici": "5.14.0",
"express": "4.18.2"
}
"undici": "5.19.1"
},
"packageManager": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE "Command" ADD COLUMN "response" TEXT NULL;

ALTER TABLE "Action" DROP CONSTRAINT "Action_commandId_fkey";

DROP TABLE "Action"
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- CreateTable
CREATE TABLE "Action" (
"id" SERIAL NOT NULL,
"commandId" INTEGER NOT NULL,
"order" INTEGER NULL,
"type" TEXT NOT NULL,
"content" TEXT NOT NULL,

CONSTRAINT "Action_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "Action" ADD CONSTRAINT "Action_commandId_fkey" FOREIGN KEY ("commandId") REFERENCES "Command"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- Copy data from command into action
INSERT INTO "Action" ("commandId", "type", "content")
SELECT id, 'RESPONSE', response FROM "Command";

-- AlterTable
ALTER TABLE "Command" DROP COLUMN "response";
Loading

0 comments on commit 6b2dea9

Please sign in to comment.