Skip to content

Commit 31c78dd

Browse files
committed
safely try read .git-revision in api.js; fixed logs in routes to include /api/
1 parent 34eec75 commit 31c78dd

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

backend/src/routes/agents.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { myGrid } = require('../grid');
77
// GET /agents get the list of all the agents on the grid
88
router.get('/', async (req, res) => {
99

10-
console.log( `GET /agents` );
10+
console.log( `GET /api/agents` );
1111

1212
const agents = Array.from( await myGrid.agents.values() ).map( agent => {
1313
return {
@@ -24,7 +24,7 @@ router.get('/', async (req, res) => {
2424
// DELETE /agents/:id delete an agent from the grid
2525
router.delete('/:id', async (req, res) => {
2626

27-
console.log( `DELETE /agents/${req.params.id}` );
27+
console.log( `DELETE /api/agents/${req.params.id}` );
2828

2929
const id = req.params.id;
3030
const agent = myGrid.agents.get( id );

backend/src/routes/api.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,55 @@
11
const express = require('express');
22
const router = express.Router();
3-
const config = require('../../config');
4-
const { myGrid } = require('../grid');
5-
const { execSync } = require('child_process');
63
const fs = require('fs');
74

5+
const packageVersion = new Promise( res =>
6+
fs.readFile('./package.json', { encoding: 'utf8'}, (err, data) => {
7+
8+
let packageVersion = 'no package.json';
9+
10+
try {
11+
if ( err )
12+
throw err;
13+
else if ( data )
14+
packageVersion = JSON.parse(data).version;
15+
} catch (error) {
16+
console.error('Error while reading package.json', error);
17+
}
18+
19+
console.log( 'api.js packageVersion =', 'no package.json' );
20+
res( packageVersion )
21+
} )
22+
);
23+
24+
const commitHash = new Promise( res =>
25+
fs.readFile('./.git-revision', { encoding: 'utf8'}, (err, data) => {
26+
27+
let commitHash = 'no git';
28+
29+
try {
30+
if ( err )
31+
throw err;
32+
else if ( data )
33+
commitHash = data.toString().trim();
34+
} catch (error) {
35+
console.error('Error while reading commit hash from .git-revision', error);
36+
}
37+
38+
console.log( 'api.js commitHash =', commitHash );
39+
res( commitHash );
40+
41+
} )
42+
);
43+
844
// GET /configs
945
router.get('/', async (req, res) => {
1046

11-
console.log(`GET /version`)
47+
console.log(`GET /api`);
1248

13-
const packageVersion = await new Promise( res =>
14-
fs.readFile('./package.json', { encoding: 'utf8'}, (err, data) => res( JSON.parse(data).version ) )
15-
);
16-
17-
const commitHash = await new Promise( res =>
18-
fs.readFile('./.git-revision', { encoding: 'utf8'}, (err, data) => res( data.toString().trim() ) )
19-
);
20-
2149
res.status(200).json( {
2250
message: 'Welcome to the API',
23-
commitHash,
24-
packageVersion
51+
commitHash: await commitHash,
52+
packageVersion: await packageVersion
2553
} );
2654

2755
})

backend/src/routes/configs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { myGrid } = require('../grid');
66
// GET /configs
77
router.get('/', async (req, res) => {
88

9-
console.log(`GET /configs`)
9+
console.log(`GET /api/configs`)
1010
res.status(200).json( config );
1111

1212
})

backend/src/routes/levels.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const router = express.Router();
66
// GET /all levels
77
router.get('/', async (req, res) => {
88

9-
console.log(`GET /levels`);
9+
console.log(`GET /api/levels`);
1010

1111
// load all .js files from levels directory
1212
fs.readdir( './levels', (err, files) => {
@@ -39,7 +39,7 @@ router.get('/', async (req, res) => {
3939
router.get('/:levelName', async (req, res) => {
4040
const levelName = req.params.levelName;
4141

42-
console.log(`GET /levels/${levelName}`);
42+
console.log(`GET /api/levels/${levelName}`);
4343

4444
const level = { self: '/api/levels/' + levelName };
4545
try {

backend/src/routes/npcs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { myNPCSpawner } = require('../grid');
77
// GET /npcs get the list of all npcs on the grid
88
router.get('/', async (req, res) => {
99

10-
console.log( `GET /npcs` );
10+
console.log( `GET /api/npcs` );
1111

1212
const agents = Array.from( await myNPCSpawner.NPCs.values() ).map( npc => {
1313
return {
@@ -50,7 +50,7 @@ router.get('/:id', async (req, res) => {
5050
// PATCH /npcs/:id start or stop the npc with id
5151
router.patch('/:id', async (req, res) => {
5252

53-
console.log( `PATCH /npcs/${req.params.id}`, req.body );
53+
console.log( `PATCH /api/npcs/${req.params.id}`, req.body );
5454

5555
const npc = myNPCSpawner.NPCs.get( req.params.id );
5656
if ( npc ) {
@@ -79,7 +79,7 @@ router.patch('/:id', async (req, res) => {
7979
// POST /npcs create a new npc
8080
router.post('/', async (req, res) => {
8181

82-
console.log( `POST /npcs`, req.body );
82+
console.log( `POST /api/npcs`, req.body );
8383

8484
const npc = myNPCSpawner.createNPC();
8585
res.status(200).json( {

0 commit comments

Comments
 (0)