From e47a424a9f33648496c7ea4c5bcfc52a4dac015b Mon Sep 17 00:00:00 2001 From: lxsang Date: Fri, 30 Aug 2019 13:27:17 +0200 Subject: [PATCH] add server side api --- src/core/BaseApplication.coffee | 3 ++- src/core/BaseModel.coffee | 10 ++++++++- src/core/api.coffee | 2 ++ src/core/handlers/RemoteHandler.coffee | 16 ++++++++++++-- src/core/settings.coffee | 1 + src/packages/wTerm/main.coffee | 30 +++++++++++++++++++++----- 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/core/BaseApplication.coffee b/src/core/BaseApplication.coffee index 832c7a2..7bdd83c 100644 --- a/src/core/BaseApplication.coffee +++ b/src/core/BaseApplication.coffee @@ -18,7 +18,8 @@ class BaseApplication extends this.OS.GUI.BaseModel constructor: (name, args) -> super name, args - _OS.setting.applications[@name] = {} if not _OS.setting.applications[@name] + if (not _OS.setting.applications[@name]) or (Array.isArray OS.setting.applications[@name]) + _OS.setting.applications[@name] = {} @setting = _OS.setting.applications[@name] @keycomb = ALT: {} diff --git a/src/core/BaseModel.coffee b/src/core/BaseModel.coffee index df0d9b7..31e5eeb 100644 --- a/src/core/BaseModel.coffee +++ b/src/core/BaseModel.coffee @@ -43,7 +43,15 @@ class BaseModel mt = @meta() return mt.path if mt and mt.path return null - + + # call a server side script + call: (cmd, func) -> + @_api.apigateway cmd, false, func + + # get a stream + stream: () -> + return @_api.apigateway null, true, null + init: -> #implement by sub class onexit: (e) -> diff --git a/src/core/api.coffee b/src/core/api.coffee index 3d6c93e..6f7f5c8 100644 --- a/src/core/api.coffee +++ b/src/core/api.coffee @@ -337,6 +337,8 @@ self.OS.API = }, f setting: (f) -> _API.handler.setting f + apigateway: (d, ws, c) -> + return _API.handler.apigateway d, ws, c search: (text) -> r = [] diff --git a/src/core/handlers/RemoteHandler.coffee b/src/core/handlers/RemoteHandler.coffee index f662e9f..a8d0dee 100644 --- a/src/core/handlers/RemoteHandler.coffee +++ b/src/core/handlers/RemoteHandler.coffee @@ -18,9 +18,8 @@ self.OS.API.HOST = self.location.hostname+ (if self.location.port then":#{self.location.port}" else "") self.OS.API.REST = "#{self.location.protocol}//#{self.OS.API.HOST}" -self.OS.API.TERMURI = "wss://lxsang.me/wterm" - _REST = self.OS.API.REST +_HOST = self.OS.API.HOST self.OS.API.handler = # get file, require authentification get: "#{_REST}/VFS/get" @@ -82,6 +81,19 @@ self.OS.API.handler = scanapp: (p, c ) -> path = "#{_REST}/system/application" + + apigateway: (d, ws, c) -> + if ws + path = "#{_HOST}/system/apigateway?ws=1" + proto = if window.location.protocol is "https:" then "wss://" else "ws://" + socket = new WebSocket proto + path + if c then c(socket) + return socket + else + path = "#{_REST}/system/apigateway?ws=0" + _API.post path, d, c, (e, s) -> + _courrier.osfail __("Fail to invoke gateway api"), e, s + auth: (c) -> p = "#{_REST}/user/auth" _API.post p, {}, c, (e, s) -> diff --git a/src/core/settings.coffee b/src/core/settings.coffee index 1ac82b2..5d096b7 100644 --- a/src/core/settings.coffee +++ b/src/core/settings.coffee @@ -25,6 +25,7 @@ repeat: "repeat" } unless _OS.setting.appearance.wp _OS.setting.appearance.wps = [] unless _OS.setting.appearance.wps + _OS.setting.applications = {} unless _OS.setting.applications _OS.setting.user = conf.user _OS.setting.VFS = conf.VFS if conf.VFS _OS.setting.desktop.path = "home://.desktop" unless _OS.setting.desktop.path diff --git a/src/packages/wTerm/main.coffee b/src/packages/wTerm/main.coffee index 4074f96..9e2b792 100644 --- a/src/packages/wTerm/main.coffee +++ b/src/packages/wTerm/main.coffee @@ -75,9 +75,9 @@ class wTerm extends this.OS.GUI.BaseApplication me = @ @term.clear() @term.focus() - proto = if window.location.protocol is "https:" then "wss://" else "ws://" - #@socket = new WebSocket proto + @_api.HOST + "/wterm" - @socket = new WebSocket @_api.TERMURI + return @configure() unless @setting.uri + + @socket = new WebSocket @setting.uri @socket.onopen = () -> #el.style.display = "none" me.resizeContent (($ me.mterm).width()) , (($ me.mterm).height()) @@ -86,9 +86,29 @@ class wTerm extends this.OS.GUI.BaseApplication @socket.onmessage = (e) -> me.term.write e.data if me.term and e.data @socket.onclose = () -> me.socket = null - me.quit() + #me.quit() console.log "socket closed" #el.style.display = "block" - cleanup: (e)-> + cleanup: (e) -> @socket.close() if @socket + + menu: () -> + me = @ + { + text: "__(Edit)", + child: [ + { text: "__(Terminal URI)", dataid: "#{@name}-termuri" } + ], + onmenuselect: (e) -> me.configure() + } + + configure: () -> + @sock.close() if @socket + me = @ + @openDialog "PromptDialog", + (d) -> + return unless (d and d isnt "") + me.setting.uri = d + me.openSession() + , "__(Please enter terminal URI)", { label: "__(URI)", value: me.setting.uri || "wss://lxsang.me/wterm" } this.OS.register "wTerm", wTerm \ No newline at end of file