Skip to content

Commit

Permalink
feat: déclaration de la configuration applicative
Browse files Browse the repository at this point in the history
Ça a le bénéfice de faire planter l'application au démarrage en cas de réglage absent.
  • Loading branch information
thom4parisot committed Sep 30, 2024
1 parent 487ff67 commit b541045
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 92 deletions.
138 changes: 80 additions & 58 deletions export/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"test": "jest",
"start": "node src/app.js",
"dev": "npx dotenv -e ../stylo.env npm run start",
"dev": "node src/app.js",
"prod": "NODE_ENV=production node --heapsnapshot-signal=SIGUSR2 src/app.js"
},
"repository": {
Expand All @@ -33,6 +33,8 @@
"homepage": "https://github.com/EcrituresNumeriques/stylo#readme",
"dependencies": {
"archiver": "5.3.2",
"convict": "^6.2.4",
"convict-format-with-validator": "^6.2.0",
"cors": "^2.8.5",
"express": "^4.16.4",
"graphql": "^16.1.0",
Expand All @@ -44,7 +46,6 @@
},
"devDependencies": {
"dotenv": "^16.0.1",
"dotenv-cli": "^5.1.0",
"jest": "^29.5.0"
},
"husky": {
Expand Down
4 changes: 3 additions & 1 deletion export/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { logger } = require('./logger')
const pino = require('pino-http')({
logger
})
const config = require('./config.js')
config.validate({ allowed: 'strict' })

const {
exportArticleHtml,
Expand All @@ -29,7 +31,7 @@ app.use(cors({
origin: '*'
}))

const listenPort = process.env.PORT || 3060
const listenPort = config.get('port')

const asyncExportVersionHtml = asyncHandler(exportVersionHtml)
const asyncExportArticleHtml = asyncHandler(exportArticleHtml)
Expand Down
41 changes: 41 additions & 0 deletions export/src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const convict = require('convict')
require('dotenv').config({ path: '../../stylo.env' })

convict.addFormat(require('convict-format-with-validator').url)

/**
* @type {convict.Config<{
*
* }>}
*/
module.exports = convict({
port: {
default: 3060,
format: 'port',
env: 'PORT'
},
api: {
urlEndpoint: {
default: 'http://localhost:3030/graphql',
format: 'url',
env: 'SNOWPACK_PUBLIC_GRAPHQL_ENDPOINT'
},
passthroughToken: {
format: 'string',
sensitive: true,
env: 'SE_GRAPHQL_TOKEN'
}
},
export: {
// legacy option
canonicalBaseUrl: {
format: 'url',
env: 'EXPORT_CANONICAL_BASE_URL'
},
urlEndpoint: {
default: 'http://127.0.0.1:3080',
format: 'url',
env: 'SNOWPACK_PUBLIC_PANDOC_EXPORT_ENDPOINT'
}
}
})
5 changes: 3 additions & 2 deletions export/src/export.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const config = require('./config.js')
const archiver = require('archiver')

const { logger } = require('./logger')
const { FindByIdNotFoundError } = require('./helpers/errors')
const { normalize } = require('./helpers/filename')
const { getArticleById, getVersionById, getCorpusById } = require('./graphql')

const canonicalBaseUrl = process.env.EXPORT_CANONICAL_BASE_URL
const exportEndpoint = process.env.SNOWPACK_PUBLIC_PANDOC_EXPORT_ENDPOINT || 'http://127.0.0.1:3080'
const canonicalBaseUrl = config.get('export.canonicalBaseUrl')
const exportEndpoint = config.get('export.urlEndpoint')

const exportZip = async ({ bib, yaml, md, id, versionId, title }, res, _) => {
const filename = `${normalize(title)}.zip`
Expand Down
5 changes: 3 additions & 2 deletions export/src/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ function getGraphQLClient () {
if (graphQLClient) {
return graphQLClient
}
const graphqlEndpoint = process.env.SNOWPACK_PUBLIC_GRAPHQL_ENDPOINT || 'http://localhost:3030/graphql'
const passthroughToken = process.env.SE_GRAPHQL_TOKEN
const graphqlEndpoint = config.get('api.urlEndpoint')
const passthroughToken = config.get('api.passthroughToken')

graphQLClient = new GraphQLClient(graphqlEndpoint, {
headers: {
authorization: `Bearer ${passthroughToken}`,
Expand Down
Loading

0 comments on commit b541045

Please sign in to comment.