Skip to content

Commit

Permalink
Add deploy workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
oamaok committed Oct 17, 2021
1 parent a9937fc commit 87516c5
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 254 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Deploy

on:
push:
branches: [main]
pull_request:
branches: []

workflow_dispatch:

jobs:
Deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy
run: |
TEMP=$(mktemp)
echo "${{ secrets.SSH_KEY }}" > $TEMP
ssh -o 'StrictHostKeyChecking no' -i $TEMP ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} -p${{ secrets.SSH_PORT }} 'bash -s' < scripts/deploy.sh
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# [modulate](https://modulate.bitti.io)

Modular synthesis in your browser.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
- Only accesible via hotkey?

# General UI/UX

- Load unsaved patch Y/N at startup to mask the audio init
- Hotkey configuration?
- Global volume control
19 changes: 14 additions & 5 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const esbuild = require('esbuild')
const chokidar = require('chokidar')
const CssModulesPlugin = require('./build/css-modules-plugin')

const isProduction = process.env.NODE_ENV === 'production'

const recursiveCopy = async (srcDir, destDir) => {
const absoluteSrc = path.resolve(__dirname, srcDir)
const absoluteDest = path.resolve(__dirname, destDir)
Expand Down Expand Up @@ -99,12 +101,19 @@ ${worklets.map((worklet) => ` ${worklet}: typeof ${worklet}`).join('\n')}
console.timeEnd('Build worklets')
}

const buildServer = () => {}

;(async () => {
await buildWorklets()
await buildClient()
await buildServer()
try {
await Promise.all([buildWorklets(),
buildClient()])
} catch (err) {
console.error(err)
if (isProduction) {
process.exit(1)
}
}
if(isProduction) {
process.exit(0)
}

chokidar
.watch('./worklets/*', {
Expand Down
8 changes: 1 addition & 7 deletions client/src/generated/worklets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ import Clock from '../../worklets/Clock'
import Gain from '../../worklets/Gain'
import ModulationHelper from '../../worklets/ModulationHelper'
import Sequencer from '../../worklets/Sequencer'
export const workletNames = [
'ADSR',
'Clock',
'Gain',
'ModulationHelper',
'Sequencer',
] as const
export const workletNames = ["ADSR","Clock","Gain","ModulationHelper","Sequencer"] as const
export type Worklets = {
ADSR: typeof ADSR
Clock: typeof Clock
Expand Down
7 changes: 7 additions & 0 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
apps: [{
name: "modulate-server",
script: "yarn",
args: "ts-node ./server/src/index.ts"
}]
}
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"build": "NODE_ENV=production node build.js",
"start:client": "node build.js",
"start:server": "yarn run-migrations && ts-node-dev --respawn --transpile-only ./server/src/index.ts",
"add-migration": "node server/scripts/add-migration",
Expand All @@ -17,25 +18,24 @@
"@types/sqlite3": "^3.1.7",
"@types/uuid": "^8.3.1",
"argon2": "^0.28.2",
"chokidar": "^3.5.2",
"cssnano": "^5.0.8",
"esbuild": "^0.13.2",
"esbuild-css-modules-plugin": "^2.0.9",
"fp-ts": "^2.11.5",
"io-ts": "^2.2.16",
"jsonwebtoken": "^8.5.1",
"kaiku": "^1.19.1",
"postcss": "^8.3.9",
"postcss-modules": "^4.2.2",
"prettier": "^2.4.1",
"sql-template-strings": "^2.2.2",
"sqlite3": "^5.0.2",
"ts-node": "^10.2.1",
"typescript": "^4.4.3",
"uuid": "^8.3.2"
},
"devDependencies": {
"chokidar": "^3.5.2",
"classnames": "^2.3.1",
"fp-ts": "^2.11.4",
"io-ts": "^2.2.16",
"ts-node": "^10.2.1",
"cssnano": "^5.0.8",
"esbuild": "^0.13.2",
"kaiku": "^1.19.1",
"postcss": "^8.3.9",
"postcss-modules": "^4.2.2",
"prettier": "^2.4.1",
"ts-node-dev": "^1.1.8"
}
}
8 changes: 8 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
cd ~/modulate
git fetch origin main
git reset --hard origin/main
yarn
yarn build
yarn run-migrations
pm2 restart ecosystem.config.js
3 changes: 0 additions & 3 deletions server/logger.ts

This file was deleted.

5 changes: 5 additions & 0 deletions server/scripts/run-migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const getMigrationVersion = (filename) => parseInt(filename.split('-')[0])
.filter((filename) => getMigrationVersion(filename) > version)
.sort()

if (applicableMigrations.length === 0) {
console.log('No migrations to run.')
return
}

for (const migration of applicableMigrations) {
console.log('Running migration ', migration)
await db.run('BEGIN')
Expand Down
7 changes: 1 addition & 6 deletions server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as http from 'https'
import * as http from 'http'
import fs from 'fs'
import router, { serverStatic, Response } from './router'
import * as db from './database'
Expand All @@ -12,11 +12,6 @@ const unauthorized = (res: Response) => {
}

const server = http.createServer(
{
key: fs.readFileSync('./cert/key.pem'),
cert: fs.readFileSync('./cert/cert.pem'),
passphrase: 'password',
},
router()
.get('/*', serverStatic('./dist/client/index.html'))
.get('/assets/*', serverStatic('./dist/client/assets'))
Expand Down
3 changes: 3 additions & 0 deletions server/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const info = (message: string) => {
console.log(`[${new Date().toISOString()}] [info] ${message}`)
}
45 changes: 26 additions & 19 deletions server/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { readFile, stat } from 'fs/promises'
import * as http from 'http'
import * as t from 'io-ts'
import * as auth from './authorization'
import * as logger from './logger'
import mimeTypes from './mime-types'
import { User } from '../../common/types'

Expand Down Expand Up @@ -153,19 +154,38 @@ const router = (): RouterChain => {
req: http.IncomingMessage,
res: http.ServerResponse
) => {

const url = new URL(
req.url ?? '',
`http://${req.headers.host ?? 'localhost'}`
)

const segments = url.pathname.split('/')


const response: Response = {
status(code) {
res.statusCode = code
},
json(data: any) {
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify(data))
},
send: res.write.bind(res),
end: () => {
logger.info(`${req.socket.remoteAddress} ${req.method} ${url.pathname} [${res.statusCode}]`)
res.end()
},
header: res.setHeader.bind(res),
}

let matchingRoutes: {
route: Route
parameters: Record<string, string>
wildcardMatch: string
}[] = []


for (let i = 0; i < routes.length; i++) {
const route = routes[i]

Expand Down Expand Up @@ -213,9 +233,9 @@ const router = (): RouterChain => {
}

if (matchingRoutes.length === 0) {
res.statusCode = 404
res.write('404')
res.end()
response.status(404)
response.send('404')
response.end()
return
}

Expand Down Expand Up @@ -243,18 +263,6 @@ const router = (): RouterChain => {
authorization,
}

const response: Response = {
status(code) {
res.statusCode = code
},
json(data: any) {
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify(data))
},
send: res.write.bind(res),
end: res.end.bind(res),
header: res.setHeader.bind(res),
}

switch (route.method) {
case 'GET': {
Expand All @@ -275,10 +283,9 @@ const router = (): RouterChain => {
body = validationResult.right
}
} catch (err) {
res.statusCode = 400
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify({ error: err }))
res.end()
response.status(400)
response.json({ error: err })
response.end()
break
}

Expand Down
Loading

0 comments on commit 87516c5

Please sign in to comment.