kann deployed werde (zum ersten Mal)
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { get_pflux } from '../db/mongo.js';
|
||||
import { getCollections, update_pflux, clientClose } from '../db/mongo.js';
|
||||
|
||||
export function registerAddressRoute(app, requireLogin) {
|
||||
const ADDRESS_SERVICE_URL = process.env.ADDRESS_SERVICE_URL || 'https://noise.fuerst-stuttgart.de/srv/getaddress';
|
||||
const APIHOST = process.env.APIHOST || 'https://noise.fuerst-stuttgart.de/srv/';
|
||||
|
||||
app.get('/api/address/:sensorNumber', requireLogin, async (req, res) => {
|
||||
const sensorNumber = parseInt(req.params.sensorNumber, 10);
|
||||
@@ -10,18 +11,12 @@ export function registerAddressRoute(app, requireLogin) {
|
||||
return res.status(400).json({ error: 'Ungültige Sensornummer' });
|
||||
}
|
||||
|
||||
// Verbindung zur sensor_data DB
|
||||
const mongoUri = process.env.MONGO_URI || 'mongodb://localhost:27017';
|
||||
const client = new MongoClient(mongoUri);
|
||||
await client.connect();
|
||||
const sensorDb = client.db('sensor_data');
|
||||
const properties = sensorDb.collection('properties');
|
||||
const propFlux = sensorDb.collection('prop_flux');
|
||||
|
||||
const { propertiesCollection, prop_fluxCollection } = getCollections();
|
||||
|
||||
// Suche nach Sensornummer als _id
|
||||
const propEntry = await properties.findOne({ _id: sensorNumber });
|
||||
const propEntry = await propertiesCollection.findOne({ _id: sensorNumber });
|
||||
if (!propEntry) {
|
||||
await client.close();
|
||||
await clientClose()
|
||||
return res.status(404).json({ error: 'Sensor nicht gefunden' });
|
||||
}
|
||||
|
||||
@@ -38,6 +33,8 @@ export function registerAddressRoute(app, requireLogin) {
|
||||
} catch (err) {
|
||||
console.error('Fehler beim Kopieren nach prop_flux:', err);
|
||||
// Kein Abbruch, nur Logging
|
||||
} finally {
|
||||
await clientClose()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +42,8 @@ export function registerAddressRoute(app, requireLogin) {
|
||||
let addressString = '';
|
||||
let addrParts = {};
|
||||
try {
|
||||
const url = ADDRESS_SERVICE_URL + `?sensorid=${encodeURIComponent(propEntry._id)}`;
|
||||
const url = APIHOST + 'getaddress/' + `?sensorid=${encodeURIComponent(propEntry._id)}`;
|
||||
console.log(url)
|
||||
const r = await fetch(url, { headers: { 'Accept': 'application/json' } });
|
||||
if (r.ok) {
|
||||
const data = await r.json();
|
||||
@@ -61,8 +59,6 @@ export function registerAddressRoute(app, requireLogin) {
|
||||
console.error('Address lookup failed:', err);
|
||||
}
|
||||
|
||||
await client.close();
|
||||
|
||||
return res.json({
|
||||
address: addressString,
|
||||
parts: addrParts,
|
||||
|
||||
@@ -3,7 +3,7 @@ import bcrypt from 'bcrypt';
|
||||
import { getCollections, update_pflux } from '../db/mongo.js';
|
||||
|
||||
export function registerApiRoutes(app, requireLogin) {
|
||||
const { entriesCollection, usersCollection, prop_fluxCollection } = getCollections();
|
||||
const { usersCollection, prop_fluxCollection } = getCollections();
|
||||
|
||||
app.get('/api/check-email', async (req, res) => {
|
||||
const email = (req.query.email || '').toLowerCase().trim();
|
||||
|
||||
Reference in New Issue
Block a user