Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fee37e3

Browse files
committed
Update the script launching the app inside a Docker container
1 parent 8ba3b2a commit fee37e3

File tree

11 files changed

+204
-122
lines changed

11 files changed

+204
-122
lines changed

README.md

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
# Node.js API Starter Kit &nbsp; <a href="https://github.com/kriasoft/nodejs-api-starter/stargazers"><img src="https://img.shields.io/github/stars/kriasoft/nodejs-api-starter.svg?style=social&label=Star&maxAge=3600" height="20"></a> <a href="https://twitter.com/ReactStarter"><img src="https://img.shields.io/twitter/follow/ReactStarter.svg?style=social&label=Follow&maxAge=3600" height="20"></a>
1+
# Node.js API Starter Kit &nbsp; <a href="https://gitter.im/kriasoft/nodejs-api-starter"><img src="https://img.shields.io/gitter/room/kriasoft/nodejs-api-starter.js.svg" width="102" height="20"></a> <a href="https://github.com/kriasoft/nodejs-api-starter/stargazers"><img src="https://img.shields.io/github/stars/kriasoft/nodejs-api-starter.svg?style=social&label=Star&maxAge=3600" height="20"></a> <a href="https://twitter.com/ReactStarter"><img src="https://img.shields.io/twitter/follow/ReactStarter.svg?style=social&label=Follow&maxAge=3600" height="20"></a>
22

3-
[Node.js API Starter Kit][nodejskit] is a boilerplate and tooling for authoring
4-
**data API** backends with [Node.js][node] 7+, [JavaScript][js] (via
5-
[Babel][babel]) and [GraphQL][gql]. It's meant to be paired with a web and/or
6-
mobile application project such as [React Starter Kit][rsk].
3+
[Node.js API Starter Kit][nodejskit] is a boilerplate and tooling for authoring **data API**
4+
backends with [Node.js][node], [JavaScript][js] (via [Babel][babel]) and [GraphQL][gql]. It's
5+
meant to be paired with a web and/or mobile application project such as [React Starter Kit][rsk].
76

87

9-
## Prerequisites
8+
## Features
109

11-
* macOS, Windows or Linux
12-
* [Docker][docker] v1.12.5 or newer
13-
* Text editor or IDE (e.g. [VS Code][code], [WebStorm][wstorm] etc.)
10+
✓ Cross-platform, develope on macOS, Windows or Linux inside a [Docker][docker] container<br>
11+
✓ No development dependencies except for [Docker][docker] v1.12.5 or neweer<br>
12+
✓ Authentication and authorization via [Passport.js][passport] (see [`src/passport`](./src/passport))<br>
13+
✓ Session and cache management with [Redis][redis] (see [stop using JWT for sessions](http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/))<br>
14+
[PostgreSQL][pg] database schema boilerplate and migration tools (see [`scripts`](./scripts), [`migrations`](./migrations))<br>
15+
[GraphQL][gql] boilerplate, everything needed to get started building a [GraphQL][gql] API endpoint<br>
16+
✓ The exact same process is used to build the app for production and build for running/testing locally<br>
1417

1518

1619
## Directory Layout
@@ -19,22 +22,21 @@ mobile application project such as [React Starter Kit][rsk].
1922
.
2023
├── /build/ # The compiled output (via Babel)
2124
├── /migrations/ # Database schema migrations
22-
├── /node_modules/ # Project dependencies (npm modules)
2325
├── /scripts/ # Build automation scripts
2426
├── /src/ # Node.js application source files
25-
│ ├── /db/ # Database access methods and connection pooling
27+
│ ├── /db/ # Database access and connection pooling
2628
│ ├── /passport/ # Passport.js authentication strategies
2729
│ ├── /routes/ # Express routes, e.g. /login/facebook
28-
│ ├── /types/ # GraphQL types /w resolve functions
30+
│ ├── /types/ # GraphQL types with resolve functions
2931
│ │ ├── /User.js # User account (id, email, etc.)
3032
│ │ ├── /Viewer.js # The top-level GraphQL object type
3133
│ │ └── /... # etc.
32-
│ ├── /app.js # Express application
34+
│ ├── /app.js # Express.js application
3335
│ ├── /schema.js # GraphQL schema
3436
│ └── /server.js # Node.js server (entry point)
3537
├── /test/ # Unit, integration and load tests
3638
├── .env # Application settings for the dev environment
37-
├── .env.example # Available application settings for a reference
39+
├── .env.example # Available application settings as a reference
3840
├── docker-compose.yml # Defines Docker services, networks and volumes
3941
├── Dockerfile # Commands for building a Docker image for production
4042
├── package.json # The list of project dependencies
@@ -44,7 +46,9 @@ mobile application project such as [React Starter Kit][rsk].
4446

4547
## Getting Started
4648

47-
Just clone the repo and launch the app with [Docker Compose][compose]:
49+
Make sure that you have [Docker][docker] v1.12.5 or newer installed plus a good text editor or IDE
50+
([VS Code][code], [WebStorm][wstorm] or another), clone the repo and launch the app with [Docker
51+
Compose][compose]:
4852

4953
```bash
5054
git clone -o nodejs-api-starter -b master --single-branch \
@@ -57,27 +61,20 @@ docker-compose up # Launch Docker containers with the Node.js API
5761
The API server must become available at [http://localhost:5000/](http://localhost:5000/)
5862
([live demo][demo]).
5963

60-
In order to open a new terminal session from inside the Docker container run:
64+
Once the docker container named `api` is started, the Docker engine executes `node scripts/run.js`
65+
command that installs Node.js dependencies, migrates database schema to the latest version,
66+
compiles Node.js app from source files (see [`src`](./src)) and launches it with "live reload"
67+
on port `5000` (see [`.env`](.env.example)).
68+
69+
In order to open a new terminal session from inside the `api` Docker container run:
6170

6271
```bash
6372
docker-compose exec api /bin/sh
6473
```
6574

66-
67-
## Database
68-
69-
The following scripts can be used to transfer your existing database into another state and vise
70-
versa. Those state transitions are saved in migration files (`/migrations/*.js`), which describe
71-
the way how to get to the new state and how to revert the changes in order to get back to the old
72-
state. You can execute them inside the `api` docker container.
73-
74-
```bash
75-
yarn run db:version # Print database schema version
76-
yarn run db:migrate # Migrate database schema to the latest version
77-
yarn run db:migrate:undo # Rollback the latest migration
78-
yarn run db:migration <name> # Create a new migration from the template (see /migrations folder)
79-
yarn run db:seed # Import reference data
80-
```
75+
From this shell you can run automation scripts such as `yarn test`, `yarn run db:migrate` etc.
76+
Find the full list of scripts available inside the [`scripts`](./scripts) folder and
77+
the [`package.json`](./package.json) file.
8178

8279

8380
## Testing
@@ -92,22 +89,19 @@ yarn run test:watch # Run unit tests in watch mode
9289

9390
## Debugging
9491

95-
In order to run the app with [V8 inspector][v8debug] enabled, simply set
96-
`NODE_DEBUG=true` flag in the `.env` file, restart the app (`docker-compose up`)
97-
and [attach your debugger][vsdebug] to `127.0.0.1:9229` (see
98-
[`.vscode/launch.json`](https://gist.github.com/koistya/421ea3e0139225b27f909e98202a34de)
99-
for [VS Code][code]).
92+
In order to run the app with [V8 inspector][v8debug] enabled, simply set `NODE_DEBUG=true` flag in
93+
the [`.env`](.env.example) file, restart the app (`docker-compose up`) and [attach your
94+
debugger][vsdebug] to `127.0.0.1:9229` (see [`.vscode/launch.json`](https://gist.github.com/koistya/421ea3e0139225b27f909e98202a34de)
95+
for [VS Code][code] as an example).
10096

10197

10298
## Deployment
10399

104-
Customize the deployment script found in `scripts/publish.js`. Set this script
105-
executable by running `chmod +x scripts/publish.sh` on macOS/Linux or convert
106-
it to `publish.cmd` on Windows. Then whenever you need to deploy your app to a
107-
remote server simply run:
100+
Customize the deployment script found in `scripts/publish.js` on macOS/Linux or convert it to
101+
`publish.cmd` on Windows. Then whenever you need to deploy your app to a remote server simply run:
108102

109103
```bash
110-
scripts/publish.sh
104+
/bin/sh scripts/publish.sh # or, `scripts/publish.cmd` on Windows
111105
```
112106

113107
Not sure where to deploy your app? [DigitalOcean][do] is a great choice in many cases (get [$10 credit][do])
@@ -139,8 +133,8 @@ However, if you decide to get involved, please take a moment to review the [guid
139133

140134
## Support
141135

142-
* [#react-starter-kit](http://stackoverflow.com/questions/tagged/react-starter-kit) on Stack Overflow — Questions and answers
143-
* [#react-starter-kit](https://gitter.im/kriasoft/react-starter-kit) on Gitter — Watch announcements, share ideas and feedback
136+
* [#nodejs-api-starter](http://stackoverflow.com/questions/tagged/nodejs-api-starter) on Stack Overflow — Questions and answers
137+
* [#nodejs-api-starter](https://gitter.im/kriasoft/nodejs-api-starter) on Gitter — Watch announcements, share ideas and feedback
144138
* [GitHub Issues](https://github.com/kriasoft/nodejs-api-starter/issues) — Check open issues, send feature requests
145139
* [@koistya](https://twitter.com/koistya) on [Codementor](https://www.codementor.io/koistya) or [HackHands](https://hackhands.com/koistya/) — Private consulting
146140

@@ -154,7 +148,7 @@ file. The documentation to the project is licensed under the
154148

155149

156150
---
157-
Made with ♥ by Konstantin Tarkus ([@koistya](https://twitter.com/koistya)) and [contributors](https://github.com/kriasoft/nodejs-api-starter/graphs/contributors)
151+
Made with ♥ by Konstantin Tarkus ([@koistya](https://twitter.com/koistya), [blog](https://medium.com/@tarkus)) and [contributors](https://github.com/kriasoft/nodejs-api-starter/graphs/contributors)
158152

159153

160154
[nodejskit]: https://github.com/kriasoft/nodejs-api-starter
@@ -164,7 +158,7 @@ Made with ♥ by Konstantin Tarkus ([@koistya](https://twitter.com/koistya)) and
164158
[babel]: http://babeljs.io/
165159
[gql]: http://graphql.org/
166160
[yarn]: https://yarnpkg.com
167-
[demo]: https://www.reactstarterkit.com/graphql
161+
[demo]: https://reactstarter.com/graphql
168162
[pg]: https://www.postgresql.org/
169163
[do]: https://m.do.co/c/eef302dbae9f
170164
[code]: https://code.visualstudio.com/
@@ -173,4 +167,6 @@ Made with ♥ by Konstantin Tarkus ([@koistya](https://twitter.com/koistya)) and
173167
[compose]: https://docs.docker.com/compose/
174168
[v8debug]: https://chromedevtools.github.io/debugger-protocol-viewer/v8/
175169
[vsdebug]: https://code.visualstudio.com/Docs/editor/debugging
170+
[passport]: http://passportjs.org/
171+
[redis]: https://redis.io/
176172

docker-compose.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ services:
1313
- postgres
1414
- redis
1515
volumes:
16-
- yarn:/root/.cache/yarn
17-
- npm:/root/.npm
16+
- api-yarn-cache:/root/.cache/yarn
17+
- api-npm-cache:/root/.npm
1818
- ./:/usr/src/app
1919
working_dir: /usr/src/app
20-
entrypoint: /bin/sh scripts/entrypoints/api.sh
20+
command: node scripts/run.js
2121

2222
postgres:
2323
image: postgres:9.6.1-alpine
@@ -27,18 +27,18 @@ services:
2727
- /run/postgresql
2828
- /tmp
2929
volumes:
30-
- postgres:/var/lib/postgresql/data
31-
- ./scripts/entrypoints/postgres.sh:/docker-entrypoint-initdb.d/init.sh
30+
- postgres-data:/var/lib/postgresql/data
31+
- ./scripts/postgres-init.sh:/docker-entrypoint-initdb.d/init.sh
3232

3333
redis:
3434
image: redis:3.2.6-alpine
3535
restart: always
3636
read_only: true
3737
volumes:
38-
- redis:/data
38+
- redis-data:/data
3939

4040
volumes:
41-
postgres:
42-
redis:
43-
yarn:
44-
npm:
41+
api-yarn-cache:
42+
api-npm-cache:
43+
postgres-data:
44+
redis-data:

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@
5757
"test:watch": "mocha test/unit --compilers js:babel-register --reporter min --watch",
5858
"build": "node scripts/build.js",
5959
"build:watch": "node scripts/build.js --watch",
60-
"db:create": "node --harmony scripts/db.js create",
61-
"db:drop": "node --harmony scripts/db.js drop",
6260
"db:version": "node --harmony scripts/db.js version",
6361
"db:migrate": "node --harmony scripts/db.js migrate",
6462
"db:migrate:undo": "node --harmony scripts/db.js migrate:undo",
6563
"db:migration": "node --harmony scripts/db.js migration",
6664
"db:seed": "node --harmony scripts/db.js seed",
67-
"deploy": "node scripts/deploy.js",
68-
"start": "node scripts/start.js"
65+
"deploy": "/bin/sh scripts/deploy.sh",
66+
"start": "node scripts/run.js"
6967
}
7068
}

scripts/.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"rules": {
3+
"global-require": "off",
34
"no-console": "off",
45
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }]
56
}

scripts/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Automation Scripts
2+
3+
The `*.js` scripts in this folder are intended to be used from inside the running `api` Docker
4+
container. First, make sure that all the required Docker containers are running:
5+
6+
```bash
7+
docker-compose up # Launch Docker containers, press CTR+Z (transfer to background)
8+
```
9+
10+
Then open a new terminal session from inside the `api` container by running:
11+
12+
```bash
13+
docker-compose exec api /bin/sh
14+
```
15+
16+
From this shell you must be able to execute `scripts/*.js` files directly or by using
17+
[`yarn run`][yarnrun]. Alternatively, execute any command directly on a running container with:
18+
19+
```bash
20+
docker-compose exec api <command>
21+
```
22+
23+
New to Docker? Please, skim through the [Docker in Action](http://amzn.to/2hmUrNP) book that
24+
describes all the main concepts.
25+
26+
27+
### [`db.js`](./db.js) — database management
28+
29+
The following commands can be used to transfer your existing database into another state and vise
30+
versa. Those state transitions are saved in migration files ([`migrations/*.js`](../migrations)),
31+
which describe the way how to get to the new state and how to revert the changes in order to get
32+
back to the old state.
33+
34+
```bash
35+
node --harmony scripts/db version # Print database schema version
36+
node --harmony scripts/db migrate # Migrate database schema to the latest version
37+
node --harmony scripts/db migrate:undo # Rollback the latest migration
38+
node --harmony scripts/db migration # Create a new migration from the template
39+
node --harmony scripts/db seed # Import reference data
40+
```
41+
42+
For more information on how use migrations reffer to [Knex documentation][knex].
43+
44+
45+
### [`build.js`](./build.js) — compilation
46+
47+
```bash
48+
node scripts/build # Compile the app into the ./build folder
49+
```
50+
51+
52+
### [`run.js`](./run.js) — launching for testing/debugging
53+
54+
```bash
55+
node scripts/run # Compile the app and launch Node.js server with "live reload"
56+
```
57+
58+
This script will also execute `yarn install` in case some of the Node.js dependencies are missing.
59+
60+
61+
### [`publish.sh`](./publish.sh) — deployment
62+
63+
```bash
64+
/bin/sh scripts/publish.sh # Compile the app, build a Docker image and deploy it
65+
```
66+
67+
68+
[yarnrun]: https://yarnpkg.com/en/docs/cli/run
69+
[knex]: http://knexjs.org/

scripts/db.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const fs = require('fs');
1111
const knex = require('knex');
1212
const task = require('./task');
1313

14-
const command = process.argv[2];
14+
// The list of available commands, e.g. node scripts/db.js migrate:undo
1515
const commands = ['version', 'migrate', 'migrate:undo', 'migration', 'seed'];
16+
const command = process.argv[2];
1617

1718
const config = {
1819
client: 'pg',
@@ -22,6 +23,7 @@ const config = {
2223
},
2324
};
2425

26+
// The template for database migration files (see templates/*.js)
2527
const version = new Date().toISOString().substr(0, 16).replace(/\D/g, '');
2628
const template = `module.exports.up = async (db) => {\n \n};\n
2729
module.exports.down = async (db) => {\n \n};\n

scripts/entrypoints/api.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

scripts/entrypoints/postgres.sh renamed to scripts/postgres-init.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#!/bin/bash
2-
set -e
1+
#!/bin/sh -e
32

43
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
54
CREATE DATABASE prod;

0 commit comments

Comments
 (0)