-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,685 additions
and
17 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,64 @@ | ||
import express from 'express'; | ||
import { createServer } from 'http'; | ||
import {port} from "./config.js"; | ||
|
||
class EndpointRegistry { | ||
constructor() { | ||
this.app = express(); | ||
this.server = createServer(this.app); | ||
this.registeredEndpoints = new Map(); | ||
|
||
this.app.use(express.json()); | ||
|
||
this.app.use((err, req, res, next) => { | ||
console.error(err.stack); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
}); | ||
} | ||
|
||
registerEndpoint(namespace, routes, middleware = []) { | ||
if (this.registeredEndpoints.has(namespace)) { | ||
throw new Error(`Endpoint namespace '${namespace}' is already registered`); | ||
} | ||
|
||
const router = express.Router(); | ||
|
||
middleware.forEach(mw => router.use(mw)); | ||
|
||
Object.entries(routes).forEach(([path, handlers]) => { | ||
Object.entries(handlers).forEach(([method, handler]) => { | ||
router[method.toLowerCase()](path, async (req, res, next) => { | ||
try { | ||
await handler(req, res, next); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
this.app.use(`/api/${namespace}`, router); | ||
this.registeredEndpoints.set(namespace, { routes, middleware }); | ||
} | ||
|
||
start() { | ||
return new Promise((resolve) => { | ||
this.server.listen(port, () => { | ||
console.log(`listening on port ${port}`); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
stop() { | ||
return new Promise((resolve) => { | ||
this.server.close(resolve); | ||
}); | ||
} | ||
|
||
getRegisteredEndpoints() { | ||
return Array.from(this.registeredEndpoints.keys()); | ||
} | ||
} | ||
|
||
export const endpoints = new EndpointRegistry(); |
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,142 @@ | ||
export default [ | ||
{ | ||
"inputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "constructor" | ||
}, | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": false, | ||
"internalType": "string", | ||
"name": "key", | ||
"type": "string" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "uint128", | ||
"name": "value", | ||
"type": "uint128" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "uint128", | ||
"name": "timestamp", | ||
"type": "uint128" | ||
} | ||
], | ||
"name": "OracleUpdate", | ||
"type": "event" | ||
}, | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": false, | ||
"internalType": "address", | ||
"name": "newUpdater", | ||
"type": "address" | ||
} | ||
], | ||
"name": "UpdaterAddressChange", | ||
"type": "event" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "string", | ||
"name": "key", | ||
"type": "string" | ||
} | ||
], | ||
"name": "getValue", | ||
"outputs": [ | ||
{ | ||
"internalType": "uint128", | ||
"name": "", | ||
"type": "uint128" | ||
}, | ||
{ | ||
"internalType": "uint128", | ||
"name": "", | ||
"type": "uint128" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "string[]", | ||
"name": "keys", | ||
"type": "string[]" | ||
}, | ||
{ | ||
"internalType": "uint256[]", | ||
"name": "compressedValues", | ||
"type": "uint256[]" | ||
} | ||
], | ||
"name": "setMultipleValues", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "string", | ||
"name": "key", | ||
"type": "string" | ||
}, | ||
{ | ||
"internalType": "uint128", | ||
"name": "value", | ||
"type": "uint128" | ||
}, | ||
{ | ||
"internalType": "uint128", | ||
"name": "timestamp", | ||
"type": "uint128" | ||
} | ||
], | ||
"name": "setValue", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "address", | ||
"name": "newOracleUpdaterAddress", | ||
"type": "address" | ||
} | ||
], | ||
"name": "updateOracleUpdaterAddress", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "string", | ||
"name": "", | ||
"type": "string" | ||
} | ||
], | ||
"name": "values", | ||
"outputs": [ | ||
{ | ||
"internalType": "uint256", | ||
"name": "", | ||
"type": "uint256" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
} | ||
]; |
Oops, something went wrong.