Skip to content

Commit

Permalink
add redirect to the right domain (#109)
Browse files Browse the repository at this point in the history
* add redirect to the right domain

* fix close new server

* fix dev

* fix tests staying open
  • Loading branch information
juliangruber authored Oct 19, 2023
1 parent 3f24a40 commit 261b28e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ COPY --from=builder /app /app
WORKDIR /app
ENV NODE_ENV production
ENV PATH /root/.volta/bin:$PATH
ENV DOMAIN api.filspark.com

CMD [ "npm", "run", "start" ]
8 changes: 7 additions & 1 deletion bin/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import assert from 'node:assert'
const {
PORT = 8080,
HOST = '127.0.0.1',
DOMAIN = 'localhost',
DATABASE_URL,
SENTRY_ENVIRONMENT = 'development'
} = process.env
Expand Down Expand Up @@ -58,7 +59,12 @@ const round = await getCurrentRound()
assert(!!round, 'cannot obtain the current Spark round number')
console.log('SPARK round number at service startup:', round)

const handler = await createHandler({ client, logger: console, getCurrentRound })
const handler = await createHandler({
client,
logger: console,
getCurrentRound,
domain: DOMAIN
})
const server = http.createServer(handler)
console.log('Starting the http server on host %j port %s', HOST, PORT)
server.listen(PORT, HOST)
Expand Down
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import getRawBody from 'raw-body'
import assert from 'http-assert'
import { validate } from './lib/validate.js'

const handler = async (req, res, client, getCurrentRound) => {
const handler = async (req, res, client, getCurrentRound, domain) => {
if (req.headers.host.split(':')[0] !== domain) {
return redirect(res, `https://${domain}${req.url}`)
}
const segs = req.url.split('/').filter(Boolean)
if (segs[0] === 'retrievals' && req.method === 'POST') {
await createRetrieval(req, res, client, getCurrentRound)
Expand Down Expand Up @@ -334,12 +337,23 @@ const notFound = (res) => {
res.end('Not Found')
}

export const createHandler = async ({ client, logger, getCurrentRound }) => {
const redirect = (res, location) => {
res.statusCode = 301
res.setHeader('location', location)
res.end()
}

export const createHandler = async ({
client,
logger,
getCurrentRound,
domain
}) => {
await migrate(client)
return (req, res) => {
const start = new Date()
logger.info(`${req.method} ${req.url} ...`)
handler(req, res, client, getCurrentRound)
handler(req, res, client, getCurrentRound, domain)
.catch(err => errorHandler(res, err, logger))
.then(() => {
logger.info(`${req.method} ${req.url} ${res.statusCode} (${new Date() - start}ms)`)
Expand Down
35 changes: 34 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ describe('Routes', () => {
},
async getCurrentRound () {
return currentSparkRoundNumber
}
},
domain: '127.0.0.1'
})
server = http.createServer(handler)
server.listen()
Expand Down Expand Up @@ -669,6 +670,38 @@ describe('Routes', () => {
})
})

describe('Redirect', () => {
it('redirects to the right domain', async () => {
let server
try {
const handler = await createHandler({
client,
logger: {
info () {},
error (...args) { console.error(...args) }
},
async getCurrentRound () {
return currentSparkRoundNumber
},
domain: 'foobar'
})
server = http.createServer(handler)
server.listen()
await once(server, 'listening')
const spark = `http://127.0.0.1:${server.address().port}`
const res = await fetch(
`${spark}/rounds/${currentSparkRoundNumber}`,
{ redirect: 'manual' }
)
await assertResponseStatus(res, 301)
assert.strictEqual(res.headers.get('location'), `https://foobar/rounds/${currentSparkRoundNumber}`)
} finally {
server.closeAllConnections()
server.close()
}
})
})

async function givenRetrieval (props = {}) {
const createRequest = await fetch(
`${spark}/retrievals`,
Expand Down

0 comments on commit 261b28e

Please sign in to comment.