Using Hyde for Static Sites

August 27, 2009

So you've decided to build a website, and you need to decide what software you're going to use to do so. There's a few options here:

  1. Use one of the existing services out there, like Blogger or Wordpress.
  2. Build it yourself using a web framework. There's a lot out there for whatever your programming language of choice may be.
  3. Make a static html site by hand.

Static sites are easy to build, and you just dump the static files into a directory and have your webserver do the rest. A problem is that static sites are a pain to maintain. Change one visual element in the header, and you have to make that change across every single page.

Using a Static Site Generator

A static site generator gives you the power of web framework templating and layouts, but without all the hassle that comes with maintaining a database and setting up the app server. The one I used for building this site with is Hyde, which uses Django templates.

This was a big win for me since I already knew how to use Django templates and template tags, and like Django configuration was minimal. The documentation was also good for a beta release, and seemed to do the job much more reliably than other site generators like Webby or Webgen.

Installation

The Hype repository on github has reasonably complete documentation on its wiki page, so head there for instructions on install and setup. The templates directory also has good examples on a basic site with blog posts and layout inheritance.

Local Development

Here's the small static server I use for local development, using web.py.

import os
import mimetypes
import web

basedir = os.path.dirname(os.path.abspath(__file__)) + '/deploy/'
notfoundpath = basedir + '404.html'

urls = (
    '/(.*)', 'static'
)

app = web.application(urls, globals())

class static:        
    def GET(self, path):
        fullpath = basedir + path
        
        if os.path.isdir(fullpath):
            if not fullpath.endswith('/'):
                fullpath += '/'
            
            fullpath += '/index.html'
        
        if os.path.isfile(fullpath):
            content_type, encoding = mimetypes.guess_type(path)
            
            if not content_type:
                content_type = 'text/html'
            
            web.header('Content-Type', content_type)
            web.header('Cache-Control', 'no-cache')
            return open(fullpath).read()        
        else:
            raise web.notfound(open(notfoundpath).read())
    
if __name__ == "__main__":
    app.run()

Production Deployment

For production deployment Apache is reasonably easy to configure and serve static files. Here's a basic virtual host config that should work for most environments.

NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin your-email@your-site.com
    ServerName your-site.com
    ServerAlias www.your-site.com
    ErrorLog logs/your-site.com-error_log
    CustomLog logs/your-site.com-access_log common

    DocumentRoot /path/to/your/hyde/project/deploy

    AddOutputFilterByType DEFLATE text/html text/css application/x-javascript
</VirtualHost>