-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (52 loc) · 2.19 KB
/
server.js
File metadata and controls
64 lines (52 loc) · 2.19 KB
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
const express = require('express');
const router = express.Router();
const cors = require('cors');
const ImageKit = require('imagekit');
const uuid = require('uuid');
const path = require('path');
const pugTemplatePath = path.join(__dirname, "../views/index.pug");
const app = express();
app.use(express.static('static'))
app.use(cors());
app.set('view engine', 'pug');
const startServer = (port = 3000, PUBLIC_KEY, PRIVATE_KEY, URL_ENDPOINT) => {
return new Promise((resolve, reject) => {
try {
const imagekit = new ImageKit({
publicKey: PUBLIC_KEY,
privateKey: PRIVATE_KEY,
urlEndpoint: URL_ENDPOINT
});
router.get("/auth", (req, res) => {
try {
const token = req.query.token || uuid.v4();
const expiration = req.query.expire || parseInt(Date.now()/1000)+ (60 * 10); // Default expiration in 10 mins
const signatureObj = imagekit.getAuthenticationParameters(token, expiration);
res.status(200).send(signatureObj);
} catch (err) {
console.error("Error while responding to auth request:", JSON.stringify(err, undefined, 2));
res.status(500).send("Internal Server Error");
}
});
router.get("/", (req, res) => {
try {
res.render(pugTemplatePath, {publicKey: PUBLIC_KEY, urlEndpoint: URL_ENDPOINT, authenticationEndpoint: `http://localhost:3000/auth`});
} catch (err) {
console.error("Error while responding to static page request:", JSON.stringify(err, undefined, 2));
res.status(500).send("Internal Server Error");
}
});
app.use("/",router);
app.listen(port, () => {
console.info(`Auth server running on port ${port}.`);
resolve();
});
} catch (err) {
console.error(JSON.stringify(err, undefined, 2));
reject("Error starting auth server.")
}
});
}
module.exports = {
startServer
}