forked from salaheddine-zemmouri/docker-gui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (46 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const bodyParser = require('body-parser')
const express = require('express')
const session = require('express-session')
const sslify = require('express-sslify')
const http = require('http')
const path = require('path')
const api = require('./api')
const dockerfile = require('./api/dockerfilegen')
const config = require('./config')
const cors = require('cors');
const app = express()
const server = http.createServer(app)
const port = process.env.DOCKER_UI_PORT || config.port || 9898
const host = process.env.DOCKER_UI_HOST || config.host || 'localhost'
const assets = process.env.NODE_ENV === 'production' ? 'dist' : 'build'
// use it before all route definitions
app.use(cors({origin: '*'}));
// Force https if needed
if (process.env.DOCKER_UI_HTTPS || config.https) {
app.use(sslify.HTTPS({trustProtoHeader: process.env.DOCKER_UI_HTTPS_PROTO || config.httpsProto}))
}
// Add static files
app.use(express.static(path.join(__dirname, assets)))
// Add body parse middleware
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
// Add session middleware
app.use(session({
key: 'docker-ui.sid',
secret: process.env.DOCKER_UI_SECRET || config.secret || 'secret',
resave: false,
saveUninitialized: true,
}))
// Route to the api
app.use('/api/v1', api)
// Route to the Dockerfile generator
app.use("/api/dockerfile", dockerfile.router)
// Route to the index file
app.use('*', (req, res) => {
res.sendFile(path.join(__dirname, `${assets}/index.html`))
})
// Start the server
server.listen(port, host, err => {
if (err) throw err
console.log(`Listening ${host}:${port}`)
})