|
1 |
| -import express, { Express, Request, Response, Application } from "express"; |
2 |
| -import dotenv from "dotenv"; |
| 1 | +import express, { Request, Response } from "express"; |
| 2 | +import { PrismaClient } from "@prisma/client"; |
3 | 3 |
|
4 |
| -//For env File |
5 |
| -dotenv.config(); |
| 4 | +const prisma = new PrismaClient(); |
| 5 | +const app = express(); |
| 6 | +const PORT = 3000; |
6 | 7 |
|
7 |
| -const app: Application = express(); |
8 |
| -const port = process.env.PORT || 8000; |
| 8 | +app.use(express.json()); |
9 | 9 |
|
10 |
| -app.get("/", (req: Request, res: Response) => { |
11 |
| - res.send("Welcome to Express & TypeScript Server"); |
| 10 | +// GET /logs endpoint to query logs |
| 11 | +app.get("/logs", async (req: Request, res: Response) => { |
| 12 | + try { |
| 13 | + // Extract filter parameters from query string |
| 14 | + const { fromBlock, toBlock, address, topics } = req.query; |
| 15 | + |
| 16 | + // Prepare filter object based on query parameters |
| 17 | + const filter: any = {}; |
| 18 | + if (fromBlock) filter.blockNumber = { gte: parseInt(fromBlock as string) }; |
| 19 | + if (toBlock) |
| 20 | + filter.blockNumber = { |
| 21 | + ...filter.blockNumber, |
| 22 | + lte: parseInt(toBlock as string), |
| 23 | + }; |
| 24 | + if (address) filter.address = address; |
| 25 | + if (topics) |
| 26 | + filter.topics = { |
| 27 | + has: topics, |
| 28 | + }; |
| 29 | + |
| 30 | + const logs = await prisma.log.findMany({ |
| 31 | + where: filter, |
| 32 | + }); |
| 33 | + |
| 34 | + res.json(logs); |
| 35 | + } catch (error) { |
| 36 | + console.error("Error querying logs:", error); |
| 37 | + res.status(500).json({ error: "Internal server error" }); |
| 38 | + } |
12 | 39 | });
|
13 | 40 |
|
14 |
| -app.listen(port, () => { |
15 |
| - console.log(`Server is Fire at http://localhost:${port}`); |
| 41 | +// Start the server |
| 42 | +app.listen(PORT, () => { |
| 43 | + console.log(`Server is running on port ${PORT}`); |
16 | 44 | });
|
0 commit comments