Skip to content

Commit e0554f9

Browse files
authored
Merge pull request #13 from notEduardo/adding-endpoints
adding combination endpoint
2 parents bbc9ec8 + 68f41ce commit e0554f9

File tree

6 files changed

+118
-10
lines changed

6 files changed

+118
-10
lines changed

README.md

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
21
# Faucet Backend API
32

4-
This API provides endpoints for interacting with two main tables: `faucet.solana_balances` and `faucet.rate_limits`. Below are the available endpoints for each table.
3+
This API provides endpoints for interacting with two main tables: `faucet.solana_balances` and `faucet.rate_limits`.
4+
Below are the available endpoints for each table.
55

66
---
7+
78
## How to Run
89

910
1. Clone the repository
@@ -78,8 +79,7 @@ This API provides endpoints for interacting with two main tables: `faucet.solana
7879
"account": "string",
7980
"balance": "number",
8081
"date": "timestamp"
81-
},
82-
...
82+
}
8383
]
8484
```
8585

@@ -99,8 +99,7 @@ This API provides endpoints for interacting with two main tables: `faucet.solana
9999
"account": "string",
100100
"balance": "number",
101101
"date": "timestamp"
102-
},
103-
...
102+
}
104103
]
105104
```
106105

@@ -176,6 +175,40 @@ This API provides endpoints for interacting with two main tables: `faucet.solana
176175
}
177176
```
178177

178+
### **Create a New Rate Limit Combination**
179+
180+
**POST** `/api/rate-limits-combo`
181+
182+
- **Description**: Adds a new rate limit combination entry. Each combination of `ip_address`, `wallet_address`,
183+
and `github_userid` is checked for uniqueness before inserting to DB.
184+
- **Request Body**:
185+
```json
186+
{
187+
"ip_address": "string",
188+
"wallet_address": "string",
189+
"github_userid": "string"
190+
}
191+
```
192+
- **Curl Command**:
193+
```bash
194+
curl -v -X POST http://localhost:3000/api/rate-limits-combo \
195+
-H "Content-Type: application/json" \
196+
-d '{
197+
"ip_address": "19216801",
198+
"wallet_address": "wallet_123",
199+
"github_userid": "user123"
200+
}'
201+
```
202+
- **Response**:
203+
```json
204+
{
205+
"id": "3",
206+
"ip_address":"19216801",
207+
"wallet_address":"wallet_123",
208+
"github_userid":"user123"
209+
}
210+
```
211+
179212
---
180213

181214
## Error Handling

src/db/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const { Pool } = pg;
44

55
dotenv.config();
66

7-
const pgClient = new Pool({
7+
const pgClient = new Pool(process.env.DB_CONNECTION_NAME ? {
88
user: process.env.DB_USER,
99
database: process.env.DB_NAME,
1010
password: process.env.DB_PASSWORD,
1111
host: `/cloudsql/${process.env.DB_CONNECTION_NAME}`
12+
} : {
13+
connectionString: process.env.POSTGRES_STRING
1214
});
1315

1416
const query = (text, params) => pgClient.query(text, params);

src/db/rateLimits.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,23 @@ const deleteRateLimit = async (key) => {
4444
return result.rows[0];
4545
};
4646

47+
async function createRateLimitCombo(ip_address, wallet_address, github_userid ) {
48+
const query = `
49+
INSERT INTO faucet.rate_limits_combo (ip_address, wallet_address, github_userid)
50+
VALUES ($1, $2, $3)
51+
RETURNING *;
52+
`;
53+
54+
const values = [ip_address, wallet_address, github_userid];
55+
const result = await db.query(query, values);
56+
57+
return result.rows[0];
58+
}
59+
4760
export default {
4861
createRateLimit,
4962
getRateLimit,
5063
updateRateLimit,
5164
deleteRateLimit,
65+
createRateLimitCombo
5266
};

src/db/rateLimitsCombo.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import db from './config.js'; // Import the database configuration
2+
3+
const addRateLimitCombo = async (ip_address, wallet_address, github_userid) => {
4+
const query = `
5+
INSERT INTO faucet.rate_limits_combo (ip_address, wallet_address, github_userid)
6+
VALUES ($1, $2, $3)
7+
ON CONFLICT (ip_address, wallet_address, github_userid) DO NOTHING
8+
RETURNING *;
9+
`;
10+
const values = [ip_address, wallet_address, github_userid];
11+
12+
try {
13+
const result = await db.query(query, values);
14+
15+
if (result.rowCount === 0) {
16+
console.log('Duplicate combination found:', {ip_address, wallet_address, github_userid});
17+
} else {
18+
console.log('New combo inserted:', result.rows[0]);
19+
}
20+
21+
return result.rows[0];
22+
} catch (error) {
23+
console.error('Error during combo creation:', error);
24+
throw error;
25+
}
26+
};
27+
28+
export default {
29+
addRateLimitCombo,
30+
};

src/routes/middleware/authorization.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { OAuth2Client } from 'google-auth-library';
33
const oAuth2Client = new OAuth2Client();
44

55
const validateGoogleToken = async (req, res, next) => {
6+
if(process.env.POSTGRES_STRING) {
7+
return next();
8+
}
9+
610
const authHeader = req.header('Authorization');
711
if (!authHeader) {
812
return res.status(401).json({ message: 'Unauthorized' });

src/routes/rateLimitRoute.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import rateLimits from '../db/rateLimits.js'; // Import the CRUD methods for the
33

44
const router = express.Router();
55

6-
// CREATE a new rate limit
6+
// POST a new rate limit
77
router.post('/rate-limits', async (req, res, next) => {
88
const { key, timestamps } = req.body;
99

@@ -16,7 +16,7 @@ router.post('/rate-limits', async (req, res, next) => {
1616
}
1717
});
1818

19-
// READ a rate limit by key
19+
// GET a rate limit by key
2020
router.get('/rate-limits/:key', async (req, res, next) => {
2121
const { key } = req.params;
2222

@@ -34,7 +34,7 @@ router.get('/rate-limits/:key', async (req, res, next) => {
3434
}
3535
});
3636

37-
// UPDATE timestamps for a rate limit by key
37+
// PUT timestamps for a rate limit by key
3838
router.put('/rate-limits/:key', async (req, res, next) => {
3939
const { key } = req.params;
4040
const { timestamps } = req.body;
@@ -53,4 +53,29 @@ router.put('/rate-limits/:key', async (req, res, next) => {
5353
}
5454
});
5555

56+
// POST rate limit combination
57+
router.post('/rate-limits-combo', async (req, res, next) => {
58+
const { ip_address, wallet_address, github_userid } = req.body;
59+
60+
// Validate the input
61+
if (!ip_address || !wallet_address) {
62+
return res.status(400).json({ message: 'All fields (ip_address, wallet_address) are required.' });
63+
}
64+
65+
try {
66+
// Insert the new combination into the database
67+
const newCombo = await rateLimits.createRateLimitCombo(ip_address, wallet_address, github_userid ?? '');
68+
69+
res.status(201).json(newCombo);
70+
} catch (error) {
71+
if (error.code === '23505') { // Unique violation error code for PostgreSQL
72+
console.warn('Duplicate rate limit combo:', error.detail);
73+
res.status(409).json({ message: 'Combination of ip_address, wallet_address, and github_userid already exists.' });
74+
} else {
75+
console.error('Error creating rate limit combo:', error);
76+
next(error);
77+
}
78+
}
79+
});
80+
5681
export default router;

0 commit comments

Comments
 (0)