-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into feature/addDataModel
- Loading branch information
Showing
4 changed files
with
119 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
'use strict'; | ||
|
||
import _ from 'lodash'; | ||
|
||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
import cors from 'cors'; | ||
import fileUpload from 'express-fileupload'; | ||
import { V1Router } from './routes/v1'; | ||
import { getConfig } from './utils/config-loader'; | ||
import { logger } from './config/logger.cjs'; | ||
import { | ||
assertChiaNetworkMatchInConfiguration, | ||
assertDataLayerAvailable, | ||
assertWalletIsAvailable, | ||
} from './utils/data-assertions'; | ||
import packageJson from '../package.json'; | ||
|
||
const { API_KEY, READ_ONLY } = getConfig().APP; | ||
|
||
const headerKeys = Object.freeze({ | ||
API_VERSION_HEADER_KEY: 'x-api-version', | ||
CR_READY_ONLY_HEADER_KEY: 'cw-read-only', | ||
DATA_MODEL_VERION_HEADER_KEY: 'x-datamodel-version', | ||
}); | ||
|
||
const app = express(); | ||
|
||
app.use( | ||
cors({ | ||
exposedHeaders: Object.values(headerKeys).join(','), | ||
}), | ||
); | ||
|
||
app.use(express.json()); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(fileUpload()); | ||
|
||
// Common assertions on every endpoint | ||
app.use(async function (req, res, next) { | ||
try { | ||
await assertChiaNetworkMatchInConfiguration(); | ||
await assertDataLayerAvailable(); | ||
await assertWalletIsAvailable(); | ||
next(); | ||
} catch (err) { | ||
res.status(400).json({ | ||
message: 'Chia Exception', | ||
error: err.message, | ||
}); | ||
} | ||
}); | ||
|
||
// Add optional API key if set in .env file | ||
app.use(function (req, res, next) { | ||
if (API_KEY && API_KEY !== '') { | ||
const apikey = req.header('x-api-key'); | ||
if (API_KEY === apikey) { | ||
next(); | ||
} else { | ||
res.status(403).json({ message: 'API key not found' }); | ||
} | ||
} else { | ||
next(); | ||
} | ||
}); | ||
|
||
app.use(function (req, res, next) { | ||
if (READ_ONLY) { | ||
res.setHeader(headerKeys.CR_READY_ONLY_HEADER_KEY, READ_ONLY); | ||
} else { | ||
res.setHeader(headerKeys.CR_READY_ONLY_HEADER_KEY, false); | ||
} | ||
|
||
next(); | ||
}); | ||
|
||
app.use(function (req, res, next) { | ||
logger.debug( | ||
`Setting header x-api-verion to package.json version: ${packageJson.version}`, | ||
); | ||
const version = packageJson.version; | ||
res.setHeader(headerKeys.API_VERSION_HEADER_KEY, version); | ||
|
||
const majorVersion = version.split('.')[0]; | ||
res.setHeader(headerKeys.DATA_MODEL_VERION_HEADER_KEY, `v${majorVersion}`); | ||
|
||
next(); | ||
}); | ||
|
||
app.use('/v1', V1Router); | ||
|
||
app.use((err, req, res, next) => { | ||
if (err) { | ||
if (_.get(err, 'error.details')) { | ||
// format Joi validation errors | ||
return res.status(400).json({ | ||
message: 'Data Validation error', | ||
errors: err.error.details.map((detail) => { | ||
return _.get(detail, 'context.message', detail.message); | ||
}), | ||
}); | ||
} | ||
|
||
return res.status(err.status).json(err); | ||
} | ||
|
||
next(); | ||
}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters