Hello, world!

This is a simple application that returns a plain text page that says "Hello, world!". Our web application is structured into a dispatcher (our "main responder"), an action, and words to create and run the web server.

Templates

To begin experimenting with templates, lets change the logic to include a form where a name can be provided. We will create a Chloe template file. Let's create a hello.xml file in the same location as the webapps.hello vocabulary:

Now, modify the hello-action to load the template. The default form submission is via POST and can be supported using the submit slot of the action. We respond to a form submission by returning a plain text response saying "Hello, $name!":

When you navigate to http://localhost:8080, you will see a simple form prompting you to type in a name. After submitting the form, you will see a customized response depending on the name provided.

Form Validation

It is frequently useful to validate parameters that are submitted via forms (e.g., for numbers, e-mail addresses, ranges, required or optional, etc.). To support this, we need to add validation logic for every parameter desired (using words from the validators vocabulary). In this case, the name should be a required parameter:

@Peter: When you say "database persistence", I assume you mean the form validation? That just requires the use of "v-required" and "validate-params" and the addition of the "<alloy>" call. It seems to work for me?

The form validation uses the database to persist validation errors across requests. By adding the "<alloy>", you can now also add other kinds of persistence -- maybe tracking the first time each name said hello, and adding a "We first met N seconds ago" message. That might be a simple way to learn database persistence. The Factor documentation has a db.tuples tutorial that seems like a good place to start.

@mrjbq7: when I run the example as written in the blog, I get an "The image refers to a library or symbol that was not found at load time" error. If I remove the line with reference to the database ""resource:hello.db" " it works fine. I got the same error in db.tuples tutorial.

@Peter: You are probably running on Windows? Factor doesn't come with the DLLs for SQLite, but you can install it if you want -- either from the SQLite project, or download the 32-bit or 64-bit DLLs (you want "sqlite3.dll"). If you copy that to your Factor directory, it should work.

This is probably a Factor-newbie mistake, but perhaps you can help. When running your first example in the listener, everything works fine, but when I try to save it as a file in work/webapps/hello/hello.factor and run it from the command line with `./factor -run=webapps.hello`, I get the error below. What I'm trying do is run a web server in Factor from the command line in the foreground, so is there is another way, I'd be happy to hear suggestions. Thanks!

@Ryan: Sorry about that! Every "main method" must have a stack effect of "( -- )", meaning takes no inputs and gives no outputs. In my case, the "run-hello", produces a "http-server" object which caused the traceback you see. I updated the example to use "wait-for-server" which will block until the server finishes serving connections. Can you try again?

Now that I got it working, I just put together a Heroku buildpack for Factor that uses your example (with a link back for credit, of course). Check it out if you want to run Factor apps on Heroku: https://github.com/ryanbrainard/heroku-buildpack-factor