Hello World

The following hello world example can be run with the following command:

redfoot.py --main=http://redfoot.net/2002/11/29/helloworld#Main

Once running you should be able to visit: http://localhost:8080/ and http://localhost:9090/

Let's take a close look at how it all works. First let's start by looking at redfoot.py, a program for installing and running Redfoot applications. The first time redfoot.py is run it will install the latest version of the Redfoot hypercode listed in releases by saving the hypercode in redfoot.rdf. The --main option specifies the URIRef that Redfoot is to call to kick off the application. The value specified for --main persists in the redfoot.rdf and so it only needs to be specified the first time or when wishing to change the value of it. The last thing redfoot.py does is to run the Redfoot hypercode which in turn will run the main.

Next let's take a closer look at the Hypercode found in helloworld.

Hypercode is Python code embeded inside RDF and can be serialized in the RDF/XML syntax:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
   xmlns:red="http://redfoot.net/2002/10/red#"
>

The first resource we describe is the red:Init that we use for helloworld's main:


  <red:Init rdf:about="http://redfoot.net/2002/11/29/helloworld#Main">
    <red:code>
      <red:Python>
        <rdfs:label>Hello World Main</rdfs:label>      
        <red:codestr>
<![CDATA[
#'
# add additional contexts to the rednode
rednode.import_context("http://www.w3.org/2000/01/rdf-schema")
rednode.import_context("http://www.w3.org/1999/02/22-rdf-syntax-ns")

RedHandler = red_import("http://redfoot.net/2002/11/12/RedHandler#module")
medusaglue = red_import("http://redfoot.net/2002/11/05/medusaglue#module")

redhandler = RedHandler.RedHandler(rednode)

# Load admin context and start a server for the admin pages.
rednode.import_context("http://redfoot.net/2002/11/29/admin")
admin_server = medusaglue.RedServer("", 9090) # address, port
admin_server.add_app(redhandler)
admin_server.run(1, 1) # background and daemon

# Start server listening on "" (all names) port 7780
server = medusaglue.RedServer("", 8080) # address, port
server.add_app(redhandler)
server.run() # blocking call...
# Nothing after this will get executed until run exits with a KeyboardInterrupt
rednode.save()

]]>
        </red:codestr>
      </red:Python>
    </red:code>
  </red:Init>

Next we describe the resource http://localhost:8080/ which gets used when browsers request that URL from the server to generate a response. RedHandler will look to see if the URL being requested has a red:facet property and if it does uses it generate the response.

  <rdf:Description rdf:about="http://localhost:8080/">
    <rdfs:label>Hello World</rdfs:label>
    <red:facet>
      <red:Facet>
        <rdfs:label>Hello World Facet</rdfs:label>
        <red:outer rdf:resource="http://redfoot.net/2002/11/09/redsite#outer"/>
        <red:code>
          <red:Python>
            <rdfs:label>Redsite Outer page</rdfs:label>
            <red:codestr>
<![CDATA[

response.write("""

<p>Hello World!</p>

""")

]]>
            </red:codestr>
          </red:Python>
        </red:code>
      </red:Facet>
    </red:facet>  
  </rdf:Description>
  

</rdf:RDF>