mirror of
https://github.com/EvolutionAPI/evolution-manager.git
synced 2025-12-19 11:32:18 -06:00
feat: cli v0.2.0
This commit is contained in:
60
lib/server.js
Normal file
60
lib/server.js
Normal file
@@ -0,0 +1,60 @@
|
||||
// const pm2 = require('pm2')
|
||||
const mime = require('mime-types');
|
||||
var http = require('http');
|
||||
var fs = require('fs');
|
||||
const url = require('url');
|
||||
const build = require('./utils/build.js')
|
||||
|
||||
const functions = {
|
||||
start,
|
||||
build,
|
||||
}
|
||||
|
||||
module.exports = async (argv) => {
|
||||
if (argv._.length === 1) throw new Error('No operation specified')
|
||||
|
||||
const operation = argv._[1]
|
||||
if (!functions[operation]) throw new Error(`Unknown operation: ${operation}`)
|
||||
|
||||
await functions[operation](argv)
|
||||
}
|
||||
|
||||
|
||||
|
||||
function startServer(argv) {
|
||||
const { port = 9615 } = argv || {}
|
||||
|
||||
const index = fs.readFileSync(process.cwd() + '/dist/index.html');
|
||||
|
||||
http.createServer(function (req, res) {
|
||||
try {
|
||||
const parsedUrl = url.parse(req.url, true);
|
||||
|
||||
// verify if url is a file in dist folder
|
||||
if (parsedUrl.pathname === '/') throw {}
|
||||
let filePath = process.cwd() + '/dist' + parsedUrl.pathname;
|
||||
|
||||
if (fs.existsSync(filePath)) {
|
||||
const contentType = mime.lookup(filePath) || 'text/plain';
|
||||
res.writeHead(200, { 'Content-Type': contentType });
|
||||
res.end(fs.readFileSync(filePath));
|
||||
return
|
||||
}
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(index);
|
||||
} catch {
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(index);
|
||||
}
|
||||
}).listen(port);
|
||||
|
||||
console.log('🚀 Server start')
|
||||
console.log('🚀 Server listening on port ' + port)
|
||||
}
|
||||
|
||||
async function start(argv) {
|
||||
await build()
|
||||
startServer(argv)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user