It’s no secret that I’m a fan of Nu. I thought I’d mess around a bit today and try to serve web pages using Nu. It’s surprisingly easy so I figured I’d describe what I did.
I’m assuming that you’re running OS X and have Nu installed. If not, you should go do that now.
I’m also assuming you’re running apache and have it configured to serve cgi files. If neither of these are true, you should probably do a bit of googling and get that set up. On my test server, I have it configured to be able to run cgi files from any directory.
Add the following line to your *.conf (
/etc/apache2/users/username.confor/etc/apache2/httpd.conf) file somewhere:AddHandler cgi-script .nu. You could skip this if you’ve configured apache to run *.cgi files (in which case your Nu files will have to have the .cgi extension) but I like to know what files are Nu files and which aren’t so I set up apache to recognize the .nu file extension.Set your Nu file as executable (
chmod +xnufile.nu).Put this at the top of your nu file:
#!/usr/local/bin/nush. This tells apache which tool to use to parse the file. It’s known as a shebang line.Put the following below the last step:
(puts "Content-Type: text/html\n"). This is an HTTP header command that tells the browser that the following contents is an HTML document. If you forget this, your browser will complain that it got an internal server error.Code away in Nu and
putswhatever you want to display.Server environment variables can be retrieved from NSProcessInfo (
((NSProcessInfo processInfo) environment)).
Here’s a very simple Nu script that demonstrates steps 6 through 8:
#!/usr/local/bin/nush
(puts "Content-Type: text/html\n")
(puts "<p>Hello</p>")
(puts "<pre>#{(((NSProcessInfo processInfo) environment) description)}</pre>")
Important note: I expected to release a little example script that showed using Nu with the Calendar store to show off all of your upcoming and done tasks. Unfortunately, there seems to be a rather significant problem in how the Calendar store is implemented. If you run the file listed below, you’ll see that it works correctly. However, if you access it online, the calendar store fails somewhat spectacularly (nush[13290:10b] *** -[CalPersistentStoreCoordinator createPersistentStore:]: error on adding persistent store: Error Domain=NSCocoaErrorDomain Code=258 UserInfo=0x291dc0 "Invalid file name."). I suspect that the calendar store uses your user information to figure out where the store is located. However, when you access it via the web, the user is “www” (most likely, although you can change this). In this case, the calendar store cannot find a database and throws an exception. I just thought I’d illustrate this problem in case anyone runs into it.
#!/usr/local/bin/nush
(puts "Content-Type: text/html\n")
(load "CalendarStore")
(set p (CalCalendarStore taskPredicateWithCalendars:((CalCalendarStore defaultCalendarStore) calendars)))
(set tasks ((CalCalendarStore defaultCalendarStore) tasksWithPredicate:p))
(puts "<ul>")
(tasks each: (do (task)
(puts "<li>")
(if (task isCompleted)
(puts "<span style=\"text-decoration: line-through;\">#{(task title)}</span>")
(else
(puts (task title))))
(puts "</li>")
))
(puts "</ul>")
One comment on “Nu and apache”
From Concentrate Software:
January 29th, 2008 at 8:31 pm
[…] got a little excited about my last post and using Nu in apache so I wrote a simple function to parse the query string. You know, that bit […]
Leave a Reply