WebDev Module

About

This module allows users to directly program against the web server inside the Ignition Gateway. Webpages can be build by hand using a combination of python programming and static web resources such as images, css files, javascript files, and html files.

DISCLAIMER  The Web Dev module requires specialized web-programming knowledge. The Inductive Automation support team is unable to provide detailed advice about creating a particular site. Furthermore, they are unable to provide troubleshooting beyond the basic functionality of the module.

Basic Usage

When the WebDev module is installed, a new kind of project resource heading will apear in the Designer's project browser called "Web Dev". Right-clicking on this heading will allow the creation of three new types of project resources:

1. Python Resource
Python resources are dynamic web resources. Each time a user browses to the URL associated with a python resource, the script will run and generate a response.
2. File Resource
A file resource is a static resource, usually a binary resources such as an image.
3. Text resource
A text resource is a static resource much like a file resource, except that its contents may be directly edited from within the Ignition designer. These are useful for static HTML, CSS, and JavaScript files.

Each type of resource may specify its content type. It is important to specify the correct content type for the contents of the resource.

URLs

Each resource will be directly accessible over HTTP and mounted beneath the /main/system/webdev path. For example, if you created a Text Resource directly beneath the "Web Dev", it would be mounted at: http://host:port/main/system/webdev/project/resource_name

Notice that the project name and resource name are part of the path. If your resource is nested inside a folder, it will be part of the path too, for example: http://host:port/main/system/webdev/project/folder_name/resource_name

Web dev resources may have periods in their name. This meas that if you upload an image file, you may include its extension directly in its name so that its path is more natural, for example, you might name an image resource "my_image.png" so that its URL is: http://host:port/main/system/webdev/project/my_image.png

The Web Dev module respects the published vs. staging system of Ignition. If your project is set up to have separate staging and published verions, you may access your staging version resources via the URL: http://host:port/main/system/webdev_staging/project/resource_name

Requests to the root of your project, i.e. http://host:port/main/system/project will attempt to load a resource named "index.html". If no such resource exists, a 404 response code will be returned instead.

Python Resources

Python resources are the heart of the functionality of the Web Dev module. These resources are completely dynamic, and can handle all parts of the HTTP protocol, formulating any type of response.

Each time an incoming HTTP request arrives an a URL where a python resource is mounted, the corresponding python script will be run. Each python resource may have up to seven scripts, one for each HTTP method (GET, POST, HEAD, TRACE, etc.). In practice, most python resources will probably only implement one or two of these, usually doGet or doPost at a minimum.

Parameters

Each do* method receives the same two parameters: 'request' and 'session'.

request Parameter

The request parameter is a dictionary with a bunch of information about the incoming web request.

request['params']
Any URL parameters such as /main/system/webdev/project/foo?param1=value&param2=other_value will be contained in a dictionary accessible via request['params']. For the given example, request['params'] = {'param1':'value', 'param2':'other_value'}
request['postData']
This parameter is only present for the doPost method, and its value is different based upon the value of the incoming HTTP request's Content-Type header. If the content type is application/json, then the request['postData'] will be a python dictionary structure which is the result of parsing the json document that was posted. If the content type starts with text/, then the value of request['postData'] will be the text which was posted, as a string.
request['headers']
The HTTP request headers will be in a dictionary under request['headers']. For example, you could read the User-Agent string with request['headers']['User-Agent'].
request['scheme']
The scheme is available via request['scheme']. The value will be 'http' or 'https'.
request['remoteAddr'] / request['remoteHost']
These two parameters give the remote IP address and host of the entity that made the web request. Note that these are from the perspective of the web server, and so may not be what you expect due to the effects of things like NAT-ing routers.
request['servletRequest'] / request['servletResponse']
These two parameters give you direct access to the underlying Java servlet request and response objects.
request['context']
This provides direct access to the Ignition GatewayContext. More information about this object may be found in the Ignition SDK developer guide and associated JavaDocs.

session Parameter

The session parameter is a Python dictionary which may be used to hold information which persists across multiple different calls to your Web Dev Python Resource, or across multiple Python Resources. Session management is handled automatically, and this dictionary is created for each new client session (the client must have cookies enabled for sessions to work). You may place any key-value pairs in the session dictionary you'd like, just make sure that the values are things that can be serialized. All normal python types (scalar, dictionary, lists, and tuples) are serializable.

Return Value

Each do* function on a python resource must generate some sort of HTTP response. This is done by returning a value from the function. The return value should always be a dictionary, in which the following keys are recognized. The keys are listed here in the order they are evaluated. For example, if you have both file and bytes, only file will take effect. The exception is the contentType key, which may be included with any of the other keys to override the default content type.

file
The value should be a string, which should be the path to a file on the server's filesystem. If no contentType is specified, then the system will attempt to probe the content type from the operating system using java.nio.Files.probeContentType. If the file key is present, but the value points to a file that doesn't exist, an HTTP 404 will be returned.
bytes
The value should be a byte array. The default content type is application/octet-stream, but you probably want to specify your own.
html
The value should be a string, which should be the source of an HTML document. Content type is assumed to be text/html.
json
The value is assumed to either be a string (which should be valid json) or to be a python object, which will then be encoded into a json string. Content type will be application/json
response
If none of the other keys are present, the system will look for the key response which will be stringified and then returned with the content type text/plain.

If your implementation of the do* function returns a dictionary with none of the above keys, an HTTP 500 error will be returned. However, if you return None, no HTTP 500 error will be returned. In this case, it is assumed that you used the request['servletResponse'] parameter to directly formulate your HTTP response.

Security Settings

Each python resource can configure its own optional security settings.

Require HTTPS
If this is checked, then the resource will only be accessible via an SSL connection. If a non-secure HTTP transport is used, the browser will be sent a redirect to the the gateway's SSL port. The gateway must have SSL enabled, of course.
Require Authentication

If this is checked, the resource will require authentication before it executes. This uses HTTP BASIC auth, and so should really be combined with the Require HTTPS option so that the credentials are encrypted. The username/password combination sent through the HTTP BASIC authentication headers will then be passed through the chosen User Source. If roles are specified, the user must have at least one of the roles. Specify multiple acceptible roles using a comma separated list. If the credentials are missing, an HTTP 401 will be returned with the WWW-Authenticate header. If the credentials are present but incorrect, an HTTP 403 will be returned.

If the credentials succeed, the python resource will execute. In addition, the authenticated user object returned by the User Source will be accessible inside the session object as session['user']. Since the user is stored in session, if the client has cookies enabled, then further requests against the same session will use the stored user object and will not require additional authentication.