Creating Web Content
This page will provide an overview for creating web content and sending it through the embedded web server. All web content must reside in the directory set by the "http.root.dir" property in the server.properties file. By default, this is "http" and the page will assume the property is the default value.
Generating Content
All generated content are Groovy scripts that implement the Resource abstract class. Documentation for the class is available in the javadocs folder or can be viewed in comment form on the github page
To map a URL to a specific Groovy script, an accompanying XML file, webpages.xml, is provided. The XML file pairs URL requests to Groovy scripts and lists where the scripts live. Many to one pairings are allowed. An entry in the file follows the format:
<page name="$url" script="$script_name" />Example
The sample code shows how to create a page displaying "Hello World".
//Put this in a file named HelloWorld.groovy, place file in http/groovy
import com.github.etsai.kfsxtrackingserver.DataReader
import com.github.etsai.kfsxtrackingserver.web.Resource
import groovy.xml.MarkupBuilder
public class HelloWorld extends Resource {
@Override
public String generatePage() {
def writer= new StringWriter()
def htmlBuilder= new MarkupBuilder(writer)
htmlBuilder.html() {
head() {
meta('http-equiv':'content-type', content:'text/html; charset=utf-8')
title("Hello World")
}
body() {
p("Hello World")
}
}
return "<!DOCTYPE HTML>\r\n$writer"
}
}<!--
Put in webpages.xml, place in http directory. The classpath attribute sets where
the scripts live. If the attribute changes, the script directory name must also change
-->
<webpages classpath="groovy">
<!-- lets the default URL also call the same script -->
<page name="" script="HelloWorld.groovy" />
<page name="index.html" script="HelloWorld.groovy" />
</webpages>Sending Files
To send a file to the web browser, such as a css or js file, you only need to place the file in the http directory. The web server code will not read any files that are not in the http directory, and will return a 403 error for such requests.