Adapters
chemistry
biology
finance
legal
art
climate
agent
Merge
BACCHUS45 commited on
Commit
12ba915
·
verified ·
1 Parent(s): ac179f4

Create detection-service/index.js

Browse files
Files changed (1) hide show
  1. detection-service/index.js +45 -0
detection-service/index.js ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Minimal detection service: receives satellite/infra events, writes to Postgres and emits to Redis
2
+ const express = require('express');
3
+ const bodyParser = require('body-parser');
4
+ const { Pool } = require('pg');
5
+ const Redis = require('ioredis');
6
+ const { v4: uuidv4 } = require('uuid');
7
+
8
+ const pool = new Pool({ connectionString: process.env.DATABASE_URL || 'postgresql://localhost/integral' });
9
+ const redis = new Redis(process.env.REDIS_URL || 'redis://localhost:6379');
10
+
11
+ const app = express();
12
+ app.use(bodyParser.json());
13
+
14
+ /**
15
+ Expected payload: {
16
+ namespace: "satellite" | "infrastructure",
17
+ type: "pothole-detection",
18
+ timestamp: "2025-11-30T10:00:00Z",
19
+ location: { lat: -29.12, lon: 26.22 },
20
+ severity: 3,
21
+ images: ["https://..."],
22
+ provenance: {...}
23
+ }
24
+ **/
25
+ app.post('/detect', async (req, res) => {
26
+ try {
27
+ const { namespace, type, timestamp, location, severity=1, images=[], provenance={} } = req.body;
28
+ const id = uuidv4();
29
+ const q = `INSERT INTO objects (id, namespace, type, timestamp, location, severity, images, provenance)
30
+ VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING *`;
31
+ const values = [id, namespace, type, timestamp || new Date().toISOString(), location || null, severity, JSON.stringify(images), provenance];
32
+ const result = await pool.query(q, values);
33
+ const obj = result.rows[0];
34
+
35
+ // publish event for verification
36
+ await redis.publish('infra:faults:created', JSON.stringify({ id: obj.id, namespace, type, location, severity }));
37
+
38
+ res.status(201).json(obj);
39
+ } catch (err) {
40
+ console.error(err);
41
+ res.status(500).json({ error: 'detection failed' });
42
+ }
43
+ });
44
+
45
+ app.listen(process.env.PORT || 3001, () => console.log('detection-service listening on 3001'));