Running the examples locally

The Modelit Embedded Webserver Toolbox comes with a number of examples that work out of the box. Do the following to run these examples :

  • Download the MEWT p-code zipfile;
  • Extract the ZIP file to a new folder on your computer;
  • Create a Matlab desktop icon, with "Start in" equal to this folder;
  • Start Matlab;
  • Run "demo_MEWT" from the command line
>> demo_MEWT
  • Verify the examples using the instructions as displayed on the command line. Each of the examples can be verified by using a webbrowser pointing to the corresponding example. E.g. localhost:8082/image for the example that produces a Matlab image. Note that the corresponding callbacks can be found in the examples directory of the toolbox.

Verify live examples

http

Instead of running demo_MEWT yourself, you can also verify an instance of demo_MEWT that is running on our server.
For this purpose we have compiled demo_MEWT.m and deployed it in a Docker container. We have also forwarded port 8082 to our server. This establishes a http connection to MEWT.

https

For each example, the second (https) url uses the secure connection of our Apache HTTP server and redirects the requests to the Docker container with the compiled Matlab code. This method is documented in a separate blog article. We recommend this method as it piggybacks on the on the site certificate and obviates the need to maintain a separate certificate for each single webservice.

Links

Use the links below to make a request to the server, a new window will open with the result generated by the Embedded HTTP server.
The following examples are available (The Matlab source code for each example is available by clicking on the link in the last column. The source code for demo_MEWT is shown at the end of this article): 

Example URL  
generate HTML code http://195.240.96.93:8082/html
https://modelit.nl/demo_MEWT/html
source
generate Image (PNG) http://195.240.96.93:8082/image
https://modelit.nl/demo_MEWT/image
source
show pdf document http://195.240.96.93:8082/pdf
https://modelit.nl/demo_MEWT/pdf
source
generate json code http://195.240.96.93:8082/json
https://modelit.nl/demo_MEWT/json
source
generate xml code http://195.240.96.93:8082/xml
https://modelit.nl/demo_MEWT/xml
source
capture Matlab figure http://195.240.96.93:8082/figure
https://modelit.nl/demo_MEWT/figure
source
temperature conversion http://195.240.96.93:8082/temperatureconversionsimple
https://modelit.nl/demo_MEWT/temperatureconversionsimple
source
temperature conversion with JavaScript http://195.240.96.93:8082/temperatureconversion
https://modelit.nl/demo_MEWT/temperatureconversion
source
AG-Grid (https://www.ag-grid.com/) http://195.240.96.93:8082/table/index.html
https://modelit.nl/demo_MEWT/table/index.html
source
Login form. Hint: username="user", password="1234". http://195.240.96.93:8082/authentication
https://modelit.nl/demo_MEWT/authentication
source
HTML code, login required, note that login is only required once. http://195.240.96.93:8082/html2
https://modelit.nl/demo_MEWT/html2
source

Remote Matlab function

To verify this example, run from the command line:

fp_sort = remoteFunction('http://195.240.96.93:8082/sort',@sort);
[B,I] = fp_sort(10:-1:1)

http://195.240.96.93:8082/sort (note: no useful output in browser)

 

 

 

 

 

Source code of demo_MEWT.m

function server = demo_MEWT
% demo_MEWT - Server with examples of callbacks. The server listens to
% port in the range 8082 and creates a number of callbacks
%
% CALL:
%     httpsServerExample
%
% INPUT:
%     no input required
%
% OUTPUT:
%     server: <modelit.web.server.Server>
%       a server listening to the port 8082
%       localhost:port/ ==> InfoCallback
%       localhost:port/html ==> HTMLCallback
%       localhost:port/xml ==> XMLCallback
%       localhost:port/json ==> JSONCallback
%       localhost:port/pdf ==> PDFCallback
%       localhost:port/image ==> ImageCallback
%       localhost:port/figure ==> FigureCallback
%       localhost:port/authentication ==> AuthenticationCallback (log in with username:
%       user, password: 1234)
%       localhost:port/html2 ==> HTMLCallback, credentials required
%
% NOTE:
%     this example

% Use generateSelfSignedCertificate to make a keystore

% Server listens to http request on port 8082
disp('Run demo for Modelit Embedded Webserver (MEWT)')

port = 8082;

server = modelit.web.server.Server(port);
urlroot=sprintf('localhost:%d',port);

dprintf('Launching example "InfoCallback" ...')
server.addContext('/',@InfoCallback);
dprintf('To verify this example, open the url "%s" in your web browser',...
    sprintf('%s/',urlroot))

dprintf('Launching example "HTMLCallback" ...')
server.addContext('/html',@HTMLCallback);
dprintf('To verify this example, open the url "%s" in your web browser',...
    sprintf('%s/html',urlroot))

dprintf('Launching example "XMLCallback" ...')
server.addContext('/xml',@XMLCallback);
dprintf('To verify this example, open the url "%s" in your web browser',...
    sprintf('%s/xml',urlroot))

dprintf('Launching example "JSONCallback" ...')
server.addContext('/json',@JSONCallback);
dprintf('To verify this example, open the url "%s" in your web browser',...
    sprintf('%s/json',urlroot))

dprintf('Launching example "PDFCallback" ...')
server.addContext('/pdf',@PDFCallback);
dprintf('To verify this example, open the url "%s" in your web browser',...
    sprintf('%s/pdf',urlroot))

dprintf('Launching example "ImageCallback" ...')
server.addContext('/image',@ImageCallback);
dprintf('To verify this example, open the url "%s" in your web browser',...
    sprintf('%s/image',urlroot))

dprintf('Launching example "FigureCallback" ...')
server.addContext('/figure',@FigureCallback);
dprintf('To verify this example, open the url "%s" in your web browser',...
    sprintf('%s/figure',urlroot))

dprintf('Launching example "AuthenticationCallback" ...')
server.addContext('/authentication',@AuthenticationCallback,'user','1234');
dprintf('To verify this example, open the url "%s" in your web browser. Hint: username="user", password="1234".',...
    sprintf('%s/authentication',urlroot))

dprintf('Launching example "HTMLCallback with credentials" ...')
server.addContext('/html2',@HTMLCallback,'user','1234');
dprintf('To verify this example, open the url "%s" in your web browser',...
    sprintf('%s/html2',urlroot))

dprintf('Launching example "remoteCallback" ...')
server.addContext('/sort',remoteCallback('sort'));
dprintf('To verify this example, start a SECOND Matlab session. Then issue commands:');
dprintf('    fp_sort = remoteFunction(''http://%s/sort'',@sort)',urlroot);
disp('    [B,I] = fp_sort(10:-1:1)');

% start listening
server.start;

HWIN=figure('Name',sprintf('Server is active on port %d',port),'DeleteFcn',@(o,e)stopserver(server));
h=uicontrol(HWIN,'style','pushbutton','str','Stop server','units','norm','position',[.1,.4,.8,.2],'callback',@(o,e)delete(HWIN));

function stopserver(server)
disp('Stopping server...')
server.stop;
disp('The server has been stopped.')