Hello World

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

redfoot.py --main=http://redfoot.net/2003/01/01/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 running Redfoot applications. The first thing redfoot.py does is to creates an InformationStore which is made available though the global name rednode. Next it opens the rednode from rednode.fs (unless a different location is specified with --rednode). After the rednode has been opened, redfoot.py will then look for the code pointed to by main. If no main has been specified it defaults to http://redfoot.net/2003/01/01/admin#Main. Also, if the resource is not yet known to the rednode then redfoot.py will attempt to load the resource via its URI. Unless the main's code string is still not known redfoot.py runs the main. When main returns redfoot.py commits any changes to the rednode, closes the rednode, and then exits.

If a value is specified for --main it will persists in the rednode and so it only needs to be specified the first time or when wishing to change the value of it.

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 Python code for helloworld's main:

  <red:Python rdf:ID="Main">
    <rdfs:label>Hello World Main</rdfs:label>      
    <red:codestr>
<![CDATA[

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/2003/01/01/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>

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/2003/01/01/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>