User Tools

Site Tools


02_user_tutorials:30_additional_interfaces:http_server:getting_started

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
02_user_tutorials:30_additional_interfaces:http_server:getting_started [2025/12/10 13:14] – removed - external edit (Unknown date) 127.0.0.102_user_tutorials:30_additional_interfaces:http_server:getting_started [2025/12/10 13:14] (current) – ↷ Page moved from 02_new_tutorials:30_additional_interfaces:http_server:getting_started to 02_user_tutorials:30_additional_interfaces:http_server:getting_started gaetan
Line 1: Line 1:
 +{{howhard>3}}
 +====== Getting started the HTTP-server ======
 +
 +GroIMP comes with an basic HTTP server that allows users to open projects from a given path.
 +The server can also manage basic additional input and the return of values to the http client.
 +
 +In the following a brief start for this is given.
 +
 +
 +===== Starting the Server and Running a first Model =====
 +
 +To start the server in GroIMP go on the main menu on Net/Open Http Server and select a port in the upcoming popup. This port is not specifically important, lets for now just use the default port suggested:"58080" (if you have another service running on this port just choose an other).
 +
 +
 +If we now go on [[http://localhost:58080]] GroIMP gives us a list of all installed plugins and the available http commands.
 +
 +The command we are interested in is the open command, which is called with:
 +
 +http://localhost:58080/open?<pathToyourGSZProject>
 +
 +This works with every project and just opens the project, but the definition of the path follows some semi-intuitive rules:
 +  - you don't use a key: different to most URL based parameters you do write something like ?path=my/fancy/project.gsz. You just write ?my/fancy/project.gsz
 +  - you cant use absolute paths. all paths start form the Root directory defined in the GroIMP preferences under HTTP server/ Open Project.
 +
 +
 +==== Get a return value ====
 +
 +
 +The returned values for the client needs to be defined in the project it self. Therefore we need a custom startup function which is called very time the project is started.
 +
 +Ours will now try to get the HttpResponse object for this workbench and if that exists tell GroIMP to start the run function with the response as a parameter.   
 +
 +<code java>
 +
 +import de.grogra.imp.net.*;
 +
 +protected void startup()
 +{
 + super.startup();
 + HttpResponse resp = HttpResponse.get(workbench());
 + if(resp!=null){
 + runLater(resp);
 + }
 +}
 +
 +</code>
 +
 +
 +The Run function can than be defined as you wish and would normally include the execution of your model similar to a headless execution. In our case we skip this part and directly return our message to the client and then close the workbench:
 +
 +<code java>
 +
 +protected void run(Object info)
 +{
 + HttpResponse resp = (HttpResponse)info; 
 + resp.setContent("text/text","UTF-8","hello");
 + resp.send(true);
 + closeWorkbench();
 +}
 +
 +</code>
 +
 +
 +If you save both of this files in a project and open it through the server it will return "hello" in our web browser. 
 +
 +Additionally it is possible to access the original request from the project, which allows us to receive additional parameters given by he client.
 +
 +For instance the following allows us to add ''&name=tim'' to the request and the project will greed me by name. 
 +
 +<code java>
 +import de.grogra.imp.net.*;
 +
 +protected void startup()
 +{
 + super.startup();
 + HttpResponse resp = HttpResponse.get(workbench());
 + if(resp!=null){
 + runLater(resp);
 + }
 +}
 +
 +protected void run(Object info)
 +{
 + HttpResponse resp = (HttpResponse)info; 
 + StringBuffer buf = new StringBuffer();
 + buf.append("Hi ");
 + buf.append(resp.getRequest().getQuery().split("&")[1].split("=")[1]);
 + buf.append(", whats up ");
 + resp.setContent("text/text","UTF-8",buf.toString());
 + resp.send(true);
 + closeWorkbench();
 +}
 +</code>
 +===== Starting the server headless =====
 +
 +GroIMP allows users to define which of its are suppose to be executed as an commandline parameter. 
 +This allows us to start the http server headless on a port of our linking: 
 +
 +<code>
 +java -jar core.jar --headless -- -cmd "/http/server=58080"
 +</code>
 +