2022-03-19 15:46:13 +00:00
|
|
|
import express, {
|
|
|
|
Request,
|
|
|
|
Response,
|
|
|
|
NextFunction,
|
|
|
|
} from 'common-server/utils/express';
|
2020-03-04 22:19:39 +00:00
|
|
|
const app = express();
|
|
|
|
|
2022-03-01 20:48:48 +00:00
|
|
|
import 'common-server/utils/env';
|
|
|
|
import 'common-server/utils/process';
|
2020-03-04 22:19:39 +00:00
|
|
|
|
2022-02-27 12:33:33 +00:00
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import http from 'http';
|
|
|
|
|
|
|
|
http.createServer(app);
|
|
|
|
|
|
|
|
import bodyParser from 'body-parser';
|
2022-02-28 12:00:02 +00:00
|
|
|
|
2022-02-27 12:33:33 +00:00
|
|
|
import cors from 'cors';
|
2020-03-04 22:19:39 +00:00
|
|
|
|
2020-03-04 17:28:54 +00:00
|
|
|
app.use(cors());
|
|
|
|
|
2022-03-19 15:46:13 +00:00
|
|
|
app.use(function(req: Request, res: Response, next: NextFunction) {
|
2020-03-04 22:19:39 +00:00
|
|
|
if (typeof req.body === 'string') {
|
|
|
|
req.body = JSON.parse(req.body);
|
|
|
|
}
|
2022-02-28 13:44:21 +00:00
|
|
|
res.header('Access-Control-Allow-Credentials', 'true');
|
2020-03-04 22:19:39 +00:00
|
|
|
res.header('Access-Control-Allow-Origin', req.headers.origin);
|
|
|
|
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
|
|
|
|
res.header(
|
|
|
|
'Access-Control-Allow-Headers',
|
|
|
|
'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept,Authorization'
|
|
|
|
);
|
2021-06-29 19:18:32 +00:00
|
|
|
return next();
|
2020-03-04 22:19:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add limit of 10 MB to avoid "Request Entity too large error"
|
|
|
|
// https://stackoverflow.com/questions/19917401/error-request-entity-too-large
|
|
|
|
app.use(bodyParser.urlencoded({ limit: '10mb', extended: true }));
|
|
|
|
app.use(bodyParser.json({ limit: '10mb' }));
|
|
|
|
|
2020-03-12 23:07:43 +00:00
|
|
|
//View engine setup
|
|
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
|
|
|
|
app.use(express.static(path.join(__dirname, 'views')));
|
2021-11-24 07:42:40 +00:00
|
|
|
app.use('/', express.static(path.join(__dirname, 'views', 'img')));
|
2020-03-12 23:07:43 +00:00
|
|
|
|
2020-03-04 22:19:39 +00:00
|
|
|
// Routes(API)
|
2020-04-08 19:32:14 +00:00
|
|
|
app.use('/license/validate', require('./src/api/license'));
|
2020-03-10 21:10:59 +00:00
|
|
|
app.set('port', process.env.PORT || 3004);
|
2020-03-04 22:19:39 +00:00
|
|
|
|
2022-03-19 15:46:13 +00:00
|
|
|
const server = http.listen(app.get('port'), function() {
|
2020-03-04 22:19:39 +00:00
|
|
|
// eslint-disable-next-line
|
|
|
|
console.log('Server Started on port ' + app.get('port'));
|
|
|
|
});
|
|
|
|
|
2022-03-19 15:46:13 +00:00
|
|
|
app.get(['/', '/license'], function(req: Request, res: Response) {
|
2020-03-04 22:19:39 +00:00
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.send(
|
|
|
|
JSON.stringify({
|
|
|
|
status: 200,
|
|
|
|
message: 'Service Status - OK',
|
2021-11-22 11:18:22 +00:00
|
|
|
serviceType: 'oneuptime-license-server',
|
2020-03-04 22:19:39 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2020-03-04 17:28:54 +00:00
|
|
|
|
2022-03-19 15:46:13 +00:00
|
|
|
app.use('/*', function(req: Request, res: Response) {
|
2020-03-12 23:07:43 +00:00
|
|
|
res.status(404).render('notFound.ejs', {});
|
2020-03-04 22:19:39 +00:00
|
|
|
});
|
2020-03-04 17:28:54 +00:00
|
|
|
|
2022-02-25 13:22:20 +00:00
|
|
|
export default app;
|
2022-03-19 15:46:13 +00:00
|
|
|
module.exports.close = function() {
|
2020-03-04 22:19:39 +00:00
|
|
|
server.close();
|
|
|
|
};
|