logic-helper
Version:
This template should help get you started developing with Vue 3 in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
74 lines (69 loc) • 2.83 kB
JavaScript
const actions = require('./actions.js')
const getConfig = require('../getConfig.js')
const fs = require('fs');
const path = require('path')
const EventServer = require('ws-event-bus/dist/server.js').default
const WsEventBus = require('ws-event-bus').default;
const indexHtml = path.resolve(__dirname, '../../dist/index.html')
const chalk = require('chalk')
const error = chalk.bold.red;
const warning = chalk.keyword('orange');
const success = chalk.green
function copyFile(from,to){
fs.copyFile(from, to, (err) => {
if (err) throw err;
});
}
class Server {
start(port) {
const config = getConfig()
const server = config.server;
return new Promise((resolve, reject) => {
this.start = Promise.resolve()
try {
const app = new EventServer({
port:server.port
})
app.static(path.resolve(__dirname, '../../dist'));
app.start().then(() => {
const eventBus = new WsEventBus({ //动态服务
id: 'topo-server',
url: `ws://${server.host}:${server.port}`
})
eventBus.on('error', (e) => {
error('Topo Server Event Error: ' + e)
startPort[server.port] = null;
reject()
})
eventBus.on('wait', (data, request) => {
if (data.action) {
if (data.data&&typeof data.data.file === 'string' && !/\.[a-zA-Z]+$/.test(data.data.file)) {
data.data.file += '.json';
}
actions[data.action](data.data)
.then((res) => {
eventBus.reply(request, res)
eventBus.send({event:'dispatch',id:'topo-server',data})
})
.catch((err) => {
eventBus.reply(request, err)
});
} else {
eventBus.reply(request, 'action is needed')
}
})
eventBus.on('open', (e) => {
console.log('>>>',success('topo server running at:'))
console.log('>>> Topo Editor : ',chalk.blue(`http://${server.host}:${server.port}`))
resolve()
})
})
} catch (e) {}
})
}
}
const ProxyServer = new Server();
if(`${process.cwd()}/server/koa`===__dirname){
ProxyServer.start();
}
module.exports = ProxyServer;