diff options
| -rw-r--r-- | server/async.ts (renamed from async.ts) | 0 | ||||
| -rw-r--r-- | server/main.ts (renamed from main.ts) | 61 | ||||
| -rw-r--r-- | server/package.json (renamed from package.json) | 0 | ||||
| -rw-r--r-- | server/tsconfig.json (renamed from tsconfig.json) | 0 | ||||
| -rw-r--r-- | server/yarn.lock (renamed from yarn.lock) | 0 |
5 files changed, 54 insertions, 7 deletions
diff --git a/async.ts b/server/async.ts index c88aceb..c88aceb 100644 --- a/async.ts +++ b/server/async.ts @@ -7,13 +7,19 @@ import * as cheerio from "cheerio"; import { wrap } from "./async"; -const PORT = 3000; -const DISCDAG_URL = 'http://solipsys.co.uk/cgi-bin/DiscDAG.py'; -const PUBLIC_URL = 'http://localhost:3000'; +const PORT = process.env.PORT ? +process.env.PORT : 3000; +const DISCDAG_URL = process.env.DISCDAG_URL ?? 'http://solipsys.co.uk/cgi-bin/DiscDAG.py'; +const PUBLIC_URL = process.env.PUBLIC_URL ?? 'http://localhost:3000'; +const DEBUG = process.env.NODE_ENV !== 'production' || PUBLIC_URL.indexOf('localhost') > -1; const app = express(); -app.use(cors({ origin: 'http://localhost:8080', credentials: true })); +if (DEBUG) { + app.use(cors({ + origin: 'http://localhost:8080', + credentials: true, + })); +} app.use(express.json()); app.use(cookieParser()); @@ -89,7 +95,14 @@ const discdag = express() }; }).toArray(); - return $res.json({ discussions }); + return $res.json({ + discussions, + user: { + id: user, + type: 'User', + name, + }, + }); })) .get('/user/:name', wrap(async ($req, $res) => { @@ -148,7 +161,7 @@ const discdag = express() items[id] = { type: 'Note', - published: time.replace(/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)[a-z]+/, '$1-$2-$3T$4:$5:$6Z'), + published: time.replace(/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)[a-z]*/, '$1-$2-$3T$4:$5:$6Z'), attributedTo: `${PUBLIC_URL}/discdag/user/${author}`, inReplyTo: [], replies: [], @@ -184,7 +197,41 @@ const discdag = express() }); })); -app.use('/discdag', discdag) +app.use('/discdag', discdag); + +app.get(/\/remote\/.*/, wrap(async ($req, $res) => { + const { origin } = $req.headers; + const url = $req.url.match('^/remote/(.*)')![1]; + + if (!DEBUG && origin !== PUBLIC_URL) { + return $res.status(403).end(); + } + + const res = await fetch(url, { + headers: { + 'Accept': 'application/ld+json, application/json', + 'X-Forwarded-For': $req.ip, + }, + }); + + $res.status(res.status); + for (const key of [ + 'Content-Type', + 'Content-Size', + 'Location', + 'Age', + 'Cache-Control', + 'Expires', + 'Etag', + ]) { + const val = res.headers.get(key); + if (val) $res.setHeader(key, val); + } + + res.body.pipe($res); + + return $res; +})); try { app.listen(PORT, (): void => { diff --git a/package.json b/server/package.json index b51d743..b51d743 100644 --- a/package.json +++ b/server/package.json diff --git a/tsconfig.json b/server/tsconfig.json index 7b48b7e..7b48b7e 100644 --- a/tsconfig.json +++ b/server/tsconfig.json diff --git a/yarn.lock b/server/yarn.lock index 63ac408..63ac408 100644 --- a/yarn.lock +++ b/server/yarn.lock |
