diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..31d1a00 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM node:4.6 +WORKDIR /app +ADD . /app +RUN npm install +EXPOSE 3000 +CMD npm start diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 0000000..586b21d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,20 @@ +web: + build: . + command: node index-db.js + ports: + - "3000:3000" + links: + - db + environment: + MYSQL_DATABASE: myapp + MYSQL_USER: myapp + MYSQL_PASSWORD: mysecurepass + MYSQL_HOST: db +db: + image: orchardup/mysql + ports: + - "3306:3306" + environment: + MYSQL_DATABASE: myapp + MYSQL_USER: myapp + MYSQL_PASSWORD: mysecurepass diff --git a/docker.sh b/docker.sh index d2365d7..56f1286 100644 --- a/docker.sh +++ b/docker.sh @@ -21,3 +21,5 @@ curl -L "https://github.com/docker/compose/releases/download/1.21.2/docker-compo # install docker-machine curl -L "https://github.com/docker/machine/releases/download/v0.15.0/docker-machine-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-machine && \ chmod +x /usr/local/bin/docker-machine + +usermod -G docker vagrant diff --git a/index-db.js b/index-db.js new file mode 100755 index 0000000..d40a13f --- /dev/null +++ b/index-db.js @@ -0,0 +1,35 @@ +var express = require('express'); +var app = express(); +var mysql = require("mysql"); +var con = mysql.createConnection({ host: process.env.MYSQL_HOST, user: process.env.MYSQL_USER, password: process.env.MYSQL_PASSWORD, database: process.env.MYSQL_DATABASE}); + +// mysql code + +con.connect(function(err){ + if(err){ + console.log('Error connecting to db: ', err); + return; + } + console.log('Connection to db established'); + con.query('CREATE TABLE IF NOT EXISTS visits (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, ts BIGINT)',function(err) { + if(err) throw err; + }); +}); + +// Request handling +app.get('/', function (req, res) { + // create table if not exist + con.query('INSERT INTO visits (ts) values (?)', Date.now(),function(err, dbRes) { + if(err) throw err; + res.send('Hello World! You are visitor number '+dbRes.insertId); + }); +}); + + +// server +var server = app.listen(3000, function () { + var host = server.address().address; + var port = server.address().port; + + console.log('Example app listening at http://%s:%s', host, port); +}); diff --git a/index.js b/index.js new file mode 100755 index 0000000..20abd1d --- /dev/null +++ b/index.js @@ -0,0 +1,13 @@ +var express = require('express'); +var app = express(); + +app.get('/', function (req, res) { + res.send('Hello World!'); +}); + +var server = app.listen(3000, function () { + var host = server.address().address; + var port = server.address().port; + + console.log('Example app listening at http://%s:%s', host, port); +}); diff --git a/misc/Jenkinsfile b/misc/Jenkinsfile new file mode 100755 index 0000000..a0a4a82 --- /dev/null +++ b/misc/Jenkinsfile @@ -0,0 +1,19 @@ +node { + def commit_id + stage('Preparation') { + checkout scm + sh "git rev-parse --short HEAD > .git/commit-id" + commit_id = readFile('.git/commit-id').trim() + } + stage('test') { + nodejs(nodeJSInstallationName: 'nodejs') { + sh 'npm install --only=dev' + sh 'npm test' + } + } + stage('docker build/push') { + docker.withRegistry('https://index.docker.io/v1/', 'dockerhub') { + def app = docker.build("wardviaene/docker-nodejs-demo:${commit_id}", '.').push() + } + } +} diff --git a/misc/Jenkinsfile.v2 b/misc/Jenkinsfile.v2 new file mode 100755 index 0000000..c468709 --- /dev/null +++ b/misc/Jenkinsfile.v2 @@ -0,0 +1,31 @@ +node { + def commit_id + stage('Preparation') { + checkout scm + sh "git rev-parse --short HEAD > .git/commit-id" + commit_id = readFile('.git/commit-id').trim() + } + stage('test') { + def myTestContainer = docker.image('node:4.6') + myTestContainer.pull() + myTestContainer.inside { + sh 'npm install --only=dev' + sh 'npm test' + } + } + stage('test with a DB') { + def mysql = docker.image('mysql').run("-e MYSQL_ALLOW_EMPTY_PASSWORD=yes") + def myTestContainer = docker.image('node:4.6') + myTestContainer.pull() + myTestContainer.inside("--link ${mysql.id}:mysql") { // using linking, mysql will be available at host: mysql, port: 3306 + sh 'npm install --only=dev' + sh 'npm test' + } + mysql.stop() + } + stage('docker build/push') { + docker.withRegistry('https://index.docker.io/v1/', 'dockerhub') { + def app = docker.build("wardviaene/docker-nodejs-demo:${commit_id}", '.').push() + } + } +} diff --git a/misc/Vagrantfile b/misc/Vagrantfile new file mode 100755 index 0000000..f7ceb7a --- /dev/null +++ b/misc/Vagrantfile @@ -0,0 +1,7 @@ +Vagrant.configure(2) do |config| + config.vm.define "docker" do |docker| + docker.vm.box = "ubuntu/trusty64" + docker.vm.network "private_network", ip: "192.168.0.249" + docker.vm.hostname = "docker.example.com" + end +end diff --git a/package.json b/package.json new file mode 100755 index 0000000..0150f3d --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "myapp", + "version": "0.0.1", + "private": true, + "scripts": { + "start": "node index.js", + "test": "mocha" + }, + "engines": { + "node": "^4.6.1" + }, + "dependencies": { + "express": "^4.14.0", + "mysql": "^2.10.2" + }, + "devDependencies": { + "mocha": "^3.4.2" + } +} diff --git a/test/test.js b/test/test.js new file mode 100755 index 0000000..9592531 --- /dev/null +++ b/test/test.js @@ -0,0 +1,8 @@ +var assert = require('assert'); +describe('Array', function() { + describe('#indexOf()', function() { + it('should return -1 when the value is not present', function() { + assert.equal(-1, [1,2,3].indexOf(4)); + }); + }); +});