This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
144 lines (122 loc) · 3.94 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
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
#!/usr/bin/env node
/*!
* hex
* Copyright (C) 2019-present imtbl https://github.com/imtbl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const bodyParser = require('body-parser')
const service = require('restana')()
require('dotenv').config()
const config = require('./src/config')
const logger = require('./src/util/logger')
const schemas = require('./src/util/schemas')
const importer = require('./src/importer')
service.use(bodyParser.json())
service.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader(
'Access-Control-Allow-Headers',
'Accept, Authorization, Content-Type, Origin, X-Requested-With'
)
res.setHeader(
'Access-Control-Allow-Methods', 'GET, HEAD, POST, OPTIONS'
)
if (req.method === 'OPTIONS') {
return res.send()
}
next()
})
service.get('/', (req, res) => {
res.send({
hex: {
version: config.version,
apiVersion: config.apiVersion
}
})
})
service.get('/settings', async (req, res) => {
try {
await schemas.settings.validateAsync({
accessKey: req.headers.authorization
})
} catch (err) {
return res.send({
error: `${err.details[0].path[0]}`
}, 400)
}
res.send({
settings: {
skipImport: config.skipImport,
skipKnownFiles: config.skipKnownFiles,
deleteArchivesAfterImport: config.deleteArchivesAfterImport,
skipTags: config.skipTags,
blacklistedNamespaces: config.blacklistedNamespaces,
namespaceReplacements: config.namespaceReplacements,
additionalTags: config.additionalTags,
addUniqueIdentifierTag: config.additionalTags,
uniqueIdentifierNamespace: config.uniqueIdentifierNamespace
}
})
})
service.post('/import', async (req, res) => {
try {
await schemas.import.validateAsync({
accessKey: req.headers.authorization,
cookies: req.body.cookies,
url: req.body.url,
skipImport: req.body.skipImport,
skipKnownFiles: req.body.skipKnownFiles,
deleteArchivesAfterImport: req.body.deleteArchivesAfterImport,
skipTags: req.body.skipTags,
blacklistedNamespaces: req.body.blacklistedNamespaces,
namespaceReplacements: req.body.namespaceReplacements,
additionalTags: req.body.additionalTags,
addUniqueIdentifierTag: req.body.addUniqueIdentifierTag,
uniqueIdentifierNamespace: req.body.uniqueIdentifierNamespace
})
} catch (err) {
return res.send({
error: `${err.details[0].path[0]}`
}, 400)
}
res.send({
import: req.body.url
})
importer.run(req.body.url, req.body.cookies, {
skipImport: req.body.skipImport,
skipKnownFiles: req.body.skipKnownFiles,
deleteArchivesAfterImport: req.body.deleteArchivesAfterImport,
skipTags: req.body.skipTags,
blacklistedNamespaces: req.body.blacklistedNamespaces,
namespaceReplacements: req.body.namespaceReplacements,
additionalTags: req.body.additionalTags,
addUniqueIdentifierTag: req.body.addUniqueIdentifierTag,
uniqueIdentifierNamespace: req.body.uniqueIdentifierNamespace
})
})
;(async () => {
await service.start(config.port)
logger.log('hex has started.')
})()
process.on('SIGTERM', async () => {
await service.close()
logger.log('hex has shut down.')
process.exit(0)
})
process.on('SIGINT', async () => {
await service.close()
logger.log('hex has shut down.')
process.exit(0)
})