Skip to content

Commit 62c6314

Browse files
authored
Merge pull request #18 from Human-Connection/17-improve-docker-setup
Improve Docker Setup
2 parents 9bad2bb + 6db1cc7 commit 62c6314

10 files changed

Lines changed: 317 additions & 25 deletions

File tree

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ WORKDIR /home/node/coc-api
99
# Copy contents of local folder to `WORKDIR`
1010
COPY . .
1111

12-
# Install nodemon globally
13-
RUN npm install --global nodemon
12+
# Install npm packages globally
13+
RUN npm install --global nodemon db-migrate db-migrate-mysql wait-on
1414

1515
# Install dependencies from package.json
1616
RUN npm install --quiet --no-warnings

README.md

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To find out more about the Clock of Change and Human Connection - the network be
1111
* Express.js: We use the [Express.js](https://expressjs.com/en/) framework - a Node.js framework to help build web applications
1212
* Nodemon: We use [Nodemon](https://nodemon.io/) for development - a handy replacement wrapper for Node.js that automatically restarts the application on file changes
1313
* MySQL: We use [MySQL](https://www.mysql.com) as our relational database of choice to store our data
14-
14+
* MailHog: We use [MailHog](https://github.com/mailhog/MailHog) to debug and preview the email communication
1515

1616
## Project Structure & Components
1717

@@ -21,7 +21,9 @@ To find out more about the Clock of Change and Human Connection - the network be
2121
* core/: The core directory contains the most important files of the project like database, mailer, router and main controller
2222
* core/entryController.js: Is the main controller for all the requests
2323
* core/restapi.js: All routes can be found here, they will also be listed further down
24+
* documentation/: Documentation for the Clock of Change API
2425
* mails/: The mails directory holds the mail templates
26+
* migrations/: Migrations for the database
2527
* public/: The public directory is not used for now
2628

2729

@@ -31,25 +33,86 @@ We use MySQL for the COC API as our relational database.
3133
Currently all of the database related code can be found in the `core/db.js` file.
3234
This includes the credentials for the database (host, user, password and db name) and can be changed in this file.
3335

34-
TODO: Add description of tables
36+
MySQL DB can be configured with the following environment variables:
37+
38+
| Variable | Description |
39+
|--------------|-------------------------------|
40+
| `MYSQL_HOST` | Host address for the database |
41+
| `MYSQL_DB` | Database Name |
42+
| `MYSQL_USER` | MySQL User |
43+
| `MYSQL_PASS` | MySQL Password |
44+
45+
Currently we have two tables:
46+
* apikeys: Contains the apikeys required to perform authorized API request
47+
* entries: Stores the user entries for the Clock of Change
48+
49+
For more information about the tables, see the SQL dump of the tables at `documentation/tables.sql` .
3550

3651
**MAILER**
3752

3853
The code related to the mail system can be found in the file `core/mailer.js`.
3954
For the mailer to work the smtp credentials need to be changed in this file as well.
4055
Then the mailer will work and use the mail templates from `mails/entry/`
4156

57+
SMTP can be configured with the following environment variables:
58+
59+
| Variable | Description |
60+
|--------------|-------------------------------|
61+
| `MAIL_HOST` | Host address for the mailer |
62+
| `MAIL_PORT` | Port number for the mailer |
63+
| `MAIL_USER` | Mailer User |
64+
| `MAIL_PASS` | Mailer Password |
65+
66+
67+
68+
To debug and preview the emails, we use [MailHog](https://github.com/mailhog/MailHog).
69+
When installing the Clock of Change without Docker, you have to install MailHog manually (see link for details).
70+
Then set the host address of MailHog in the Clock of Change API and use `1025` as the port number.
71+
72+
Assuming MailHog is running on localhost or you have chosen the Docker installation, you can debug and preview the mails under [http://localhost:8025/](http://localhost:8025/).
4273

4374
## Installation
4475

4576
**PREREQUESITES**
4677

47-
Before starting the installation you need to make sure you have the following tools installed:
48-
* Git: You need to have Git installed. You can check this in the console with `git --help`. For installation instructions visit https://git-scm.com/
49-
* Node.js: You need to have Node.js installed. You can check this in the console with `node -v`. For installation instructions visit https://nodejs.org/en/
50-
* Npm: You need to have npm installed. You can check this in the console with `npm -v`. For installation instructions head to https://www.npmjs.com/get-npm
78+
Before starting the installation you need to make sure you have a recent version of [Git](https://git-scm.com/), [Nodejs](https://nodejs.org/en/) and [Npm](https://www.npmjs.com/get-npm) installed.
79+
E.g. we have the following versions:
80+
```
81+
$ git --version
82+
git version 2.14.2.windows.1
83+
$ node --version
84+
v10.15.0
85+
$ npm --version
86+
4.6.0
87+
88+
Git: 2.14.2
89+
Node: 10.15.0
90+
Npm: 4.6.0
91+
OS: Windows 10
92+
```
5193

52-
**INSTALLATION**
94+
**DOCKER INSTALLATION**
95+
96+
The Clock of Change API server comes bundled as a Docker Container, which enables you to run then server out of the box.
97+
98+
Of course you need to have a recent version of [Docker](https://www.docker.com/get-started) installed. If you don't have Docker, follow the instructions of the link.
99+
You can check the version like this:
100+
```
101+
$ docker -v
102+
Docker version 18.09.1, build 4c52b90
103+
```
104+
105+
To run the Docker version, follow these steps:
106+
1. First you need to clone the git repository of the Clock of Change API. Head to a directory where you want the git repository to reside
107+
and open the directory in the console. Then run `git clone https://github.com/Human-Connection/Clock-of-Change-API.git` to clone the repository to this directory.
108+
2. Go to the newly created Clock-of-Change-API directory (`cd Clock-of-Change-API` in the console)
109+
3. Run `docker-compose up`. This will build the Docker container on first startup and run it. This can take a while, but after some time you should see the Clock of Change ticking.
110+
111+
Now the Clock of Change API server is ready for usage at [http://127.0.0.1:1337](http://127.0.0.1:1337)
112+
113+
**LOCAL INSTALLATION & USAGE**
114+
115+
If you do not want to use the docker version, you can also install the Clock of Change API server locally.
53116

54117
1. First you need to clone the git repository of the Clock of Change API. Head to a directory where you want the git repository to reside
55118
and open the directory in the console. Then run `git clone https://github.com/Human-Connection/Clock-of-Change-API.git` to clone the repository to this directory.
@@ -65,13 +128,31 @@ Now the Clock of Change API server is ready to tick.
65128

66129
**START THE SERVER**
67130

131+
This section only applies if you have chosen the local installation.
132+
When installing the Clock of Change API server with Docker, the server is starting automatically.
133+
68134
In the base Clock-of-Change-API directory run
69135

70136
`npm run start`
71137

72138
in the console to start the Clock of Change API server.
73139
This will start Nodemon and the Node.js server, which will start listening for and processing requests at [http://localhost:1337](http://localhost:1337).
74140

141+
**RUN DATABASE MIGRATIONS**
142+
143+
This section only applies if you have chosen the local installation.
144+
When installing the Clock of Change API server with Docker, the database migrations are applied automatically.
145+
146+
To create the necessary tables by applying the database migrations, run `db-migrate up` in the console.
147+
148+
This should give you an output like this:
149+
```
150+
$ db-migrate up
151+
[INFO] Processed migration 20190206134449-entries
152+
[INFO] Processed migration 20190206140226-apikeys
153+
[INFO] Done
154+
```
155+
75156
**MAKE REQUESTS**
76157

77158
Pro Tip: Get [Postman](https://www.getpostman.com/) to make requests. This amazing tool makes working with APIs way easier.

core/db.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let pool = mysql.createPool({
88
host : process.env.MYSQL_HOST || 'localhost',
99
user : process.env.MYSQL_USER || '',
1010
password : process.env.MYSQL_PASS || '',
11-
database : process.env.MYSQL_DB || '',
11+
database : process.env.MYSQL_DB || 'clock_of_change',
1212
//socketPath : '/var/run/mysqld/mysqld.sock',
1313
connectionLimit : 30,
1414
supportBigNumbers : true
@@ -40,7 +40,7 @@ exports.isValidApiKey = function(secret, callback){
4040
pool.getConnection(function(err, connection) {
4141
if(err) { console.log(err); callback(true); return; }
4242

43-
let sql = "SELECT valid from apikeys WHERE secret = '?';";
43+
let sql = "SELECT * from apikeys WHERE secret = ?;";
4444

4545
// make the query
4646
connection.query(sql, [secret], function(err, results) {
@@ -74,7 +74,7 @@ exports.getUserByHash = function(hash, callback){
7474
pool.getConnection(function(err, connection) {
7575
if (err) { console.log(err); callback(true); return; }
7676

77-
let sql = "SELECT email, firstname from entries WHERE confirm_key = '?';";
77+
let sql = "SELECT email, firstname from entries WHERE confirm_key = ?;";
7878

7979
// make the query
8080
connection.query(sql, [hash], function(err, results) {
@@ -121,7 +121,7 @@ exports.saveEntry = function(fields, callback){
121121
if(err) { console.log(err); callback(true); return; }
122122
let data = prepareEntry(fields);
123123

124-
let sqlEmailExists = "SELECT count(*) as cnt FROM entries WHERE email = '?';";
124+
let sqlEmailExists = "SELECT count(*) as cnt FROM entries WHERE email = ?;";
125125
connection.query(sqlEmailExists, [data.email], function(err, results) {
126126
if(!err) {
127127
if(results[0]['cnt'] > 0){
@@ -130,7 +130,7 @@ exports.saveEntry = function(fields, callback){
130130
}else{
131131
let sql = "INSERT INTO entries (firstname, lastname, email, country, message, anon, ipv4, image, "
132132
+ "created_at, updated_at, confirm_key, beta, newsletter, pax) "
133-
+ "VALUES ('?', '?', '?', '?', '?', ?, '?', '?', ?, ?, '?', ?, ?, ?);";
133+
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
134134

135135
// run the query
136136
connection.query(
@@ -153,6 +153,7 @@ exports.saveEntry = function(fields, callback){
153153
],
154154
function(err, results) {
155155
connection.release();
156+
console.log('this.sql', this.sql); //command/query
156157
if(err) { callback(true); return; }
157158
callback(false, results);
158159
}

core/mailer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ let nodeMailer = require('nodemailer'),
88
function initTransporter(){
99
if(transporter === undefined){
1010
transporter = nodeMailer.createTransport({
11-
host: '',
12-
port: 25,
11+
host: process.env.MAIL_HOST || 'localhost',
12+
port: process.env.MAIL_PORT || 25,
1313
secure: false,
1414
auth: {
15-
user: '',
16-
pass: ''
15+
user: process.env.MAIL_USER || '',
16+
pass: process.env.MAIL_PASS || ''
1717
}
1818
});
1919
}
@@ -65,4 +65,4 @@ exports.sendVerificationMail = function(key, recipient, callback = null){
6565
});
6666
}
6767
});
68-
};
68+
};

database.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dev": {
3+
"driver": "mysql",
4+
"host": {"ENV": "MYSQL_HOST"},
5+
"port": "3306",
6+
"user": {"ENV": "MYSQL_USER"},
7+
"password": {"ENV": "MYSQL_PASS"},
8+
"database": {"ENV": "MYSQL_DB"}
9+
}
10+
}

docker-compose.yml

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ services:
55
image: mysql:5
66
environment:
77
MYSQL_ROOT_PASSWORD: 123456
8-
MYSQL_DATABASE: db1
8+
MYSQL_DATABASE: clock_of_change
9+
ports:
10+
- 3306:3306
11+
12+
mailhog:
13+
container_name: mailhog
14+
image: mailhog/mailhog
15+
ports:
16+
- 1025:1025
17+
- 8025:8025
918

1019
coc-api:
1120
image: clock-of-change-api
@@ -17,10 +26,15 @@ services:
1726
ports:
1827
- 1337:1337
1928
environment:
20-
- DB_HOST=db:3306
21-
- DB_NAME=${MYSQL_DATABASE}
22-
- DB_USER=root
23-
- DB_PASSWORD=${MYSQL_ROOT_PASSWORD}
29+
- MYSQL_HOST=db
30+
- MYSQL_DB=clock_of_change
31+
- MYSQL_USER=root
32+
- MYSQL_PASS=123456
33+
- MAIL_HOST=mailhog
34+
- MAIL_PORT=1025
35+
- MAIL_USER=mailer
36+
- MAIL_PASS=123456
2437
depends_on:
2538
- db
26-
command: "npm run start"
39+
- mailhog
40+
command: bash -c "wait-on tcp:db:3306 && db-migrate up && npm run start"

documentation/tables.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
CREATE TABLE IF NOT EXISTS `apikeys` (
2+
`secret` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
3+
`valid` int(1) DEFAULT NULL,
4+
UNIQUE KEY `secret` (`secret`)
5+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
6+
7+
CREATE TABLE IF NOT EXISTS `entries` (
8+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
9+
`firstname` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
10+
`lastname` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
11+
`email` varchar(320) COLLATE utf8mb4_unicode_ci NOT NULL,
12+
`country` char(2) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
13+
`message` text COLLATE utf8mb4_unicode_ci NOT NULL,
14+
`anon` int(1) DEFAULT '0',
15+
`ipv4` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
16+
`image` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
17+
`created_at` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
18+
`updated_at` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
19+
`confirm_key` varchar(75) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
20+
`beta` int(1) DEFAULT '0',
21+
`newsletter` int(1) DEFAULT '0',
22+
`pax` int(1) DEFAULT '0',
23+
`email_confirmed` int(1) DEFAULT '0',
24+
`status` int(1) DEFAULT '0',
25+
PRIMARY KEY (`id`)
26+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

0 commit comments

Comments
 (0)