123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132(*********************************************************************************)(* Stog *)(* *)(* Copyright (C) 2012-2024 INRIA All rights reserved. *)(* Author: Maxence Guesdon, INRIA Saclay *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public *)(* License along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)moduleS=Cohttp_lwt_unix.ServermoduleJ=Yojson.SafeopenStog.Urllet(>>=)=Lwt.bindmoduleServer_P=structincludeOjs_base.Rpc.Base(Stog_multi_ed_common.Types.App_msg)letwsdata_of_msgmsg=J.to_string(Stog_multi_ed_common.Types.server_msg_to_yojsonmsg)letmsg_of_wsdata=Ojs_server.Server.mk_msg_of_wsdataStog_multi_ed_common.Types.client_msg_of_yojsonendmoduleServer=Ojs_server.Server.Make(Server_P)moduleSFT=Ojs_filetree_server.Server.Make(Stog_multi_ed_common.Types.FT)moduleSED=Ojs_ed_server.Server.Make(Stog_multi_ed_common.Types.ED)moduleGit=Git_server.Make(Stog_multi_ed_common.Types.Git)(*
class myft broadcall broadcast ~id root =
object(self)
inherit SFT.filetree broadcall broadcast ~id root as super
method handle_message msg =
prerr_endline "message!";
super#handle_message msg
end
*)letinit?sshkey~stog_dir~git()=letconnections=newServer.connection_groupinletfiletrees=newSFT.filetreesconnections#broadcallconnections#broadcast(newSFT.filetree)inleteditors=newSED.editorsconnections#broadcallconnections#broadcast(newSED.editor)inletgit_repos=newGit.reposconnections#broadcallconnections#broadcast(newGit.repo)inlet_ft=filetrees#add_filetreeStog_multi_ed_common.Types.ft_idstog_dirinlet_ed=editors#add_editorStog_multi_ed_common.Types.ed_idstog_dirinlet_repo=git_repos#add_repo~id:Stog_multi_ed_common.Types.gitrepo_id?sshkeygitinlethandle_messagesend_msgrpcmsg=matchmsgwith|Stog_multi_ed_common.Types.ED.Editor_->editors#handle_messagesend_msgmsg|Stog_multi_ed_common.Types.FT.Filetree_->filetrees#handle_messagesend_msgmsg|Stog_multi_ed_common.Types.Git.Git_->git_repos#handle_messagesend_msgmsg|Server_P.Call(call_id,((Stog_multi_ed_common.Types.FT.Filetree_)asmsg))->letreturnmsg=Server.Rpc.returnrpccall_idmsginfiletrees#handle_callreturnmsg|Server_P.Call(call_id,((Stog_multi_ed_common.Types.ED.Editor_)asmsg))->letreturnmsg=Server.Rpc.returnrpccall_idmsgineditors#handle_callreturnmsg|Server_P.Call(call_id,((Stog_multi_ed_common.Types.Git.Git_)asmsg))->letreturnmsg=Server.Rpc.returnrpccall_idmsgingit_repos#handle_callreturnmsg|_->letstr=Printf.sprintf"Unhandled message (Stog_multi_ed.handle_message): %s"(Printexc.to_string(Obj.magicmsg))infailwithstrinconnections#set_handle_messagehandle_message;connectionsletbody_tmpl=[%xtmpl"templates/multi_ed.tmpl"]letpagecfguser~ws_url~title~client_js_url=letclient_js_url=Stog.Url.to_stringclient_js_urlinletjs=["stog_server = { wsUrl: '"^(Stog.Url.to_stringws_url)^"' } ;"]inletbody=body_tmpl~client_js_url~ft_id:Stog_multi_ed_common.Types.ft_id~ojs_msg_id:Stog_multi_ed_common.Types.ojs_msg_id~bar_id:Stog_multi_ed_common.Types.bar_id~git_id:Stog_multi_ed_common.Types.gitrepo_id~ed_id:Stog_multi_ed_common.Types.ed_id()inPage.pagecfguser~empty:true~title~jsbodyletclient_js="stog_multi_ed.js"letclient_js_content=[%blob"stog_multi_ed_js.bc.js"]leteditor_pagecfguser~http_url~ws_urlbase_pathsession_id=letclient_js_path=base_path@["editor";client_js]in(* FIXME: port number when we will be able to change an
http connection into a websocket one manually *)letclient_js_url=Stog.Url.appendhttp_url.pubclient_js_pathinletws_url=Stog.Url.appendws_url.pub(base_path@["editor"])inlettitle=Printf.sprintf"Session %S"session_idinpagecfg(Someuser)~ws_url~title~client_js_urllethttp_handlercfguser~http_url~ws_urlbase_pathsession_idreqbody=function|[s]whens=client_js->Stog_server.Preview.respond_jsclient_js_content|[]|[""]->letbody=editor_pagecfguser~http_url~ws_urlbase_pathsession_idinletbody=Xtmpl.Rewrite.to_stringbodyinS.respond_string~status:`OK~body()|_->S.respond_error~status:`Not_found~body:""()