Skip to content

Commit ed19fad

Browse files
committed
fix: make api respond with actual books
1 parent dc30fd9 commit ed19fad

File tree

2 files changed

+67
-63
lines changed

2 files changed

+67
-63
lines changed

app.js

+54-55
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,68 @@
1-
require("./tracing");
2-
const fs = require("fs");
3-
const express = require("express");
4-
const cors = require("cors");
5-
const { v4: uuidv4 } = require("uuid");
6-
const morgan = require("morgan");
1+
require('./tracing')
2+
const fs = require('fs')
3+
const express = require('express')
4+
const cors = require('cors')
5+
const { v4: uuidv4 } = require('uuid')
6+
const morgan = require('morgan')
77

8-
const app = express();
9-
const port = process.env.PORT || 5000;
8+
const app = express()
9+
const port = process.env.PORT || 5000
1010

11-
app.use(express.json());
12-
app.use(cors());
13-
app.use(morgan("tiny"));
11+
app.use(express.json())
12+
app.use(cors())
13+
app.use(morgan('tiny'))
1414

15-
app.get("/404", (req, res) => {
16-
res.status(404).sendFile(__dirname + "/404.html");
17-
});
15+
app.get('/404', (req, res) => {
16+
res.status(404).sendFile(__dirname + '/404.html')
17+
})
1818

19-
app.use(express.static(__dirname + "/vue/dist"));
19+
app.use(express.static(__dirname + '/vue/dist'))
2020

21-
app.get("/api/books", (req, res) => {
22-
if (fs.existsSync("downtime.json")) {
23-
console.log('Downtime detected');
24-
res.status(500).send();
25-
} else {
26-
const rawData = fs.readFileSync("books.json");
27-
const books = JSON.parse(rawData);
21+
app.get('/api/books', (req, res) => {
22+
if (fs.existsSync('downtime.json')) {
23+
console.log('Downtime detected')
24+
res.status(500).send()
25+
} else {
26+
const rawData = fs.readFileSync('books.json')
27+
const books = JSON.parse(rawData)
2828

29-
res.status(200).json(books);
30-
}
31-
});
29+
res.status(200).json(books)
30+
}
31+
})
3232

33-
app.get("/api/books/:id", (req, res) => {
34-
const rawData = fs.readFileSync("books.json");
35-
const books = JSON.parse(rawData);
36-
var arrayFound = books.filter(function (item) {
37-
return item.id === req.params.id;
38-
});
39-
res.status(200).json(arrayFound[0]);
40-
});
33+
app.get('/api/books/:id', (req, res) => {
34+
const rawData = fs.readFileSync('books.json', 'utf8')
35+
const books = JSON.parse(rawData)
36+
const idAsNumber = Number(req.params.id)
37+
const found = books.find((item) => item.id === idAsNumber)
38+
res.status(200).json(found)
39+
})
4140

42-
app.get("/api/users/login", (req, res) => {
43-
res.status(200).json({
44-
message: "Login successful",
45-
token: uuidv4(),
46-
name: "Danube",
47-
});
48-
});
41+
app.get('/api/users/login', (req, res) => {
42+
res.status(200).json({
43+
message: 'Login successful',
44+
token: uuidv4(),
45+
name: 'Danube',
46+
})
47+
})
4948

50-
app.post("/api/toggle", (req, res) => {
51-
if (fs.existsSync("downtime.json")) {
52-
fs.unlinkSync("downtime.json");
53-
res.status(200).send();
54-
} else {
55-
fs.writeFileSync("downtime.json", "{}");
56-
res.status(200).send();
57-
}
58-
});
49+
app.post('/api/toggle', (req, res) => {
50+
if (fs.existsSync('downtime.json')) {
51+
fs.unlinkSync('downtime.json')
52+
res.status(200).send()
53+
} else {
54+
fs.writeFileSync('downtime.json', '{}')
55+
res.status(200).send()
56+
}
57+
})
5958

6059
app.get('/api/downtime', (req, res) => {
61-
const downtime = fs.existsSync('downtime.json')
62-
res.status(200).json({ downtime })
63-
});
60+
const downtime = fs.existsSync('downtime.json')
61+
res.status(200).json({ downtime })
62+
})
6463

6564
app.get(/.*/, (req, res) => {
66-
res.sendFile(__dirname + "/vue/dist/index.html");
67-
});
65+
res.sendFile(__dirname + '/vue/dist/index.html')
66+
})
6867

69-
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
68+
app.listen(port, () => console.log(`Example app listening on port ${port}!`))

tracing.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ const {
77
getNodeAutoInstrumentations,
88
} = require('@opentelemetry/auto-instrumentations-node')
99

10+
const shouldEnableTracing = process.env.OTEL_URL && process.env.OTEL_TOKEN
11+
1012
const exporter = new OTLPTraceExporter({
1113
timeoutMillis: 2000,
1214
url: process.env.OTEL_URL,
1315
headers: {
14-
Authorization: process.env.OTEL_TOKEN
16+
Authorization: process.env.OTEL_TOKEN,
1517
},
1618
})
1719

@@ -41,14 +43,17 @@ const sdk = new NodeSDK({
4143
},
4244
})],
4345
})
44-
sdk.start()
4546

46-
process.on("SIGTERM", () => {
47-
sdk
47+
if (shouldEnableTracing) {
48+
sdk.start()
49+
50+
process.on('SIGTERM', () => {
51+
sdk
4852
.shutdown()
4953
.then(
50-
() => console.log("SDK shut down successfully"),
51-
(err) => console.log("Error shutting down SDK", err)
54+
() => console.log('SDK shut down successfully'),
55+
(err) => console.log('Error shutting down SDK', err),
5256
)
53-
.finally(() => process.exit(0));
54-
});
57+
.finally(() => process.exit(0))
58+
})
59+
}

0 commit comments

Comments
 (0)