forked from VinokurovVE/tests
Rename; Added EMS server; redis compose
This commit is contained in:
62
ems/src/index.ts
Normal file
62
ems/src/index.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import express, { Request, Response } from 'express'
|
||||
import { Redis } from 'ioredis'
|
||||
import dotenv from 'dotenv'
|
||||
import bodyParser from 'body-parser'
|
||||
|
||||
const cors = require('cors')
|
||||
|
||||
const redis = new Redis({
|
||||
port: Number(process.env.REDIS_PORT) || 6379,
|
||||
host: process.env.REDIS_HOST,
|
||||
password: process.env.REDIS_PASSWORD,
|
||||
})
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const app = express()
|
||||
const port = process.env.EMS_PORT
|
||||
|
||||
// Middleware to parse JSON requests
|
||||
app.use(bodyParser.json())
|
||||
|
||||
app.get('/hello', cors(), (req: Request, res: Response) => {
|
||||
res.send('Hello, World!')
|
||||
})
|
||||
|
||||
// Route to store GeoJSON data
|
||||
app.post('/geojson', cors(), async (req: Request, res: Response) => {
|
||||
const geoJSON = req.body
|
||||
|
||||
if (!geoJSON || !geoJSON.features) {
|
||||
return res.status(400).send('Invalid GeoJSON')
|
||||
}
|
||||
|
||||
const id = `geojson:${Date.now()}`;
|
||||
redis.set(id, JSON.stringify(geoJSON), (err, reply) => {
|
||||
if (err) {
|
||||
return res.status(500).send('Error saving GeoJSON to Redis');
|
||||
}
|
||||
res.send({ status: 'success', id });
|
||||
})
|
||||
})
|
||||
|
||||
// Route to fetch GeoJSON data
|
||||
app.get('/geojson/:id', cors(), async (req: Request, res: Response) => {
|
||||
const id = req.params.id;
|
||||
|
||||
redis.get(id, (err, data) => {
|
||||
if (err) {
|
||||
return res.status(500).send('Error fetching GeoJSON from Redis');
|
||||
}
|
||||
|
||||
if (data) {
|
||||
res.send(JSON.parse(data));
|
||||
} else {
|
||||
res.status(404).send('GeoJSON not found');
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server running on http://localhost:${port}`);
|
||||
})
|
Reference in New Issue
Block a user