From 9378b5687e3ce5ef365c80eae0ad6ccec223261b Mon Sep 17 00:00:00 2001 From: zloystorm <72349983+zloystorm@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:41:05 +0300 Subject: [PATCH 01/21] Create package.json asd --- package.json | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000000..c9c2afc7be --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "json-server-demo", + "version": "1.0.0", + "main": "server.js", + "scripts": { + "start": "json-server --watch db.json --port 3000" + }, + "dependencies": { + "json-server": "^0.16.3" + } +} From 0f9106397db3dc724c339b9a7591196a3163079f Mon Sep 17 00:00:00 2001 From: zloystorm <72349983+zloystorm@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:41:47 +0300 Subject: [PATCH 02/21] Create server.js --- server.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 server.js diff --git a/server.js b/server.js new file mode 100644 index 0000000000..4af91c8a83 --- /dev/null +++ b/server.js @@ -0,0 +1,10 @@ +const jsonServer = require('json-server'); +const server = jsonServer.create(); +const router = jsonServer.router('db.json'); +const middlewares = jsonServer.defaults(); + +server.use(middlewares); +server.use(router); +server.listen(3000, () => { + console.log('JSON Server is running'); +}); From be0205545e13e1eb835b2cfdb0ed83715b444b28 Mon Sep 17 00:00:00 2001 From: zloystorm <72349983+zloystorm@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:53:43 +0300 Subject: [PATCH 03/21] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c9c2afc7be..b3aff701d7 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "main": "server.js", "scripts": { - "start": "json-server --watch db.json --port 3000" + "start": "node server.js" }, "dependencies": { "json-server": "^0.16.3" From 79a0be9c07e3798f71aab6bc2773ccb959472842 Mon Sep 17 00:00:00 2001 From: zloystorm <72349983+zloystorm@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:53:55 +0300 Subject: [PATCH 04/21] Update server.js --- server.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 4af91c8a83..9c45894a12 100644 --- a/server.js +++ b/server.js @@ -2,9 +2,10 @@ const jsonServer = require('json-server'); const server = jsonServer.create(); const router = jsonServer.router('db.json'); const middlewares = jsonServer.defaults(); +const port = process.env.PORT || 3000; server.use(middlewares); server.use(router); -server.listen(3000, () => { - console.log('JSON Server is running'); +server.listen(port, () => { + console.log(`JSON Server is running on port ${port}`); }); From d0aa2ff2a7d59c1b9f0363a324dd4658ddff8e6c Mon Sep 17 00:00:00 2001 From: zloystorm <72349983+zloystorm@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:54:35 +0300 Subject: [PATCH 05/21] Create vercel.json --- vercel.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 vercel.json diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000000..1def834a00 --- /dev/null +++ b/vercel.json @@ -0,0 +1,15 @@ +{ + "version": 2, + "builds": [ + { + "src": "server.js", + "use": "@vercel/node" + } + ], + "routes": [ + { + "src": "/(.*)", + "dest": "/server.js" + } + ] +} From 31ee121546268775a8c661d533dbd41854de8546 Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 16:10:29 +0300 Subject: [PATCH 06/21] Setup JSON --- server.js => api/server.js | 1 + 1 file changed, 1 insertion(+) rename server.js => api/server.js (91%) diff --git a/server.js b/api/server.js similarity index 91% rename from server.js rename to api/server.js index 9c45894a12..921d3d2a29 100644 --- a/server.js +++ b/api/server.js @@ -2,6 +2,7 @@ const jsonServer = require('json-server'); const server = jsonServer.create(); const router = jsonServer.router('db.json'); const middlewares = jsonServer.defaults(); +const path = require('path'); const port = process.env.PORT || 3000; server.use(middlewares); From 43b04ecab2f488b16d66d704e16a02f22d3e85f0 Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 16:13:10 +0300 Subject: [PATCH 07/21] Setup JSON2 --- api/server.js | 12 ------------ package.json | 1 + server.js | 14 ++++++++++++++ 3 files changed, 15 insertions(+), 12 deletions(-) delete mode 100644 api/server.js create mode 100644 server.js diff --git a/api/server.js b/api/server.js deleted file mode 100644 index 921d3d2a29..0000000000 --- a/api/server.js +++ /dev/null @@ -1,12 +0,0 @@ -const jsonServer = require('json-server'); -const server = jsonServer.create(); -const router = jsonServer.router('db.json'); -const middlewares = jsonServer.defaults(); -const path = require('path'); -const port = process.env.PORT || 3000; - -server.use(middlewares); -server.use(router); -server.listen(port, () => { - console.log(`JSON Server is running on port ${port}`); -}); diff --git a/package.json b/package.json index b3aff701d7..69781416d9 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "start": "node server.js" }, "dependencies": { + "express": "^4.17.1", "json-server": "^0.16.3" } } diff --git a/server.js b/server.js new file mode 100644 index 0000000000..d4e112c9c7 --- /dev/null +++ b/server.js @@ -0,0 +1,14 @@ +const express = require('express'); +const jsonServer = require('json-server'); +const path = require('path'); + +const app = express(); +const router = jsonServer.router(path.join(__dirname, 'db.json')); +const middlewares = jsonServer.defaults(); +const port = process.env.PORT || 3000; + +app.use(middlewares); +app.use(router); +app.listen(port, () => { + console.log(`Server is running on port ${port}`); +}); From 60fbe526f17066986b5346b30a0e4eb7d6c2b5ec Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 16:31:00 +0300 Subject: [PATCH 08/21] Setup JSON --- db.json | 2 +- server.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/db.json b/db.json index 7e0d3c0a92..fe38520b44 100644 --- a/db.json +++ b/db.json @@ -9,6 +9,6 @@ { "id": 2, "body": "some comment", "postId": 1 } ], "profile": { - "name": "typicode" + "name": "test1" } } diff --git a/server.js b/server.js index d4e112c9c7..742c09a3cc 100644 --- a/server.js +++ b/server.js @@ -10,5 +10,5 @@ const port = process.env.PORT || 3000; app.use(middlewares); app.use(router); app.listen(port, () => { - console.log(`Server is running on port ${port}`); + console.log(`Server running port ${port}`); }); From d8c6a7fe173fca1cb7005c4ad1f90f05aafe2c5c Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 16:38:15 +0300 Subject: [PATCH 09/21] ended JSON --- db.json | 69 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/db.json b/db.json index fe38520b44..6f4a559cce 100644 --- a/db.json +++ b/db.json @@ -1,14 +1,59 @@ { - "posts": [ - { "id": 1, "title": "Post 1" }, - { "id": 2, "title": "Post 2" }, - { "id": 3, "title": "Post 3" } - ], - "comments": [ - { "id": 1, "body": "some comment", "postId": 1 }, - { "id": 2, "body": "some comment", "postId": 1 } - ], - "profile": { - "name": "test1" - } + "chains": [ + { "id": 1, "name": "Ethereum" }, + { "id": 2, "name": "Binance Smart Chain" }, + { "id": 3, "name": "Polygon" }, + { "id": 4, "name": "Avalanche" }, + { "id": 5, "name": "Fantom" } + ], + "shortNames": [ + { "id": 1, "shortName": "ETH", "chainId": 1 }, + { "id": 2, "shortName": "BSC", "chainId": 2 }, + { "id": 3, "shortName": "MATIC", "chainId": 3 }, + { "id": 4, "shortName": "AVAX", "chainId": 4 }, + { "id": 5, "shortName": "FTM", "chainId": 5 } + ], + "chainIds": [ + { "id": 1, "chainId": 1, "chainIdValue": 1 }, + { "id": 2, "chainId": 2, "chainIdValue": 56 }, + { "id": 3, "chainId": 3, "chainIdValue": 137 }, + { "id": 4, "chainId": 4, "chainIdValue": 43114 }, + { "id": 5, "chainId": 5, "chainIdValue": 250 } + ], + "networks": [ + { "id": 1, "network": "mainnet", "chainId": 1 }, + { "id": 2, "network": "mainnet", "chainId": 2 }, + { "id": 3, "network": "mainnet", "chainId": 3 }, + { "id": 4, "network": "mainnet", "chainId": 4 }, + { "id": 5, "network": "mainnet", "chainId": 5 } + ], + "nativeCurrencies": [ + { "id": 1, "name": "Ether", "symbol": "ETH", "decimals": 18, "chainId": 1 }, + { "id": 2, "name": "Binance Coin", "symbol": "BNB", "decimals": 18, "chainId": 2 }, + { "id": 3, "name": "Matic", "symbol": "MATIC", "decimals": 18, "chainId": 3 }, + { "id": 4, "name": "Avalanche", "symbol": "AVAX", "decimals": 18, "chainId": 4 }, + { "id": 5, "name": "Fantom", "symbol": "FTM", "decimals": 18, "chainId": 5 } + ], + "rpcUrls": [ + { "id": 1, "url": "https://mainnet.infura.io/v3/YOUR-PROJECT-ID", "chainId": 1 }, + { "id": 2, "url": "https://api.mycryptoapi.com/eth", "chainId": 1 }, + { "id": 3, "url": "https://cloudflare-eth.com", "chainId": 1 }, + { "id": 4, "url": "https://bsc-dataseed.binance.org/", "chainId": 2 }, + { "id": 5, "url": "https://bsc-dataseed1.defibit.io/", "chainId": 2 }, + { "id": 6, "url": "https://bsc-dataseed1.ninicoin.io/", "chainId": 2 }, + { "id": 7, "url": "https://rpc-mainnet.maticvigil.com/", "chainId": 3 }, + { "id": 8, "url": "https://rpc-mainnet.matic.network", "chainId": 3 }, + { "id": 9, "url": "https://rpc-mainnet.matic.quiknode.pro", "chainId": 3 }, + { "id": 10, "url": "https://api.avax.network/ext/bc/C/rpc", "chainId": 4 }, + { "id": 11, "url": "https://api.avax-test.network/ext/bc/C/rpc", "chainId": 4 }, + { "id": 12, "url": "https://rpcapi.fantom.network", "chainId": 5 }, + { "id": 13, "url": "https://rpc.fantom.network", "chainId": 5 } + ], + "blockExplorerUrls": [ + { "id": 1, "url": "https://etherscan.io", "chainId": 1 }, + { "id": 2, "url": "https://bscscan.com", "chainId": 2 }, + { "id": 3, "url": "https://polygonscan.com", "chainId": 3 }, + { "id": 4, "url": "https://cchain.explorer.avax.network", "chainId": 4 }, + { "id": 5, "url": "https://ftmscan.com", "chainId": 5 } + ] } From 29fa6b81ac6946879f9c42f9f3a04e2a6ae34f6a Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 16:44:05 +0300 Subject: [PATCH 10/21] setup server.js --- server.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/server.js b/server.js index 742c09a3cc..9eeafd5799 100644 --- a/server.js +++ b/server.js @@ -1,14 +1,36 @@ -const express = require('express'); const jsonServer = require('json-server'); +const low = require('lowdb'); +const FileSync = require('lowdb/adapters/FileSync'); +const Memory = require('lowdb/adapters/Memory'); +const fs = require('fs'); const path = require('path'); -const app = express(); -const router = jsonServer.router(path.join(__dirname, 'db.json')); +const app = jsonServer.create(); +const router = jsonServer.router('db.json'); const middlewares = jsonServer.defaults(); -const port = process.env.PORT || 3000; + +const dbFilePath = path.join(__dirname, 'db.json'); +const adapter = process.env.NODE_ENV === 'production' ? new Memory() : new FileSync(dbFilePath); +const db = low(adapter); + +// Скопируем данные из db.json в память при запуске сервера +if (process.env.NODE_ENV === 'production') { + const dbData = JSON.parse(fs.readFileSync(dbFilePath, 'utf-8')); + db.defaults(dbData).write(); +} app.use(middlewares); +app.use(jsonServer.bodyParser); + +// Используем базу данных из памяти +app.use((req, res, next) => { + req.app.db = db; + next(); +}); + app.use(router); + +const port = process.env.PORT || 3000; app.listen(port, () => { - console.log(`Server running port ${port}`); + console.log(`JSON Server is running on port ${port}`); }); From 40c71f973c03ec2dbaaa52d108e58ed247d8992f Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 16:48:38 +0300 Subject: [PATCH 11/21] setup server.js --- server.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/server.js b/server.js index 9eeafd5799..52635c19e2 100644 --- a/server.js +++ b/server.js @@ -6,23 +6,21 @@ const fs = require('fs'); const path = require('path'); const app = jsonServer.create(); -const router = jsonServer.router('db.json'); -const middlewares = jsonServer.defaults(); - const dbFilePath = path.join(__dirname, 'db.json'); const adapter = process.env.NODE_ENV === 'production' ? new Memory() : new FileSync(dbFilePath); const db = low(adapter); -// Скопируем данные из db.json в память при запуске сервера if (process.env.NODE_ENV === 'production') { const dbData = JSON.parse(fs.readFileSync(dbFilePath, 'utf-8')); db.defaults(dbData).write(); } +const middlewares = jsonServer.defaults(); +const router = jsonServer.router(db); // Используем базу данных из lowdb напрямую + app.use(middlewares); app.use(jsonServer.bodyParser); -// Используем базу данных из памяти app.use((req, res, next) => { req.app.db = db; next(); From ed98f612f4c4366ad5751a22648de12d79754655 Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 17:06:19 +0300 Subject: [PATCH 12/21] setup server.js --- server.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 52635c19e2..133a0f85f4 100644 --- a/server.js +++ b/server.js @@ -16,11 +16,23 @@ if (process.env.NODE_ENV === 'production') { } const middlewares = jsonServer.defaults(); -const router = jsonServer.router(db); // Используем базу данных из lowdb напрямую +const router = jsonServer.router(db); app.use(middlewares); app.use(jsonServer.bodyParser); +app.use((req, res, next) => { + if (req.method !== 'GET') { + const apiKey = req.headers['x-api-key']; + const expectedApiKey = '12345678910'; + + if (!apiKey || apiKey !== expectedApiKey) { + return res.status(403).json({ error: 'Forbidden' }); + } + } + next(); +}); + app.use((req, res, next) => { req.app.db = db; next(); @@ -30,5 +42,5 @@ app.use(router); const port = process.env.PORT || 3000; app.listen(port, () => { - console.log(`JSON Server is running on port ${port}`); + console.log(`Server running port ${port}`); }); From fe0566feec39577c50285099813e43f9c0f16f77 Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 17:29:33 +0300 Subject: [PATCH 13/21] setup server.js --- server.js | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index 133a0f85f4..b581fcd7c2 100644 --- a/server.js +++ b/server.js @@ -16,15 +16,14 @@ if (process.env.NODE_ENV === 'production') { } const middlewares = jsonServer.defaults(); -const router = jsonServer.router(db); - app.use(middlewares); app.use(jsonServer.bodyParser); +// Middleware для проверки ключа в заголовках app.use((req, res, next) => { if (req.method !== 'GET') { const apiKey = req.headers['x-api-key']; - const expectedApiKey = '12345678910'; + const expectedApiKey = 'your-secret-api-key'; // Замените на ваш реальный ключ if (!apiKey || apiKey !== expectedApiKey) { return res.status(403).json({ error: 'Forbidden' }); @@ -33,14 +32,43 @@ app.use((req, res, next) => { next(); }); +// Custom route to get chain info with all parameters +app.get('/chains/:id/full', (req, res) => { + const chainId = parseInt(req.params.id, 10); + const chain = db.get('chains').find({ id: chainId }).value(); + if (!chain) { + return res.status(404).json({ error: 'Chain not found' }); + } + + const shortName = db.get('shortNames').find({ chainId: chainId }).value(); + const chainIdValue = db.get('chainIds').find({ chainId: chainId }).value(); + const network = db.get('networks').find({ chainId: chainId }).value(); + const nativeCurrency = db.get('nativeCurrencies').find({ chainId: chainId }).value(); + const rpcUrls = db.get('rpcUrls').filter({ chainId: chainId }).map('url').value(); + const blockExplorerUrls = db.get('blockExplorerUrls').filter({ chainId: chainId }).map('url').value(); + + const fullChainInfo = { + ...chain, + shortName: shortName ? shortName.shortName : null, + chainIdValue: chainIdValue ? chainIdValue.chainIdValue : null, + network: network ? network.network : null, + nativeCurrency: nativeCurrency ? nativeCurrency : null, + rpcUrls: rpcUrls, + blockExplorerUrls: blockExplorerUrls + }; + + res.json(fullChainInfo); +}); + app.use((req, res, next) => { req.app.db = db; next(); }); +const router = jsonServer.router(db); // Используем базу данных из lowdb напрямую app.use(router); const port = process.env.PORT || 3000; app.listen(port, () => { - console.log(`Server running port ${port}`); + console.log(`JSON Server is running on port ${port}`); }); From e25a55a464791c7a55895df9ba770a37186a18c8 Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 18:18:16 +0300 Subject: [PATCH 14/21] setup server.js --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index b581fcd7c2..73f0c8bcc3 100644 --- a/server.js +++ b/server.js @@ -23,7 +23,7 @@ app.use(jsonServer.bodyParser); app.use((req, res, next) => { if (req.method !== 'GET') { const apiKey = req.headers['x-api-key']; - const expectedApiKey = 'your-secret-api-key'; // Замените на ваш реальный ключ + const expectedApiKey = '12345678910'; // Замените на ваш реальный ключ if (!apiKey || apiKey !== expectedApiKey) { return res.status(403).json({ error: 'Forbidden' }); From bc931594a5350eec34e6879a774fab252ff3093f Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 18:25:29 +0300 Subject: [PATCH 15/21] setup server.js --- server.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 73f0c8bcc3..1afbd6cc25 100644 --- a/server.js +++ b/server.js @@ -19,11 +19,10 @@ const middlewares = jsonServer.defaults(); app.use(middlewares); app.use(jsonServer.bodyParser); -// Middleware для проверки ключа в заголовках app.use((req, res, next) => { if (req.method !== 'GET') { const apiKey = req.headers['x-api-key']; - const expectedApiKey = '12345678910'; // Замените на ваш реальный ключ + const expectedApiKey = '12345678910'; if (!apiKey || apiKey !== expectedApiKey) { return res.status(403).json({ error: 'Forbidden' }); @@ -32,7 +31,31 @@ app.use((req, res, next) => { next(); }); -// Custom route to get chain info with all parameters +app.post('/chains', (req, res) => { + const newChain = req.body; + const chainId = newChain.id; + + db.get('chains').push(newChain).write(); + db.get('shortNames').push({ id: chainId, shortName: newChain.shortName, chainId: chainId }).write(); + db.get('chainIds').push({ id: chainId, chainId: chainId, chainIdValue: newChain.chainIdValue }).write(); + db.get('networks').push({ id: chainId, network: newChain.network, chainId: chainId }).write(); + db.get('nativeCurrencies').push({ + id: chainId, + name: newChain.nativeCurrency.name, + symbol: newChain.nativeCurrency.symbol, + decimals: newChain.nativeCurrency.decimals, + chainId: chainId + }).write(); + newChain.rpcUrls.forEach((url, index) => { + db.get('rpcUrls').push({ id: chainId * 100 + index, url: url, chainId: chainId }).write(); + }); + newChain.blockExplorerUrls.forEach((url, index) => { + db.get('blockExplorerUrls').push({ id: chainId * 100 + index, url: url, chainId: chainId }).write(); + }); + + res.status(201).json(newChain); +}); + app.get('/chains/:id/full', (req, res) => { const chainId = parseInt(req.params.id, 10); const chain = db.get('chains').find({ id: chainId }).value(); From 62c5153b10b1f7a61f496e0fedd9c04d8f0fd72b Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 18:31:03 +0300 Subject: [PATCH 16/21] setup server.js --- server.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 1afbd6cc25..0eaefafeaf 100644 --- a/server.js +++ b/server.js @@ -19,10 +19,11 @@ const middlewares = jsonServer.defaults(); app.use(middlewares); app.use(jsonServer.bodyParser); +// Middleware для проверки ключа в заголовках app.use((req, res, next) => { if (req.method !== 'GET') { const apiKey = req.headers['x-api-key']; - const expectedApiKey = '12345678910'; + const expectedApiKey = 'your-secret-api-key'; // Замените на ваш реальный ключ if (!apiKey || apiKey !== expectedApiKey) { return res.status(403).json({ error: 'Forbidden' }); @@ -31,11 +32,15 @@ app.use((req, res, next) => { next(); }); +// Обработчик POST запросов для /chains app.post('/chains', (req, res) => { const newChain = req.body; const chainId = newChain.id; - db.get('chains').push(newChain).write(); + // Добавляем в основную коллекцию только основные поля + db.get('chains').push({ id: chainId, name: newChain.name }).write(); + + // Добавляем в соответствующие коллекции db.get('shortNames').push({ id: chainId, shortName: newChain.shortName, chainId: chainId }).write(); db.get('chainIds').push({ id: chainId, chainId: chainId, chainIdValue: newChain.chainIdValue }).write(); db.get('networks').push({ id: chainId, network: newChain.network, chainId: chainId }).write(); @@ -56,6 +61,7 @@ app.post('/chains', (req, res) => { res.status(201).json(newChain); }); +// Custom route to get chain info with all parameters app.get('/chains/:id/full', (req, res) => { const chainId = parseInt(req.params.id, 10); const chain = db.get('chains').find({ id: chainId }).value(); From 67fe086506d2a27cb25962ac965ad4d3212eb6db Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 18:43:23 +0300 Subject: [PATCH 17/21] setup server.js --- server.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/server.js b/server.js index 0eaefafeaf..02a43e4320 100644 --- a/server.js +++ b/server.js @@ -37,6 +37,12 @@ app.post('/chains', (req, res) => { const newChain = req.body; const chainId = newChain.id; + // Проверка на наличие объекта с таким id + const existingChain = db.get('chains').find({ id: chainId }).value(); + if (existingChain) { + return res.status(400).json({ error: 'Chain with this ID already exists' }); + } + // Добавляем в основную коллекцию только основные поля db.get('chains').push({ id: chainId, name: newChain.name }).write(); From c12f6e5c7b1c380bb295cedd517bd5755ca40ffe Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 18:49:47 +0300 Subject: [PATCH 18/21] setup server.js --- server.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/server.js b/server.js index 02a43e4320..d12edd5e4a 100644 --- a/server.js +++ b/server.js @@ -95,6 +95,16 @@ app.get('/chains/:id/full', (req, res) => { res.json(fullChainInfo); }); +// Обработчик GET запросов для /chains/:id +app.get('/chains/:id', (req, res) => { + const chainId = parseInt(req.params.id, 10); + const chain = db.get('chains').find({ id: chainId }).value(); + if (!chain) { + return res.status(404).json({ error: 'Chain not found' }); + } + res.json(chain); +}); + app.use((req, res, next) => { req.app.db = db; next(); From 8cb574f1dc5be2d3b6e59be96fd0777244ac636f Mon Sep 17 00:00:00 2001 From: zloystorm Date: Wed, 19 Jun 2024 18:56:29 +0300 Subject: [PATCH 19/21] setup server.js --- server.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/server.js b/server.js index d12edd5e4a..0d6061f12a 100644 --- a/server.js +++ b/server.js @@ -19,11 +19,10 @@ const middlewares = jsonServer.defaults(); app.use(middlewares); app.use(jsonServer.bodyParser); -// Middleware для проверки ключа в заголовках app.use((req, res, next) => { if (req.method !== 'GET') { const apiKey = req.headers['x-api-key']; - const expectedApiKey = 'your-secret-api-key'; // Замените на ваш реальный ключ + const expectedApiKey = '12345678910'; if (!apiKey || apiKey !== expectedApiKey) { return res.status(403).json({ error: 'Forbidden' }); @@ -32,21 +31,17 @@ app.use((req, res, next) => { next(); }); -// Обработчик POST запросов для /chains app.post('/chains', (req, res) => { const newChain = req.body; const chainId = newChain.id; - // Проверка на наличие объекта с таким id const existingChain = db.get('chains').find({ id: chainId }).value(); if (existingChain) { return res.status(400).json({ error: 'Chain with this ID already exists' }); } - // Добавляем в основную коллекцию только основные поля db.get('chains').push({ id: chainId, name: newChain.name }).write(); - // Добавляем в соответствующие коллекции db.get('shortNames').push({ id: chainId, shortName: newChain.shortName, chainId: chainId }).write(); db.get('chainIds').push({ id: chainId, chainId: chainId, chainIdValue: newChain.chainIdValue }).write(); db.get('networks').push({ id: chainId, network: newChain.network, chainId: chainId }).write(); @@ -67,12 +62,11 @@ app.post('/chains', (req, res) => { res.status(201).json(newChain); }); -// Custom route to get chain info with all parameters app.get('/chains/:id/full', (req, res) => { const chainId = parseInt(req.params.id, 10); const chain = db.get('chains').find({ id: chainId }).value(); if (!chain) { - return res.status(404).json({ error: 'Chain not found' }); + return res.status(404).json({ error: 'not found' }); } const shortName = db.get('shortNames').find({ chainId: chainId }).value(); @@ -95,12 +89,11 @@ app.get('/chains/:id/full', (req, res) => { res.json(fullChainInfo); }); -// Обработчик GET запросов для /chains/:id app.get('/chains/:id', (req, res) => { const chainId = parseInt(req.params.id, 10); const chain = db.get('chains').find({ id: chainId }).value(); if (!chain) { - return res.status(404).json({ error: 'Chain not found' }); + return res.status(404).json({ error: 'not found' }); } res.json(chain); }); @@ -110,7 +103,7 @@ app.use((req, res, next) => { next(); }); -const router = jsonServer.router(db); // Используем базу данных из lowdb напрямую +const router = jsonServer.router(db); app.use(router); const port = process.env.PORT || 3000; From d605f90ce5edbf7be563fc2ce8b325c19d77bd39 Mon Sep 17 00:00:00 2001 From: zloystorm Date: Thu, 20 Jun 2024 10:40:56 +0300 Subject: [PATCH 20/21] setup server.js --- server.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/server.js b/server.js index 0d6061f12a..0158c0e38a 100644 --- a/server.js +++ b/server.js @@ -89,6 +89,32 @@ app.get('/chains/:id/full', (req, res) => { res.json(fullChainInfo); }); +app.get('/chains/full', (req, res) => { + const chains = db.get('chains').value(); + + const fullChainsInfo = chains.map(chain => { + const chainId = chain.id; + const shortName = db.get('shortNames').find({ chainId: chainId }).value(); + const chainIdValue = db.get('chainIds').find({ chainId: chainId }).value(); + const network = db.get('networks').find({ chainId: chainId }).value(); + const nativeCurrency = db.get('nativeCurrencies').find({ chainId: chainId }).value(); + const rpcUrls = db.get('rpcUrls').filter({ chainId: chainId }).map('url').value(); + const blockExplorerUrls = db.get('blockExplorerUrls').filter({ chainId: chainId }).map('url').value(); + + return { + ...chain, + shortName: shortName ? shortName.shortName : null, + chainIdValue: chainIdValue ? chainIdValue.chainIdValue : null, + network: network ? network.network : null, + nativeCurrency: nativeCurrency ? nativeCurrency : null, + rpcUrls: rpcUrls, + blockExplorerUrls: blockExplorerUrls + }; + }); + + res.json(fullChainsInfo); +}); + app.get('/chains/:id', (req, res) => { const chainId = parseInt(req.params.id, 10); const chain = db.get('chains').find({ id: chainId }).value(); From 8d2968a499f239663a031cdbadf9f5dddf002cb9 Mon Sep 17 00:00:00 2001 From: zloystorm Date: Thu, 20 Jun 2024 10:53:24 +0300 Subject: [PATCH 21/21] setup server.js --- server.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/server.js b/server.js index 0158c0e38a..92b643c59d 100644 --- a/server.js +++ b/server.js @@ -62,6 +62,41 @@ app.post('/chains', (req, res) => { res.status(201).json(newChain); }); +app.put('/chains/:id', (req, res) => { + const chainId = parseInt(req.params.id, 10); + const updatedChain = req.body; + + const chain = db.get('chains').find({ id: chainId }).value(); + if (!chain) { + return res.status(404).json({ error: 'Chain not found' }); + } + + db.get('chains').find({ id: chainId }).assign({ name: updatedChain.name }).write(); + + db.get('shortNames').find({ chainId: chainId }).assign({ shortName: updatedChain.shortName }).write(); + db.get('chainIds').find({ chainId: chainId }).assign({ chainIdValue: updatedChain.chainIdValue }).write(); + db.get('networks').find({ chainId: chainId }).assign({ network: updatedChain.network }).write(); + db.get('nativeCurrencies').find({ chainId: chainId }).assign({ + name: updatedChain.nativeCurrency.name, + symbol: updatedChain.nativeCurrency.symbol, + decimals: updatedChain.nativeCurrency.decimals + }).write(); + + // Удаление старых RPC URLs и блок-эксплореров + db.get('rpcUrls').remove({ chainId: chainId }).write(); + db.get('blockExplorerUrls').remove({ chainId: chainId }).write(); + + // Добавление новых RPC URLs и блок-эксплореров + updatedChain.rpcUrls.forEach((url, index) => { + db.get('rpcUrls').push({ id: chainId * 100 + index, url: url, chainId: chainId }).write(); + }); + updatedChain.blockExplorerUrls.forEach((url, index) => { + db.get('blockExplorerUrls').push({ id: chainId * 100 + index, url: url, chainId: chainId }).write(); + }); + + res.status(200).json(updatedChain); +}); + app.get('/chains/:id/full', (req, res) => { const chainId = parseInt(req.params.id, 10); const chain = db.get('chains').find({ id: chainId }).value();