-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(routes): Move routes into a separate file
As routes have five clear categories, splitting them into separate files to group them by category makes locating them in the file system easier. Related to issue #31
- Loading branch information
Showing
7 changed files
with
257 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* AR.IO Gateway | ||
* Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { Router, default as express } from 'express'; | ||
import createPrometheusMiddleware from 'express-prometheus-middleware'; | ||
|
||
import * as config from '../config.js'; | ||
import * as system from '../system.js'; | ||
|
||
export const arIoRouter = Router(); | ||
|
||
arIoRouter.use( | ||
createPrometheusMiddleware({ | ||
metricsPath: '/ar-io/__gateway_metrics', | ||
extraMasks: [ | ||
// Mask all paths except for the ones below | ||
/^(?!api-docs)(?!ar-io)(?!graphql)(?!openapi\.json)(?!raw).+$/, | ||
// Mask Arweave TX IDs | ||
/[a-zA-Z0-9_-]{43}/, | ||
], | ||
}), | ||
); | ||
|
||
// Healthcheck | ||
arIoRouter.get('/ar-io/healthcheck', (_req, res) => { | ||
const data = { | ||
uptime: process.uptime(), | ||
message: 'Welcome to the Permaweb.', | ||
date: new Date(), | ||
}; | ||
|
||
res.status(200).send(data); | ||
}); | ||
|
||
// ar.io network info | ||
arIoRouter.get('/ar-io/info', (_req, res) => { | ||
res.status(200).send({ | ||
wallet: config.AR_IO_WALLET, | ||
}); | ||
}); | ||
|
||
// Only allow access to admin routes if the bearer token matches the admin api key | ||
arIoRouter.use('/ar-io/admin', (req, res, next) => { | ||
if (req.headers.authorization === `Bearer ${config.ADMIN_API_KEY}`) { | ||
next(); | ||
} else { | ||
res.status(401).send('Unauthorized'); | ||
} | ||
}); | ||
|
||
// Debug info (for internal use) | ||
arIoRouter.get('/ar-io/admin/debug', async (_req, res) => { | ||
res.json({ | ||
db: await system.db.getDebugInfo(), | ||
}); | ||
}); | ||
|
||
// Block access to contiguous data by ID or hash | ||
arIoRouter.put('/ar-io/admin/block-data', express.json(), async (req, res) => { | ||
// TODO improve validation | ||
try { | ||
const { id, hash, source, notes } = req.body; | ||
if (id === undefined && hash === undefined) { | ||
res.status(400).send("Must provide 'id' or 'hash'"); | ||
return; | ||
} | ||
system.db.blockData({ id, hash, source, notes }); | ||
// TODO check return value | ||
res.json({ message: 'Content blocked' }); | ||
} catch (error: any) { | ||
res.status(500).send(error?.message); | ||
} | ||
}); | ||
|
||
// Queue a TX ID for processing | ||
arIoRouter.post('/ar-io/admin/queue-tx', express.json(), async (req, res) => { | ||
try { | ||
const { id } = req.body; | ||
if (id === undefined) { | ||
res.status(400).send("Must provide 'id'"); | ||
return; | ||
} | ||
system.prioritizedTxIds.add(id); | ||
system.txFetcher.queueTxId(id); | ||
res.json({ message: 'TX queued' }); | ||
} catch (error: any) { | ||
res.status(500).send(error?.message); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* AR.IO Gateway | ||
* Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { Router } from 'express'; | ||
|
||
import * as config from '../config.js'; | ||
import { createArnsMiddleware } from '../middleware/arns.js'; | ||
import { createSandboxMiddleware } from '../middleware/sandbox.js'; | ||
import * as system from '../system.js'; | ||
import { dataHandler } from './data/index.js'; | ||
|
||
export const arnsRouter = Router(); | ||
|
||
if (config.ARNS_ROOT_HOST !== undefined) { | ||
arnsRouter.use( | ||
createArnsMiddleware({ | ||
dataHandler, | ||
nameResolver: system.nameResolver, | ||
}), | ||
); | ||
|
||
arnsRouter.use( | ||
createSandboxMiddleware({ | ||
sandboxProtocol: config.SANDBOX_PROTOCOL, | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.