2007-02-02
I decided to do some more work on HttpServerTags. I like them so much and they've turned out to be a really good abstraction. Now that I'm using them on a server, it's annoying to have to write start/stop scripts to listen for sockets and run stuff.. so I figured I could make the socket listening pragmas as well. As well as that come a number of other small enhancements, so take a gander below as well as the examples even further down.
Changes:
- You now subclass ServletTags to add your servlets.
- ServletTags class side can now define the server addresses to listen on, url matching for requests that come in and host name matching for requests that come in. See examples below.
- ServletTags instances are given the server, request, response and session as instance variables to avoid passing those around everywhere.
- Defineing a server address on the class side of a ServletTags subclass automatically starts up a server for you, making development even easier. Servers auto-start on image startup too.
Subclasses of ServletTags are used to define a listener for the TagServer as well as servlets that will respond to requests. This is done using pragmas/tags that define the server address. You can implement a class side method on your subclass of ServletTags with the following pragmas:
examples <address: 'http://0.0.0.0' host: 'mycomputer.com'> <address: 'http://127.0.0.1' host: 'localhost' url: #( 'thisprogram' )> Transcript cr; show: 'My server has started!' Then on the instance side of your class, you can implement methods that accept the following pragmas: Tags to define what you're answering: <get: #()> <get: #('index.html')> <get: #('subpage' 'blah.css')> <post: #('actions')> <delete: #('webdav')> <put: #('webdav')> Tags to define how you're answering: myMethod <get: #()> response contentType: 'text/plain'; bodyBlock: [:stream | stream asStringStream nextPutAll: 'Hello World'] myMethod: writer <get: #()> <html> writer html body div text: 'Hello World!' myMethod: writer <get: #()> <xml> writer helloWorld text: 'Hello World!' myMethod: stream <get: #()> <stream: 'text/plain'> stream asStringStream nextPutAll: 'Hello World!' myMethod <get: #()> <string: 'text/plain'> ^'Hello World!' Tags to modify the response and request: myMethod <get: #()> <string: 'text/plain'> <session> ^session id printString