Skip to content

Commit fdc3c1f

Browse files
committed
feat(.env file + flash messages)
1 parent 3ec54d8 commit fdc3c1f

File tree

12 files changed

+249
-125
lines changed

12 files changed

+249
-125
lines changed

.env

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
APP_ENV="production"
2+
3+
API_PROTOCOL="http"
4+
API_HOST="localhost"
5+
API_PORT=8905
6+
API_URL=
7+
8+
APP_PROTOCOL=http
9+
APP_PORT=3000
10+
11+
CONF_PATH="/etc/apache2/"
12+
CONF_EXTENSION=".conf"
13+
CONF_AVAILABLES="sites-available/"
14+
CONF_ENABLED="sites-enabled/"

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,20 @@ To run application, just run the following command :
1919
npm run serve:prod
2020
```
2121

22-
The server starts by default on port 3000.
22+
The server starts by default on port `3000`. To launch app with other port, or define other environment variables, you can edit `.env` file.
23+
```
24+
APP_ENV="production"
25+
26+
API_PROTOCOL="http"
27+
API_HOST="localhost"
28+
API_PORT=8905
29+
API_URL=
30+
31+
APP_PROTOCOL=http
32+
APP_PORT=3000
33+
34+
CONF_PATH="/etc/apache2/"
35+
CONF_EXTENSION=".conf"
36+
CONF_AVAILABLES="sites-available/"
37+
CONF_ENABLED="sites-enabled/"
38+
```

app/config/index.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1+
const re = /^(https?):\/\/(\S+):([0-9]+)$/
2+
const appProtocol = process.env.APP_PROTOCOL || 'http'
3+
const appPort = process.env.APP_PORT || 3000
4+
5+
let url = process.env.API_URL || null
6+
let protocol = process.env.API_PROTOCOL || 'http'
7+
let host = process.env.API_HOST || 'localhost'
8+
let port = process.env.API_PORT || 8905
9+
10+
if (url && re.test(url)) [,protocol,host,port,] = url.match(re)
11+
112
module.exports = {
213

314
api: {
4-
port: 8905
15+
protocol,
16+
host,
17+
port
518
},
619

720
app: {
8-
port: 3000
21+
port: appPort,
22+
protocol: appProtocol
23+
},
24+
25+
params: {
26+
basepath: process.env.CONF_PATH || '/etc/apache2/',
27+
availablesPath: process.env.CONF_AVAILABLES || 'sites-available/',
28+
enabledPath: process.env.CONF_ENABLED || 'sites-enabled/',
29+
extension: process.env.CONF_EXTENSION || '.conf'
930
}
1031

1132
}

app/src/VirtualHostsHandler.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@ const { userInfo } = require('os')
88
const { exec } = require('child_process')
99
const { error } = require('./Utils')
1010

11-
// const filesRegexp = '([0-9]{3,}-)(.*)(.conf)'
12-
const DEFAULT_SETTINGS = {
13-
extension: '.conf',
14-
availablesPath: 'sites-available/',
15-
enabledPath: 'sites-enabled/'
16-
}
17-
1811
class VirtualHostsHandler {
1912

20-
constructor (basepath = "/etc/apache2/", settings = {}) {
13+
constructor (settings = {}) {
2114
this._availables = null
2215
this._enabled = null
2316
this._files = {}
24-
this.basepath = basepath
2517
this.settings = {
26-
...DEFAULT_SETTINGS,
18+
...global.config.params,
2719
...settings
2820
}
2921
this._commands = {
@@ -43,8 +35,8 @@ class VirtualHostsHandler {
4335

4436
async _init () {
4537
this.paths = {
46-
available: path.join(this.basepath, this.settings.availablesPath),
47-
enabled: path.join(this.basepath, this.settings.enabledPath)
38+
available: path.join(this.settings.basepath, this.settings.availablesPath),
39+
enabled: path.join(this.settings.basepath, this.settings.enabledPath)
4840
}
4941
await this.updateList()
5042
this._enabled = (await this.enabledList()).map(item => path.basename(item))
@@ -314,7 +306,7 @@ class VirtualHostsHandler {
314306
: this._toId(datum.filename)
315307

316308
if (this._isEnabled(datum.filename))
317-
datum.enabled = path.join(this.basepath, this.settings.enabledPath, datum.filename)
309+
datum.enabled = path.join(this.settings.basepath, this.settings.enabledPath, datum.filename)
318310

319311
return datum
320312
}

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@
55
"author": "Cyprien GLEPIN <cyprien.glepin@gmail.com>",
66
"private": true,
77
"scripts": {
8-
"serve:dev": "forever -w start server.js",
9-
"serve:prod": "NODE_ENV=production node server.js",
10-
"serve:stop": "forever stop server.js",
11-
"serve:stopall": "forever stopall",
8+
"serve:dev": "APP_ENV=development forever -w start server.js",
9+
"serve:prod": "node server.js",
1210
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
13-
"start": "npm run dev",
1411
"build": "node build/build.js"
1512
},
1613
"dependencies": {
1714
"apacheconf": "0.0.5",
1815
"body-parser": "^1.18.2",
1916
"codemirror": "^5.34.0",
2017
"cors": "^2.8.4",
18+
"dotenv": "^5.0.0",
2119
"express": "^4.16.2",
2220
"fs-extra": "^5.0.0",
2321
"glob-promise": "^3.3.0",
2422
"lodash.debounce": "^4.0.8",
2523
"lodash.throttle": "^4.1.1",
24+
"lodash.uniqueid": "^4.0.1",
2625
"node-dir": "^0.1.17",
2726
"vue": "^2.5.2",
2827
"vue-router": "^3.0.1"

server.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
require('dotenv').config()
2+
3+
global.config = require('./app/config')
4+
15
const express = require('express')
26
const cors = require('cors')
37
const bodyParser = require('body-parser')
48
const { readFileSync } = require('fs')
5-
const config = require('./app/config')
69
const VirtualHostsHandler = require('./app/src/VirtualHostsHandler')
710
const { success, error } = require('./app/src/Utils')
811
const api = express()
912
const vh = new VirtualHostsHandler
10-
const env = process.env.NODE_ENV || 'development'
13+
const env = process.env.APP_ENV || 'development'
1114

1215
api.use(cors())
1316
api.use(bodyParser.json())
@@ -126,11 +129,11 @@ api.put('/api/vh/:id/disable', async (req, res) => {
126129
return res.finish(json)
127130
})
128131

129-
api.listen(config.api.port, () => console.log(`API started on http://localhost:${config.api.port}`))
132+
api.listen(config.api.port, () => console.log(`API started on ${config.api.protocol}://${config.api.host}:${config.api.port}`))
130133

131134
if (env === 'production') {
132135
const app = express()
133136
app.use(express.static(__dirname + '/dist/'));
134137
app.get('/', (req, res) => res.sendFile(__dirname + '/dist/index.html'))
135-
app.listen(config.app.port, () => console.log(`App started on http://localhost:${config.app.port}`))
138+
app.listen(config.app.port, () => console.log(`App started on ${config.app.protocol}://localhost:${config.app.port}`))
136139
}

src/App.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div id="app" class="wrapper">
3-
<Flash />
3+
<FlashContainer />
44
<div class="container">
55
<h1 class="app-title">Apache2 VHosts manager</h1>
66
<router-view/>
@@ -9,13 +9,13 @@
99
</template>
1010

1111
<script>
12-
import Flash from './components/Flash.vue'
12+
import FlashContainer from './components/FlashComponent/FlashContainer.vue'
1313
1414
export default {
1515
1616
name: 'App',
1717
18-
components: { Flash }
18+
components: { FlashContainer }
1919
2020
}
2121
</script>

src/app/VHMaganer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default class VHMaganer {
88
}
99

1010
static pathFor (path = '') {
11-
return `http://localhost:${config.app.port}/${path}`
11+
return `${config.api.protocol}://${config.api.host}:${config.api.port}/${path}`
1212
}
1313

1414
static async all () {

src/components/Flash.vue

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

0 commit comments

Comments
 (0)