-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
145 lines (123 loc) · 3.51 KB
/
server.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const backstop = require('backstopjs')
const express = require('express')
const bodyParser = require('body-parser')
const decache = require('decache')
const PROJECTS_DIRECTORY = __dirname + '/projects'
const filesystem = require('./server/filesystem')
const app = express()
// To serve static pages
app.use(express.static(__dirname + '/data'))
app.use(express.static(__dirname + '/build'))
// API server
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Authorization, Accept'
)
next()
})
// API root. Just a message
app.get('/api/', function(req, res) {
res.send('VeRTa API server.')
})
// List of projects
app.get('/api/projects', function(req, res) {
const projects = filesystem.listDir(PROJECTS_DIRECTORY)
res.send(projects)
})
// Serve project config file
app.get('/api/projects/:project', function(req, res) {
const projectName = req.params.project
const configPath = './projects/' + projectName + '/config.js'
decache(configPath)
const projectConfig = require(configPath)
res.send(projectConfig())
})
// Create new project (directory and config file)
app.post('/api/projects/:project', function(req, res) {
const projectName = req.params.project
const projectDescription = req.body.description
const mkdir = filesystem.createProject(
PROJECTS_DIRECTORY,
projectName,
projectDescription
)
res.send(mkdir)
})
// Update project (config file)
app.post('/api/projects/:project/update', function(req, res) {
const projectName = req.params.project
const projectConfig = req.body.config
const update = filesystem.updateConfig(
PROJECTS_DIRECTORY,
projectName,
projectConfig
)
res.send(update)
})
// Create project's reference
app.get('/api/projects/:project/reference', function(req, res) {
const projectName = req.params.project
const configPath = './projects/' + projectName + '/config.js'
const projectConfig = require(configPath)
backstop('reference', {
config: projectConfig()
})
.then(() => {
res.send(true)
})
.catch(() => {
res.send(false)
})
})
// Create project's test files
app.get('/api/projects/:project/test', function(req, res) {
const projectName = req.params.project
const configPath = './projects/' + projectName + '/config.js'
const projectConfig = require(configPath)
backstop('test', {
config: projectConfig()
})
.then(() => {
res.send(true)
})
.catch(() => {
res.send(false)
})
})
// Approve project's test files
app.get('/api/projects/:project/approve', function(req, res) {
const projectName = req.params.project
const configPath = './projects/' + projectName + '/config.js'
const projectConfig = require(configPath)
backstop('approve', {
config: projectConfig()
})
.then(() => {
res.send(true)
})
.catch(() => {
res.send(false)
})
})
// Show project's report
app.get('/api/projects/:project/report', function(req, res) {
const projectName = req.params.project
const configPath = './projects/' + projectName + '/config.js'
const projectConfig = require(configPath)
backstop('openReport', {
config: projectConfig()
})
.then(() => {
res.send('Report in new tab.')
})
.catch(() => {
res.send(false)
})
})
const server = app.listen(3000, function() {
console.log('Listening on port %s...', server.address().port)
})