diff --git a/ansible/deploy.yml b/ansible/deploy.yml index 3f7ef97..dd526b0 100644 --- a/ansible/deploy.yml +++ b/ansible/deploy.yml @@ -12,7 +12,7 @@ force_source: true - name: (re)start a container - docker_container: + community.docker.docker_container: name: my_website image: dominikmatic/my_website:latest state: started @@ -20,6 +20,5 @@ ports: - "80:3000" - "443:3000" - mounts: - - source: /etc/letsencrypt/ - target: /etc/letsencrypt/ \ No newline at end of file + volumes: + - "/etc/letsencrypt/:/etc/letsencrypt/" \ No newline at end of file diff --git a/app/app.js b/app/app.js index f5fd9ea..75cbdea 100644 --- a/app/app.js +++ b/app/app.js @@ -1,11 +1,19 @@ const express = require('express') const app = express() +const fs = require('fs') +const https = require('https') const port = 3000 app.set('view engine', 'ejs') app.use(express.static(__dirname + '/public')); +app.use((req, res, next) => { + if(req.protocol === 'http') { + return res.redirect(301, `https://${req.headers.host}${req.url}`); + } + next(); +}) app.get('/', (req, res) => { res.render('index.ejs') @@ -19,6 +27,10 @@ app.get('/mylinks', (req, res) => { res.render('mylinks.ejs') }) -app.listen(port, () => { +https.createServer({ + key: fs.readFileSync('/etc/letsencrypt/private.key.pem'), + cert: fs.readFileSync('/etc/letsencrypt/domain.cert.pem'), + ca: fs.readFileSync('/etc/letsencrypt/intermediate.cert.pem') +}).listen(port, () => { console.log(`Server listening on port ${port}`) })