Skip to content

Commit

Permalink
fix: make api respond with actual books
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-osch committed Jan 28, 2025
1 parent dc30fd9 commit ed19fad
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 63 deletions.
109 changes: 54 additions & 55 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,68 @@
require("./tracing");
const fs = require("fs");
const express = require("express");
const cors = require("cors");
const { v4: uuidv4 } = require("uuid");
const morgan = require("morgan");
require('./tracing')
const fs = require('fs')
const express = require('express')
const cors = require('cors')
const { v4: uuidv4 } = require('uuid')
const morgan = require('morgan')

const app = express();
const port = process.env.PORT || 5000;
const app = express()
const port = process.env.PORT || 5000

app.use(express.json());
app.use(cors());
app.use(morgan("tiny"));
app.use(express.json())
app.use(cors())
app.use(morgan('tiny'))

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

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

app.get("/api/books", (req, res) => {
if (fs.existsSync("downtime.json")) {
console.log('Downtime detected');
res.status(500).send();
} else {
const rawData = fs.readFileSync("books.json");
const books = JSON.parse(rawData);
app.get('/api/books', (req, res) => {
if (fs.existsSync('downtime.json')) {
console.log('Downtime detected')
res.status(500).send()
} else {
const rawData = fs.readFileSync('books.json')
const books = JSON.parse(rawData)

res.status(200).json(books);
}
});
res.status(200).json(books)
}
})

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

app.get("/api/users/login", (req, res) => {
res.status(200).json({
message: "Login successful",
token: uuidv4(),
name: "Danube",
});
});
app.get('/api/users/login', (req, res) => {
res.status(200).json({
message: 'Login successful',
token: uuidv4(),
name: 'Danube',
})
})

app.post("/api/toggle", (req, res) => {
if (fs.existsSync("downtime.json")) {
fs.unlinkSync("downtime.json");
res.status(200).send();
} else {
fs.writeFileSync("downtime.json", "{}");
res.status(200).send();
}
});
app.post('/api/toggle', (req, res) => {
if (fs.existsSync('downtime.json')) {
fs.unlinkSync('downtime.json')
res.status(200).send()
} else {
fs.writeFileSync('downtime.json', '{}')
res.status(200).send()
}
})

app.get('/api/downtime', (req, res) => {
const downtime = fs.existsSync('downtime.json')
res.status(200).json({ downtime })
});
const downtime = fs.existsSync('downtime.json')
res.status(200).json({ downtime })
})

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

app.listen(port, () => console.log(`Example app listening on port ${port}!`));
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
21 changes: 13 additions & 8 deletions tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const {
getNodeAutoInstrumentations,
} = require('@opentelemetry/auto-instrumentations-node')

const shouldEnableTracing = process.env.OTEL_URL && process.env.OTEL_TOKEN

const exporter = new OTLPTraceExporter({
timeoutMillis: 2000,
url: process.env.OTEL_URL,
headers: {
Authorization: process.env.OTEL_TOKEN
Authorization: process.env.OTEL_TOKEN,
},
})

Expand Down Expand Up @@ -41,14 +43,17 @@ const sdk = new NodeSDK({
},
})],
})
sdk.start()

process.on("SIGTERM", () => {
sdk
if (shouldEnableTracing) {
sdk.start()

process.on('SIGTERM', () => {
sdk
.shutdown()
.then(
() => console.log("SDK shut down successfully"),
(err) => console.log("Error shutting down SDK", err)
() => console.log('SDK shut down successfully'),
(err) => console.log('Error shutting down SDK', err),
)
.finally(() => process.exit(0));
});
.finally(() => process.exit(0))
})
}

0 comments on commit ed19fad

Please sign in to comment.