diff --git a/.dockerignore b/.dockerignore index 5171c54083337f0b87926da2e8f52abefe19d70f..b512c09d476623ff4bf8d0d63c29b784925dbdf8 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1 @@ -node_modules -npm-debug.log \ No newline at end of file +node_modules \ No newline at end of file diff --git a/.env-example b/.env-example index 8a22a9fc534d8cdb97603391ec0be74917b4591b..8b2e783080bea405c4570b3b8405f8aaba1873f5 100644 --- a/.env-example +++ b/.env-example @@ -1,6 +1,7 @@ # Set your variables and rename to .env NODE_ENV=development +NODE_PORT=4000 PGHOST=localhost PGPORT=5432 diff --git a/Dockerfile b/Dockerfile index 069f686d68700e4222e9bed45ea6c3214ada75f8..cb37be9801e60038dc0f93925955d235446bb906 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,7 @@ LABEL maintainer=francois.romain@beta.gouv.fr ENV dir /api ENV NODE_ENV production +ENV NODE_PORT 4000 WORKDIR $dir # cache node_modules if no changes to package.json diff --git a/README.md b/README.md index a46b4075818b0c1a6016702781b24a0a8e8faf58..2ef7d5ff4e6f60b13a9ba11decd1301410a57071 100644 --- a/README.md +++ b/README.md @@ -9,24 +9,56 @@ Plus d'infos sur le projet [ici](http://camino.beta.gouv.fr/). ## Npm scripts ```bash -# Install dependencies. +# Installe les dépendances npm install -# Start server with nodemon +# Démarre le serveur avec nodemon npm run dev -# Start server with node +# Démarre le serveur avec node npm start ``` --- -## docker compose +## Docker + +### Pour du dévelopement local + +```bash +# Démarre l'application et la base de donnée dans des conteneurs Docker +# en mode `development` +# écoute sur http://localhost:4000 +docker-compose -f ./docker-compose.local.yml up --build +``` + +### Pour du tester l'application en local dans un environement de production + +requiert: + +* une installation locale et active de https://github.com/jwilder/nginx-proxy +* un certificat ssl auto-signé + +[instructions](https://medium.com/@francoisromain/set-a-local-web-development-environment-with-custom-urls-and-https-3fbe91d2eaf0) ```bash +# Démarre l'application dans un container Docker +# en mode `production` +# écoute sur https://api.camino.local docker-compose -f ./docker-compose.local.yml up --build ``` +### Version de production + +[instructions](https://medium.com/@francoisromain/host-multiple-websites-with-https-inside-docker-containers-on-a-single-server-18467484ab95) + +```bash +# Démarre l'application dans un container Docker +# en mode `production` +# écoute sur http://api.camino.pw +docker-compose up -d --build +``` + --- ## postgres diff --git a/conf/index.js b/conf/index.js index 010222c2272675e56dc516ac169c5de603976202..0b3d16bf3753094343c246ebb15fbd5a1380b9b0 100644 --- a/conf/index.js +++ b/conf/index.js @@ -4,15 +4,12 @@ const virtualHost = process.env.VIRTUAL_HOST const protocol = virtualHost ? 'https' : 'http' -const dir = '' -const port = 4000 -const host = virtualHost ? '0.0.0.0' : 'localhost' +const port = Number(process.env.NODE_PORT) const url = virtualHost - ? `${protocol}://${virtualHost}/${dir}` - : `${protocol}://${host}:${port}/${dir}` + ? `${protocol}://${virtualHost}/` + : `${protocol}://localhost:${port}/` module.exports = { port, - host, url } diff --git a/conf/knex.js b/conf/knex.js index 2019fe62bb03a5edf3e8d2ae57a6ec5171c6ddc5..d0ede036f17f9d76889b2035e93919b260fcf69d 100644 --- a/conf/knex.js +++ b/conf/knex.js @@ -1,18 +1,29 @@ require('dotenv').config({ path: '../.env' }) module.exports = { - client: 'pg', - connection: { - host: process.env.PGHOST, - port: process.env.PGPORT, - database: process.env.PGDATABASE, - user: process.env.PGUSER + development: { + client: 'pg', + connection: { + host: process.env.PGHOST, + port: process.env.PGPORT, + database: process.env.PGDATABASE, + user: process.env.PGUSER + }, + debug: true, + migrations: { + directory: '../_tools/knex/migrations' + }, + seeds: { + directory: '../_tools/knex/seeds' + } }, - debug: true, - migrations: { - directory: '../_tools/knex/migrations' - }, - seeds: { - directory: '../_tools/knex/seeds' + production: { + client: 'pg', + connection: { + host: process.env.PGHOST, + port: process.env.PGPORT, + database: process.env.PGDATABASE, + user: process.env.PGUSER + } } } diff --git a/docker-compose.localhost.yml b/docker-compose.localhost.yml index cc04b316085a506abf59634f7720811e678d0bef..b98593442be5e75119c280c669e97932d2c6e05a 100644 --- a/docker-compose.localhost.yml +++ b/docker-compose.localhost.yml @@ -11,6 +11,7 @@ services: - postgres environment: NODE_ENV: development + NODE_PORT: 4000 PGHOST: postgres PGPORT: 5432 PGDATABASE: camino @@ -21,6 +22,10 @@ services: postgres: image: postgres:alpine + environment: + PGUSER: postgres + expose: + - 5432 volumes: - data:/var/lib/postgresql/data diff --git a/index.js b/index.js index f526e8ee070018fdae336f01dd985886205d424b..9d636b4adcdf6fde292167a1fd6f6752d51bba34 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ require('./postgres') const chalk = require('chalk') const express = require('express') var graphqlHTTP = require('express-graphql') -const { port, host, url } = require('./conf') +const { port, url } = require('./conf') const schema = require('./graphql/schemas') const resolvers = require('./graphql/resolvers') @@ -18,6 +18,6 @@ app.use( }) ) -app.listen(port, host, () => { - console.log(chalk.bgWhiteBright.black.bold('Server:' + url)) +app.listen(port, () => { + console.log(chalk.bgWhiteBright.black.bold('Server: ' + url)) }) diff --git a/postgres/index.js b/postgres/index.js index 53d452b7182478cc0ed46cdb6d74a3ab59cd3dfc..7fbe385d8296cf33643e13e609261bc5302ea09c 100644 --- a/postgres/index.js +++ b/postgres/index.js @@ -1,5 +1,5 @@ const Knex = require('knex') -const knexConf = require('../conf/knex') +const knexConf = require('../conf/knex')[process.env.NODE_ENV] const { Model } = require('objection') const knex = Knex(knexConf)