diff --git a/site/migrations/000-create-users.js b/site/migrations/000-create-users.js index 3a05352c7b..76dde745a2 100644 --- a/site/migrations/000-create-users.js +++ b/site/migrations/000-create-users.js @@ -6,7 +6,7 @@ exports.up = DB => { name character varying(255), username character varying(255) not null, avatar text, - github_token character varying(255) not null, + github_token character varying(255), created_at timestamp with time zone NOT NULL DEFAULT now(), updated_at timestamp with time zone ); diff --git a/site/package-lock.json b/site/package-lock.json index 3e5f82ed43..4e17717eeb 100644 --- a/site/package-lock.json +++ b/site/package-lock.json @@ -1360,6 +1360,11 @@ } } }, + "@polka/redirect": { + "version": "1.0.0-next.0", + "resolved": "https://registry.npmjs.org/@polka/redirect/-/redirect-1.0.0-next.0.tgz", + "integrity": "sha512-ym6ooqMr09+cV+y52p5kszJ0jYcX+nJfm8POrQb7QYowvpPPuneZ71EclHrQSB7a50lcytgR/xtL6AUFdvyEkg==" + }, "@polka/send": { "version": "1.0.0-next.2", "resolved": "https://registry.npmjs.org/@polka/send/-/send-1.0.0-next.2.tgz", diff --git a/site/package.json b/site/package.json index 030f28ad42..e7a1b5385e 100644 --- a/site/package.json +++ b/site/package.json @@ -15,6 +15,7 @@ "testsrc": "mocha -r esm test/**" }, "dependencies": { + "@polka/redirect": "^1.0.0-next.0", "@polka/send": "^1.0.0-next.2", "devalue": "^1.1.0", "do-not-zip": "^1.0.0", diff --git a/site/src/components/Repl/ReplWidget.svelte b/site/src/components/Repl/ReplWidget.svelte index e785c697f8..fbdd3622a7 100644 --- a/site/src/components/Repl/ReplWidget.svelte +++ b/site/src/components/Repl/ReplWidget.svelte @@ -28,7 +28,7 @@ } if (gist) { - fetch(`gist/${gist}`).then(r => r.json()).then(data => { + fetch(`repl/${gist}.json`).then(r => r.json()).then(data => { const { id, description, files } = data; name = description; diff --git a/site/src/routes/examples/[slug].json.js b/site/src/routes/examples/[slug].json.js index 2cb0958982..705a4e7e89 100644 --- a/site/src/routes/examples/[slug].json.js +++ b/site/src/routes/examples/[slug].json.js @@ -6,16 +6,16 @@ const cache = new Map(); export function get(req, res) { const { slug } = req.params; - try { - let example = cache.get(slug); + let example = cache.get(slug); - if (!example || process.env.NODE_ENV !== 'production') { - example = get_example(slug); - cache.set(slug, example); - } + if (!example || process.env.NODE_ENV !== 'production') { + example = get_example(slug); + if (example) cache.set(slug, example); + } + if (example) { send(res, 200, example); - } catch (err) { + } else { send(res, 404, { error: 'not found' }); diff --git a/site/src/routes/examples/_examples.js b/site/src/routes/examples/_examples.js index bf23769500..e4580d7fda 100644 --- a/site/src/routes/examples/_examples.js +++ b/site/src/routes/examples/_examples.js @@ -35,7 +35,7 @@ export function get_example(slug) { const dir = lookup.get(slug); const title = titles.get(slug); - if (!dir || !title) throw { status: 404, message: 'not found' }; + if (!dir || !title) return null; const files = fs.readdirSync(`content/examples/${dir}`) .filter(name => name[0] !== '.' && name !== 'meta.json') diff --git a/site/src/routes/gist/[id].js b/site/src/routes/gist/[id].js deleted file mode 100644 index 6f3eb52c3b..0000000000 --- a/site/src/routes/gist/[id].js +++ /dev/null @@ -1,65 +0,0 @@ -import send from '@polka/send'; -import { body } from './_utils.js'; -import { query } from '../../utils/db'; -import { isUser } from '../../backend/auth'; - -export async function get(req, res) { - const [row] = await query(` - select g.*, u.uid as owner from gists g - left join users u on g.user_id = u.id - where g.uid = $1 limit 1 - `, [req.params.id]); // via filename pattern - - if (!row) { - return send(res, 404, { error: 'Gist not found' }); - } - - send(res, 200, { - uid: row.uid, - name: row.name, - files: row.files, - owner: row.owner - }); -} - -export async function patch(req, res) { - const user = await isUser(req, res); - if (!user) return; // response already sent - - let id, uid=req.params.id; - - try { - const [row] = await query(`select * from gists where uid = $1 limit 1`, [uid]); - if (!row) return send(res, 404, { error: 'Gist not found' }); - if (row.user_id !== user.id) return send(res, 403, { error: 'Item does not belong to you' }); - id = row.id; - } catch (err) { - console.error('PATCH /gists @ select', err); - return send(res, 500); - } - - try { - const obj = await body(req); - obj.updated_at = 'now()'; - let k, cols=[], vals=[]; - for (k in obj) { - cols.push(k); - vals.push(obj[k]); - } - - const tmp = vals.map((x, i) => `$${i + 1}`).join(','); - const set = `set (${cols.join(',')}) = (${tmp})`; - - const [row] = await query(`update gists ${set} where id = ${id} returning *`, vals); - - send(res, 200, { - uid: row.uid, - name: row.name, - files: row.files, - owner: user.uid, - }); - } catch (err) { - console.error('PATCH /gists @ update', err); - send(res, 500, { error: err.message }); - } -} diff --git a/site/src/routes/repl/_components/AppControls/UserMenu.svelte b/site/src/routes/repl/[id]/_components/AppControls/UserMenu.svelte similarity index 96% rename from site/src/routes/repl/_components/AppControls/UserMenu.svelte rename to site/src/routes/repl/[id]/_components/AppControls/UserMenu.svelte index faa5e35ab8..1404c1d5fc 100644 --- a/site/src/routes/repl/_components/AppControls/UserMenu.svelte +++ b/site/src/routes/repl/[id]/_components/AppControls/UserMenu.svelte @@ -1,5 +1,5 @@ + + + + + + + {name} • REPL • Svelte + + + + + + + + +
+ + + {#if process.browser} +
+ +
+ + {#if mobile} + + {/if} + {/if} +
diff --git a/site/src/routes/gist/_utils.js b/site/src/routes/repl/_utils/body.js similarity index 87% rename from site/src/routes/gist/_utils.js rename to site/src/routes/repl/_utils/body.js index 8417031001..950208ab56 100644 --- a/site/src/routes/gist/_utils.js +++ b/site/src/routes/repl/_utils/body.js @@ -1,4 +1,4 @@ -export function body(req) { +export default function body(req) { return new Promise((fulfil, reject) => { let str = ''; diff --git a/site/src/routes/gist/create.js b/site/src/routes/repl/create.json.js similarity index 95% rename from site/src/routes/gist/create.js rename to site/src/routes/repl/create.json.js index 82fe5a74cb..d0abaf2db3 100644 --- a/site/src/routes/gist/create.js +++ b/site/src/routes/repl/create.json.js @@ -1,5 +1,5 @@ import send from '@polka/send'; -import { body } from './_utils.js'; +import body from './_utils/body.js'; import { query } from '../../utils/db'; import { isUser } from '../../backend/auth'; diff --git a/site/src/routes/repl/index.svelte b/site/src/routes/repl/index.svelte index d3b1e6bc01..0c7acfc9dc 100644 --- a/site/src/routes/repl/index.svelte +++ b/site/src/routes/repl/index.svelte @@ -1,238 +1,16 @@ - - - - - - - {name} • REPL • Svelte - - - - - - - - -
- - - {#if process.browser} -
- -
- - {#if mobile} - - {/if} - {/if} -
+ \ No newline at end of file