-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathroutes.js
83 lines (67 loc) · 2.68 KB
/
routes.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
/**
* Route Mappings
* (sails.config.routes)
*
* Your routes tell Sails what to do each time it receives a request.
*
* For more information on configuring custom routes, check out:
* https://sailsjs.com/anatomy/config/routes-js
*/
const path = require('path');
const fs = require('fs');
const express = require('express'); // Express is a requirement of Sails.
module.exports.routes = {
// 'GET /': {
// skipAssets: true,
// fn: (req, res) => {
// return res.redirect(302, '/main'); // redirect to the "main" React app (the marketing site)
// }
// },
'GET /*': { // default route used to auto switch React apps
skipAssets: false,
fn: [
express.static(path.resolve(__dirname, '../.tmp/public/')),
async (req, res) => {
// This will determine which React app we need to serve.
let url = req.url;
if (url === '/') {
url = '/main'; // Default to "main"
}
const parts = url.split('/', 3); // Set a limit, just to save a bit of processing power, and help prevent abuse.
const pathToCheck = path.join(__dirname, '../.tmp/public/', parts[1], '/index.html');
if (fs.existsSync(pathToCheck)) {
await sails.helpers.finalizeRequestLog(req, res, {body: 'view'});
return res.type('html').send(fs.readFileSync(pathToCheck));
}
res.status(404);
await sails.helpers.finalizeRequestLog(req, res, {view: '404'});
if (req.wantsJSON) {
return res.json({success: false, error: 'URL does not exist.'});
}
return res.view('404');
}
]
},
'GET /admin': {
fn: (req, res) => {
return res.redirect(302, '/admin/dashboard');
}
},
'GET /api/v1/users': 'admin/get-users',
'GET /api/v1/users/deleted': 'admin/get-deleted-users',
'POST /api/v1/user': 'admin/create-user',
'PUT /api/v1/user/:id': 'admin/edit-user',
'DELETE /api/v1/user/:id': 'admin/delete-user',
'PUT /api/v1/user/:id/reactivate': 'admin/reactivate-user',
'POST /api/v1/token': 'admin/create-api-token',
'POST /api/v1/login': 'common/login',
'GET /api/v1/logout': 'common/logout',
'GET /api/v1/me': 'common/get-me',
'POST /api/v1/password': 'common/change-password',
'POST /api/v1/2fa/enable': 'common/enable-2fa',
'POST /api/v1/2fa/finalize': 'common/finalize-2fa',
'POST /api/v1/2fa/disable': 'common/disable-2fa',
'GET /_ping': (req, res) => {
return res.ok('pong');
}
};