|
| 1 | +import {SendType, ServerApiType, ServerContext, ServerInfo} from "../mapi/server/type"; |
| 2 | + |
| 3 | +const serverRuntime = { |
| 4 | + port: 0, |
| 5 | +} |
| 6 | + |
| 7 | +let shellController = null |
| 8 | +let isRunning = false |
| 9 | + |
| 10 | +export const ServerLive: ServerContext = { |
| 11 | + ServerApi: null as ServerApiType | null, |
| 12 | + ServerInfo: null as ServerInfo | null, |
| 13 | + url() { |
| 14 | + return `http://localhost:${serverRuntime.port}/` |
| 15 | + }, |
| 16 | + send(type: SendType, data: any) { |
| 17 | + this.ServerApi.event.sendChannel(this.ServerInfo.eventChannelName, {type, data}) |
| 18 | + }, |
| 19 | + |
| 20 | + async _client() { |
| 21 | + return await this.ServerApi.GradioClient.connect(this.url()); |
| 22 | + }, |
| 23 | + async init() { |
| 24 | + }, |
| 25 | + async start() { |
| 26 | + // console.log('this.ServerApi.app.availablePort(50617)', await this.ServerApi.app.availablePort(50617)) |
| 27 | + this.send('starting', this.ServerInfo) |
| 28 | + let command = [] |
| 29 | + if (this.ServerInfo.setting?.['port']) { |
| 30 | + serverRuntime.port = this.ServerInfo.setting.port |
| 31 | + } else if (!serverRuntime.port || !await this.ServerApi.app.isPortAvailable(serverRuntime.port)) { |
| 32 | + serverRuntime.port = await this.ServerApi.app.availablePort(50617) |
| 33 | + } |
| 34 | + const env = await this.ServerApi.env() |
| 35 | + command.push(`"${this.ServerInfo.localPath}/launcher"`) |
| 36 | + command.push(`--env=DEBUG=true`) |
| 37 | + const dep = process.platform === 'win32' ? ';' : ':' |
| 38 | + env['PATH'] = process.env['PATH'] || '' |
| 39 | + env['PATH'] = `${this.ServerInfo.localPath}/binary${dep}${env['PATH']}` |
| 40 | + env['AIGCPANEL_SERVER_PORT'] = `${serverRuntime.port}` |
| 41 | + env['AIGCPANEL_SERVER_PLACEHOLDER_CONFIG'] = './_aigcpanel/build-config-live.json' |
| 42 | + // console.log('command', JSON.stringify(command)) |
| 43 | + shellController = await this.ServerApi.app.spawnShell(command, { |
| 44 | + stdout: (data) => { |
| 45 | + this.ServerApi.file.appendText(this.ServerInfo.logFile, data) |
| 46 | + }, |
| 47 | + stderr: (data) => { |
| 48 | + this.ServerApi.file.appendText(this.ServerInfo.logFile, data) |
| 49 | + }, |
| 50 | + success: (data) => { |
| 51 | + this.send('success', this.ServerInfo) |
| 52 | + }, |
| 53 | + error: (data, code) => { |
| 54 | + this.ServerApi.file.appendText(this.ServerInfo.logFile, data) |
| 55 | + this.send('error', this.ServerInfo) |
| 56 | + }, |
| 57 | + env, |
| 58 | + cwd: this.ServerInfo.localPath, |
| 59 | + }) |
| 60 | + }, |
| 61 | + async ping() { |
| 62 | + try { |
| 63 | + const res = await this.ServerApi.request(`${this.url()}ping`) |
| 64 | + return true |
| 65 | + } catch (e) { |
| 66 | + } |
| 67 | + return false |
| 68 | + }, |
| 69 | + async stop() { |
| 70 | + this.send('stopping', this.ServerInfo) |
| 71 | + try { |
| 72 | + shellController.stop() |
| 73 | + shellController = null |
| 74 | + } catch (e) { |
| 75 | + console.log('stop error', e) |
| 76 | + } |
| 77 | + this.send('stopped', this.ServerInfo) |
| 78 | + }, |
| 79 | + async cancel() { |
| 80 | + await this.ServerApi.launcherCancel(this) |
| 81 | + }, |
| 82 | + async config() { |
| 83 | + return { |
| 84 | + code: 0, |
| 85 | + msg: "ok", |
| 86 | + data: { |
| 87 | + httpUrl: shellController ? this.url() : null, |
| 88 | + content: ``, |
| 89 | + functions: { |
| 90 | + live: { |
| 91 | + content: ``, |
| 92 | + param: [], |
| 93 | + }, |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + }, |
| 98 | + async apiRequest(data: { |
| 99 | + url: string, |
| 100 | + param: any, |
| 101 | + }, option: any) { |
| 102 | + serverRuntime.port = 60617 |
| 103 | + // console.log('apiRequest', {url: this.url(), data, option}) |
| 104 | + const {url, param} = data |
| 105 | + return this.ServerApi.request(`${this.url()}${url}`, param, { |
| 106 | + method: 'POST', |
| 107 | + }) |
| 108 | + }, |
| 109 | +} |
0 commit comments