mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-14 19:52:10 +08:00
feat: cross go demo
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
module.exports = () => {
|
||||
return {
|
||||
openDevTools: {
|
||||
mode: 'undocked'
|
||||
mode: 'bottom'
|
||||
},
|
||||
jobs: {
|
||||
messageLog: true
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const { crossService } = require('../service/cross');
|
||||
|
||||
/**
|
||||
* Cross
|
||||
* @class
|
||||
@@ -10,17 +12,7 @@ class CrossController {
|
||||
* View process service information
|
||||
*/
|
||||
info() {
|
||||
const pids = Cross.getPids();
|
||||
Log.info('cross pids:', pids);
|
||||
|
||||
let num = 1;
|
||||
pids.forEach(pid => {
|
||||
let entity = Cross.getProc(pid);
|
||||
Log.info(`server-${num} name:${entity.name}`);
|
||||
Log.info(`server-${num} config:`, entity.config);
|
||||
num++;
|
||||
})
|
||||
|
||||
crossService.info();
|
||||
return 'hello electron-egg';
|
||||
}
|
||||
|
||||
@@ -29,7 +21,7 @@ class CrossController {
|
||||
*/
|
||||
async getUrl(args) {
|
||||
const { name } = args;
|
||||
const serverUrl = Cross.getUrl(name);
|
||||
const serverUrl = crossService.getUrl(name);
|
||||
return serverUrl;
|
||||
}
|
||||
|
||||
@@ -39,12 +31,7 @@ class CrossController {
|
||||
*/
|
||||
async killServer(args) {
|
||||
const { type, name } = args;
|
||||
if (type == 'all') {
|
||||
Cross.killAll();
|
||||
} else {
|
||||
Cross.killByName(name);
|
||||
}
|
||||
|
||||
crossService.killServer(type, name);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -54,11 +41,11 @@ class CrossController {
|
||||
async createServer(args) {
|
||||
const { program } = args;
|
||||
if (program == 'go') {
|
||||
Services.get('cross').createGoServer();
|
||||
crossService.createGoServer();
|
||||
} else if (program == 'java') {
|
||||
Services.get('cross').createJavaServer();
|
||||
crossService.createJavaServer();
|
||||
} else if (program == 'python') {
|
||||
Services.get('cross').createPythonServer();
|
||||
crossService.createPythonServer();
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -69,20 +56,8 @@ class CrossController {
|
||||
*/
|
||||
async requestApi(args) {
|
||||
const { name, urlPath, params} = args;
|
||||
const hc = new HttpClient();
|
||||
const serverUrl = Cross.getUrl(name);
|
||||
console.log('Server Url:', serverUrl);
|
||||
|
||||
const apiHello = serverUrl + urlPath;
|
||||
const options = {
|
||||
method: 'GET',
|
||||
data: params || {},
|
||||
dataType: 'json',
|
||||
timeout: 1000,
|
||||
};
|
||||
const result = await hc.request(apiHello, options);
|
||||
|
||||
return result.data;
|
||||
const data = await crossService.requestApi(name, urlPath, params);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const { ElectronEgg } = require('ee-core');
|
||||
const Lifecycle = require('./preload/lifecycle');
|
||||
const preload = require('./preload/index');
|
||||
const { Lifecycle } = require('./preload/lifecycle');
|
||||
const { preload } = require('./preload');
|
||||
|
||||
// new app
|
||||
const app = new ElectronEgg();
|
||||
@@ -10,8 +10,10 @@ const life = new Lifecycle();
|
||||
app.register("ready", life.ready);
|
||||
app.register("electron-app-ready", life.electronAppReady);
|
||||
app.register("window-ready", life.windowReady);
|
||||
app.register("preload", preload);
|
||||
app.register("before-close", life.beforeClose);
|
||||
|
||||
// register preload
|
||||
app.register("preload", preload);
|
||||
|
||||
// run
|
||||
app.run();
|
||||
@@ -2,12 +2,14 @@
|
||||
** preload为预加载模块,该文件将会在程序启动时加载 **
|
||||
*************************************************/
|
||||
|
||||
function preload() {
|
||||
// 示例功能模块,可选择性使用和修改
|
||||
console.log('preload/index.js');
|
||||
}
|
||||
|
||||
/**
|
||||
* 预加载模块入口
|
||||
*/
|
||||
module.exports = async () => {
|
||||
|
||||
// 示例功能模块,可选择性使用和修改
|
||||
console.log('preload/index.js');
|
||||
module.exports = {
|
||||
preload
|
||||
}
|
||||
@@ -5,16 +5,12 @@ const { getMainWindow } = require('ee-core/electron');
|
||||
|
||||
class Lifecycle {
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* core app have been loaded
|
||||
*/
|
||||
async ready() {
|
||||
// do some things
|
||||
console.log('------------lifecycle ready');
|
||||
console.log('[lifecycle] ready');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,14 +18,14 @@ class Lifecycle {
|
||||
*/
|
||||
async electronAppReady() {
|
||||
// do some things
|
||||
console.log('-----------lifecycle electron-app-ready');
|
||||
console.log('[lifecycle] electron-app-ready');
|
||||
}
|
||||
|
||||
/**
|
||||
* main window have been loaded
|
||||
*/
|
||||
async windowReady() {
|
||||
console.log('-------------lifecycle window-ready');
|
||||
console.log('[lifecycle] window-ready');
|
||||
// 延迟加载,无白屏
|
||||
const { windowsOption } = getConfig();
|
||||
if (windowsOption.show == false) {
|
||||
@@ -45,9 +41,11 @@ class Lifecycle {
|
||||
* before app close
|
||||
*/
|
||||
async beforeClose() {
|
||||
console.log('-----------lifecycle before-close');
|
||||
console.log('[lifecycle] before-close');
|
||||
}
|
||||
}
|
||||
|
||||
Lifecycle.toString = () => '[class Lifecycle]';
|
||||
module.exports = Lifecycle;
|
||||
module.exports = {
|
||||
Lifecycle
|
||||
};
|
||||
@@ -3,7 +3,9 @@
|
||||
const { logger } = require('ee-core/log');
|
||||
const { getExtraResourcesDir } = require('ee-core/ps');
|
||||
const path = require("path");
|
||||
const Is = require('ee-core/utils/is');
|
||||
const axios = require('axios');
|
||||
const { is } = require('ee-core/utils');
|
||||
const { cross } = require('ee-core/cross');
|
||||
|
||||
/**
|
||||
* cross
|
||||
@@ -11,6 +13,34 @@ const Is = require('ee-core/utils/is');
|
||||
*/
|
||||
class CrossService {
|
||||
|
||||
info() {
|
||||
const pids = cross.getPids();
|
||||
logger.info('cross pids:', pids);
|
||||
|
||||
let num = 1;
|
||||
pids.forEach(pid => {
|
||||
let entity = cross.getProc(pid);
|
||||
logger.info(`server-${num} name:${entity.name}`);
|
||||
logger.info(`server-${num} config:`, entity.config);
|
||||
num++;
|
||||
})
|
||||
|
||||
return 'hello electron-egg';
|
||||
}
|
||||
|
||||
getUrl(name) {
|
||||
const serverUrl = cross.getUrl(name);
|
||||
return serverUrl;
|
||||
}
|
||||
|
||||
killServer(type, name) {
|
||||
if (type == 'all') {
|
||||
cross.killAll();
|
||||
} else {
|
||||
cross.killByName(name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* create go service
|
||||
* In the default configuration, services can be started with applications.
|
||||
@@ -18,7 +48,7 @@ class CrossService {
|
||||
*/
|
||||
async createGoServer() {
|
||||
// method 1: Use the default Settings
|
||||
//const entity = await Cross.run(serviceName);
|
||||
//const entity = await cross.run(serviceName);
|
||||
|
||||
// method 2: Use custom configuration
|
||||
const serviceName = "go";
|
||||
@@ -29,7 +59,7 @@ class CrossService {
|
||||
args: ['--port=7073'],
|
||||
appExit: true,
|
||||
}
|
||||
const entity = await Cross.run(serviceName, opt);
|
||||
const entity = await cross.run(serviceName, opt);
|
||||
logger.info('server name:', entity.name);
|
||||
logger.info('server config:', entity.config);
|
||||
logger.info('server url:', entity.getUrl());
|
||||
@@ -50,18 +80,18 @@ class CrossService {
|
||||
args: ['-jar', '-server', '-Xms512M', '-Xmx512M', '-Xss512k', '-Dspring.profiles.active=prod', `-Dserver.port=18080`, `-Dlogging.file.path=${Ps.getLogDir()}`, `${jarPath}`],
|
||||
appExit: false,
|
||||
}
|
||||
if (Is.macOS()) {
|
||||
if (is.macOS()) {
|
||||
// Setup Java program
|
||||
opt.cmd = path.join(Ps.getExtraResourcesDir(), 'jre1.8.0_201.jre/Contents/Home/bin/java');
|
||||
opt.cmd = path.join(getExtraResourcesDir(), 'jre1.8.0_201.jre/Contents/Home/bin/java');
|
||||
}
|
||||
if (Is.linux()) {
|
||||
if (is.linux()) {
|
||||
// Setup Java program
|
||||
}
|
||||
|
||||
const entity = await Cross.run(serviceName, opt);
|
||||
const entity = await cross.run(serviceName, opt);
|
||||
logger.info('server name:', entity.name);
|
||||
logger.info('server config:', entity.config);
|
||||
logger.info('server url:', Cross.getUrl(entity.name));
|
||||
logger.info('server url:', cross.getUrl(entity.name));
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -73,7 +103,7 @@ class CrossService {
|
||||
*/
|
||||
async createPythonServer() {
|
||||
// method 1: Use the default Settings
|
||||
//const entity = await Cross.run(serviceName);
|
||||
//const entity = await cross.run(serviceName);
|
||||
|
||||
// method 2: Use custom configuration
|
||||
const serviceName = "python";
|
||||
@@ -85,13 +115,33 @@ class CrossService {
|
||||
windowsExtname: true,
|
||||
appExit: true,
|
||||
}
|
||||
const entity = await Cross.run(serviceName, opt);
|
||||
const entity = await cross.run(serviceName, opt);
|
||||
logger.info('server name:', entity.name);
|
||||
logger.info('server config:', entity.config);
|
||||
logger.info('server url:', entity.getUrl());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
async requestApi(name, urlPath, params) {
|
||||
const serverUrl = cross.getUrl(name);
|
||||
const apiHello = serverUrl + urlPath;
|
||||
console.log('Server Url:', serverUrl);
|
||||
|
||||
const response = await axios({
|
||||
method: 'get',
|
||||
url: apiHello,
|
||||
timeout: 1000,
|
||||
params,
|
||||
proxy: false,
|
||||
});
|
||||
if (response.status == 200) {
|
||||
const { data } = response;
|
||||
return data;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
CrossService.toString = () => '[class CrossService]';
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const { SqliteStorage } = require('ee-core/storage');
|
||||
const { getStorageDir } = require('ee-core/ps');
|
||||
const { getDataDir } = require('ee-core/ps');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
@@ -22,7 +22,7 @@ class BasedbService {
|
||||
*/
|
||||
_init() {
|
||||
// 定义数据文件
|
||||
const dbFile = path.join(getStorageDir(), "db", this.dbname);
|
||||
const dbFile = path.join(getDataDir(), "db", this.dbname);
|
||||
const sqliteOptions = {
|
||||
timeout: 6000,
|
||||
verbose: console.log
|
||||
|
||||
@@ -1,117 +1,104 @@
|
||||
<template>
|
||||
<div id="app-cross-go">
|
||||
<div class="one-block-1">
|
||||
<span>
|
||||
1. 基础控制
|
||||
</span>
|
||||
</div>
|
||||
<div class="one-block-2">
|
||||
<a-space>
|
||||
<a-button @click="create()"> 启动 </a-button>
|
||||
<a-button @click="getUrl()"> 获取地址 </a-button>
|
||||
<a-button @click="kill()"> kill </a-button>
|
||||
<a-button @click="info()"> test </a-button>
|
||||
</a-space>
|
||||
</div>
|
||||
<div class="one-block-1">
|
||||
<span>
|
||||
2. 发送http请求
|
||||
</span>
|
||||
</div>
|
||||
<div class="one-block-2">
|
||||
<a-space>
|
||||
<a-button @click="request(1)"> 前端发送 </a-button>
|
||||
<a-button @click="request(2)"> 主进程发送 </a-button>
|
||||
</a-space>
|
||||
</div>
|
||||
<div class="one-block-1">
|
||||
<span>
|
||||
3. 多个服务
|
||||
</span>
|
||||
</div>
|
||||
<div class="one-block-2">
|
||||
<a-space>
|
||||
<a-button @click="create()"> 启动 </a-button>
|
||||
<a-button @click="killAll()"> kill所有 </a-button>
|
||||
</a-space>
|
||||
</div>
|
||||
<div id="app-cross-go">
|
||||
<div class="one-block-1">
|
||||
<span>
|
||||
1. 基础控制
|
||||
</span>
|
||||
</div>
|
||||
<div class="one-block-2">
|
||||
<a-space>
|
||||
<a-button @click="create()"> 启动 </a-button>
|
||||
<a-button @click="getUrl()"> 获取地址 </a-button>
|
||||
<a-button @click="kill()"> kill </a-button>
|
||||
<a-button @click="info()"> test </a-button>
|
||||
</a-space>
|
||||
</div>
|
||||
</template>
|
||||
<script>
|
||||
import { ipcApiRoute } from '@/api';
|
||||
import { ipc } from '@/utils/ipcRenderer';
|
||||
import axios from 'axios';
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
type: 1,
|
||||
serverUrl: ''
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
info() {
|
||||
ipc.invoke(ipcApiRoute.cross.crossInfo, {}).then(res => {
|
||||
console.log('res:', res);
|
||||
})
|
||||
},
|
||||
getUrl() {
|
||||
ipc.invoke(ipcApiRoute.cross.getCrossUrl, {name: 'goapp'}).then(url => {
|
||||
this.serverUrl = url;
|
||||
this.$message.info(`服务地址: ${url}`);
|
||||
})
|
||||
},
|
||||
kill() {
|
||||
// name参数是 进程对象上的name,这里仅作为参照
|
||||
ipc.invoke(ipcApiRoute.cross.killCrossServer, {type: 'one', name: 'goapp'})
|
||||
},
|
||||
killAll() {
|
||||
ipc.invoke(ipcApiRoute.cross.killCrossServer, {type: 'all', name: 'goapp'})
|
||||
},
|
||||
create() {
|
||||
ipc.invoke(ipcApiRoute.cross.createCrossServer, { program: 'go' })
|
||||
},
|
||||
request(type) {
|
||||
if (type == 1 && this.serverUrl == "") {
|
||||
this.$message.info("请先获取服务地址");
|
||||
return
|
||||
}
|
||||
if (type == 1) {
|
||||
const testApi = this.serverUrl + '/api/hello';
|
||||
const cfg = {
|
||||
method: 'get',
|
||||
url: testApi,
|
||||
params: { id: '111'},
|
||||
timeout: 1000,
|
||||
}
|
||||
axios(cfg).then(res => {
|
||||
console.log('res:', res);
|
||||
const data = res.data.data || null;
|
||||
this.$message.info(`服务返回: ${data}`);
|
||||
})
|
||||
} else {
|
||||
ipc.invoke(ipcApiRoute.cross.requestApi, {name: 'goapp', urlPath: '/api/hello'}).then(res => {
|
||||
console.log('res:', res);
|
||||
const data = res.data || null;
|
||||
this.$message.info(`服务返回: ${data}`);
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
<style lang="less" scoped>
|
||||
#app-cross-go {
|
||||
padding: 0px 10px;
|
||||
text-align: left;
|
||||
width: 100%;
|
||||
.one-block-1 {
|
||||
font-size: 16px;
|
||||
padding-top: 10px;
|
||||
}
|
||||
.one-block-2 {
|
||||
padding-top: 10px;
|
||||
}
|
||||
<div class="one-block-1">
|
||||
<span>
|
||||
2. 发送http请求
|
||||
</span>
|
||||
</div>
|
||||
<div class="one-block-2">
|
||||
<a-space>
|
||||
<a-button @click="request(1)"> 前端发送 </a-button>
|
||||
<a-button @click="request(2)"> 主进程发送 </a-button>
|
||||
</a-space>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<script setup>
|
||||
import { ipcApiRoute } from '@/api';
|
||||
import { ipc } from '@/utils/ipcRenderer';
|
||||
import axios from 'axios';
|
||||
import { ref } from 'vue';
|
||||
import { message } from 'ant-design-vue';
|
||||
|
||||
const serverUrl = ref('');
|
||||
|
||||
function info() {
|
||||
ipc.invoke(ipcApiRoute.cross.crossInfo, {}).then(res => {
|
||||
console.log('res:', res);
|
||||
})
|
||||
}
|
||||
|
||||
function getUrl() {
|
||||
ipc.invoke(ipcApiRoute.cross.getCrossUrl, {name: 'goapp'}).then(url => {
|
||||
serverUrl.value = url;
|
||||
message.info(`服务地址: ${url}`);
|
||||
})
|
||||
}
|
||||
|
||||
function kill() {
|
||||
// name参数是 进程对象上的name,这里仅作为参照
|
||||
ipc.invoke(ipcApiRoute.cross.killCrossServer, {type: 'one', name: 'goapp'})
|
||||
}
|
||||
|
||||
function create() {
|
||||
ipc.invoke(ipcApiRoute.cross.createCrossServer, { program: 'go' })
|
||||
}
|
||||
|
||||
function request(type) {
|
||||
if (type == 1 && serverUrl.value == "") {
|
||||
message.info("请先获取服务地址");
|
||||
return
|
||||
}
|
||||
</style>
|
||||
|
||||
if (type == 1) {
|
||||
const testApi = serverUrl.value + '/api/hello';
|
||||
const cfg = {
|
||||
method: 'get',
|
||||
url: testApi,
|
||||
params: { id: '111'},
|
||||
timeout: 1000,
|
||||
}
|
||||
axios(cfg).then(res => {
|
||||
console.log('res:', res);
|
||||
const data = res.data.data || null;
|
||||
message.info(`服务返回: ${data}`);
|
||||
})
|
||||
} else {
|
||||
ipc.invoke(ipcApiRoute.cross.requestApi, {name: 'goapp', urlPath: '/api/hello'}).then(res => {
|
||||
console.log('res:', res);
|
||||
if (res) {
|
||||
const { data} = res;
|
||||
message.info(`服务返回: ${data}`);
|
||||
} else {
|
||||
message.info(`服务无返回`);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<style lang="less" scoped>
|
||||
#app-cross-go {
|
||||
padding: 0px 10px;
|
||||
text-align: left;
|
||||
width: 100%;
|
||||
.one-block-1 {
|
||||
font-size: 16px;
|
||||
padding-top: 10px;
|
||||
}
|
||||
.one-block-2 {
|
||||
padding-top: 10px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -3,10 +3,10 @@ tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
# 编译使用的shell命令
|
||||
cmd = "go build -tags=fts5 -o ./tmp/goapp ./main.go"
|
||||
cmd = "go build -o ./tmp/goapp ./main.go"
|
||||
|
||||
# 由`cmd`命令得到的二进制文件名
|
||||
bin = "./tmp/goapp --basedir=../ --env=dev --port=7003"
|
||||
bin = "./tmp/goapp --basedir=../ --env=dev --port=7073"
|
||||
# 在运行二进制文件时添加额外的参数 (bin/full_bin)。将运行“./tmp/main hello world”
|
||||
# args_bin = ["hello", "world"]
|
||||
|
||||
|
||||
@@ -3,10 +3,10 @@ tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
# 编译使用的shell命令
|
||||
cmd = "go build -tags=fts5 -o ./tmp/goapp.exe ./main.go"
|
||||
cmd = "go build -o ./tmp/goapp.exe ./main.go"
|
||||
|
||||
# 由`cmd`命令得到的二进制文件名
|
||||
bin = "./tmp/goapp.exe --basedir=../ --env=dev --port=7003"
|
||||
bin = "./tmp/goapp.exe --basedir=../ --env=dev --port=7073"
|
||||
# 在运行二进制文件时添加额外的参数 (bin/full_bin)。将运行“./tmp/main hello world”
|
||||
# args_bin = ["hello", "world"]
|
||||
|
||||
|
||||
@@ -3,10 +3,10 @@ tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
# 编译使用的shell命令
|
||||
cmd = "go build -tags=fts5 -o ./tmp/goapp ./main.go"
|
||||
cmd = "go build -o ./tmp/goapp ./main.go"
|
||||
|
||||
# 由`cmd`命令得到的二进制文件名
|
||||
bin = "./tmp/goapp --basedir=../ --env=dev --port=7003"
|
||||
bin = "./tmp/goapp --basedir=../ --env=dev --port=7073"
|
||||
# 在运行二进制文件时添加额外的参数 (bin/full_bin)。将运行“./tmp/main hello world”
|
||||
# args_bin = ["hello", "world"]
|
||||
|
||||
|
||||
@@ -3,10 +3,10 @@ tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
# 编译使用的shell命令
|
||||
cmd = "go build -tags=fts5 -o ./tmp/goapp.exe ./main.go"
|
||||
cmd = "go build -o ./tmp/goapp.exe ./main.go"
|
||||
|
||||
# 由`cmd`命令得到的二进制文件名
|
||||
bin = "./tmp/goapp.exe --basedir=../ --env=dev --port=7003"
|
||||
bin = "./tmp/goapp.exe --basedir=../ --env=dev --port=7073"
|
||||
# 在运行二进制文件时添加额外的参数 (bin/full_bin)。将运行“./tmp/main hello world”
|
||||
# args_bin = ["hello", "world"]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user