forked from ar-io/ar-io-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(middleware): all routes use middleware api and expose capabi…
…lity via ANS-101 impl ar-io#27
- Loading branch information
1 parent
fc7b01a
commit f6466c3
Showing
8 changed files
with
451 additions
and
204 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* 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 swaggerUi from 'swagger-ui-express'; | ||
|
||
import type { ArweaveG8wayMiddleware } from './types.js'; | ||
|
||
export const createGatewayApiDocsMiddleware: (coreDeps: { | ||
openapiDocument: any; | ||
}) => ArweaveG8wayMiddleware = | ||
({ openapiDocument }) => | ||
async ({ addCapability }) => { | ||
await addCapability({ name: 'gateway-api-docs', version: '1.0.0' }); | ||
|
||
return async (app) => { | ||
// OpenAPI | ||
app.get('/openapi.json', (_req, res) => res.json(openapiDocument)); | ||
|
||
// Swagger UI | ||
app.use( | ||
'/api-docs', | ||
swaggerUi.serve, | ||
swaggerUi.setup(openapiDocument, { explorer: true }), | ||
); | ||
|
||
return app; | ||
}; | ||
}; |
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,116 @@ | ||
/** | ||
* 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 express from 'express'; | ||
|
||
import type { StandaloneSqliteDatabase } from '../database/standalone-sqlite.js'; | ||
import type { TransactionFetcher } from '../workers/transaction-fetcher.js'; | ||
import type { ArweaveG8wayMiddleware } from './types.js'; | ||
|
||
export const createArIoCoreMiddleware: (coreDeps: { | ||
AR_IO_WALLET: string; | ||
}) => ArweaveG8wayMiddleware = | ||
({ AR_IO_WALLET }) => | ||
async ({ addCapability }) => { | ||
await addCapability({ name: 'gateway-ar-core', version: '1.0.0' }); | ||
|
||
return async (app) => { | ||
// Healthcheck | ||
app.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 | ||
app.get('/ar-io/info', (_req, res) => { | ||
res.status(200).send({ | ||
wallet: AR_IO_WALLET, | ||
}); | ||
}); | ||
|
||
return app; | ||
}; | ||
}; | ||
|
||
export const createArIoAdminMiddleware: (coreDeps: { | ||
db: StandaloneSqliteDatabase; | ||
prioritizedTxIds: Set<string>; | ||
txFetcher: TransactionFetcher; | ||
ADMIN_API_KEY: string; | ||
}) => ArweaveG8wayMiddleware = | ||
({ db, prioritizedTxIds, txFetcher, ADMIN_API_KEY }) => | ||
async ({ addCapability }) => { | ||
await addCapability({ name: 'ar-io-admin', version: '1.0.0' }); | ||
|
||
return async (app) => { | ||
// Only allow access to admin routes if the bearer token matches the admin api key | ||
app.use('/ar-io/admin', (req, res, next) => { | ||
if (req.headers.authorization === `Bearer ${ADMIN_API_KEY}`) { | ||
next(); | ||
} else { | ||
res.status(401).send('Unauthorized'); | ||
} | ||
}); | ||
|
||
// Debug info (for internal use) | ||
app.get('/ar-io/admin/debug', async (_req, res) => { | ||
res.json({ | ||
db: await db.getDebugInfo(), | ||
}); | ||
}); | ||
|
||
// Block access to contiguous data by ID or hash | ||
app.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; | ||
} | ||
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 | ||
app.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; | ||
} | ||
prioritizedTxIds.add(id); | ||
txFetcher.queueTxId(id); | ||
res.json({ message: 'TX queued' }); | ||
} catch (error: any) { | ||
res.status(500).send(error?.message); | ||
} | ||
}); | ||
|
||
return app; | ||
}; | ||
}; |
Oops, something went wrong.