-
Notifications
You must be signed in to change notification settings - Fork 0
Latest changes from bottle server #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,6 +103,8 @@ dist | |
| # TernJS port file | ||
| .tern-port | ||
|
|
||
| # VSCode settings | ||
| .vscode | ||
|
|
||
| /data/* | ||
| !/data/.gitkeep | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ import { valid } from './util'; | |
|
|
||
| const app = express(); | ||
|
|
||
| const PORT = 8000; | ||
|
|
||
| const ROOT = 'js.bottle.remotehack.space'; | ||
|
|
||
| const SUBREG = /^(?<subdomain>\w[\w-]*)\.js\.bottle\.remotehack\.space$/; | ||
|
|
@@ -19,67 +21,62 @@ app.all('/', urlencodedParser, async (req, res, next) => { | |
|
|
||
| const {host} = req.headers; | ||
|
|
||
| if(host === ROOT) { | ||
| // | ||
| if(req.method === 'GET') { | ||
| res.sendFile(join(__dirname, '/../www/landing.html')) | ||
| return; | ||
| } | ||
|
|
||
| if(req.method === 'POST') { | ||
|
|
||
| const {subdomain} = req.body | ||
| if (host !== ROOT) { | ||
| next(); | ||
| return; | ||
| } | ||
|
|
||
| if(valid(subdomain)) { | ||
| if(await store.exists({subdomain})) { | ||
| if(req.method === 'GET') { | ||
| res.sendFile(join(__dirname, '/../www/landing.html')) | ||
| return; | ||
| } | ||
|
|
||
| res.redirect(subdomain + ROOT) | ||
|
|
||
| return; | ||
| } else { | ||
| if (req.method === 'POST') { | ||
|
|
||
| await store.createStore({subdomain}) | ||
| const {subdomain} = req.body | ||
|
|
||
| res.send(`CREATED SUBDOMAIN: ${req.body.subdomain}`) | ||
| return; | ||
| } | ||
| if (valid(subdomain)) { | ||
| if (!await store.exists({subdomain})) { | ||
| await store.createStore({subdomain}) | ||
| } | ||
|
|
||
|
|
||
| return; | ||
| res.redirect(`http://${subdomain}.${ROOT}`) | ||
| } else { | ||
| res.status(404).send(`${subdomain} is not a valid subdomain name!`); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| next(); | ||
|
|
||
| }) | ||
|
|
||
| app.all('*', async (req, res) => { | ||
| const {host} = req.headers; | ||
|
|
||
| const match =host?.match(SUBREG); | ||
| if(match) { | ||
|
|
||
| const {subdomain} = (match.groups as any) | ||
| const match = host?.match(SUBREG); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I always knew seeing my first optional chaining in the wild would be special...but this is BEYOND!!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...I also obviously was not paying attention during the initial coding session on Saturday |
||
| const {subdomain} = match?.groups || {}; | ||
|
|
||
| if (!match || !subdomain) { | ||
| const msg = `Failed to match for subdomain on host ${host}`; | ||
| console.error(msg); | ||
| res.status(404).send(msg); | ||
| return; | ||
| } | ||
|
|
||
| const timestamp = Date.now().toString(); | ||
| const content = JSON.stringify([req.headers, req.url]) | ||
|
|
||
| const timestamp = Date.now().toString(); | ||
| const content = JSON.stringify([req.headers, req.url]) | ||
| try { | ||
| await store.write({subdomain, timestamp, content}) | ||
|
|
||
| const result = await store.read({subdomain}); | ||
|
|
||
| res.contentType('text/plain'); | ||
| res.send(result); | ||
|
|
||
| return; | ||
| } catch (e) { | ||
| console.error(e); | ||
| res.status(404).send(`Nobody has created this domain yet.`); | ||
| } | ||
|
|
||
| res.send(`(NODE) Cool `) | ||
| }) | ||
|
|
||
| app.listen(8000, () => { | ||
| console.log("started") | ||
| app.listen(PORT, () => { | ||
| console.log(`started on port ${PORT}`); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏