Permalink
Fetching contributors…
Cannot retrieve contributors at this time
108 lines (84 sloc) 3.73 KB

mod_swift - mods_baredemo

Apache 2 Swift3 macOS Travis

mod_swift is a technology demo which shows how to write native modules for the Apache Web Server in the Swift 3 programming language. Server Side Swift the right way.

mods_baredemo

This project contains two 'raw' Apache handlers written in Swift. They are very basic and only annotate the Apache API instead of wrapping it in some pretty Swift library.

The project contains a pre-configured Xcode scheme for Apache. Just press run in Xcode and you should be able to access Apache as http://localhost:8042.

RequestInfoHandler

RequestInfoHandler.swift just shows a few properties available as part of the Apache request. If it is invoked on behalf of a matching file in the document root, it will also deliver the file to the browser.

MustacheHandler

MustacheHandler.swift loads and evaluates a Mustache template sitting the Apache document root. The handler is attached to the mustache file type using a configuration like this:

<Location />
  AddType application/x-zeezide-mustache .mustache
  AddHandler de.zeezide.mustache .mustache
</Location>

This will trigger the handler for all files ending in .mustache within the document root.

Here is your screenshot:

and here is some code used to generate that page (shortened, full):

let sampleDict  : [ String : Any ] = [
  "name"        : "Chris",
  "value"       : 10000,
  "taxed_value" : Int(10000 - (10000 * 0.4))
]

func MustacheHandler(p: UnsafeMutablePointer<request_rec>?) -> Int32 {
  var req = ZzApacheRequest(raw: p!) // make it nicer to use
  guard req.handler == "de.zeezide.mustache" else { return DECLINED }
  
  req.contentType = "text/html; charset=ascii"
  guard let fn = req.filename else { return HTTP_NOT_FOUND }
  
  guard let template = try? String(contentsOfFile: fn) else {
    return HTTP_INTERNAL_SERVER_ERROR
  }
  req.puts(MustacheParser().parse(string: template).render(object: sampleDict))
  return OK
}

What this does is it loads a Mustache template located in the Apache documents directory. It then resolves the template from some Swift dictionary and returns the result to the browser. Note that the file lookup and all that is managed by other Apache modules, this handler is just invoked for Mustache templates (as configured in our apache.conf).

Remember that this is just a proof of concept. Quite likely you'd want some wrapper library making the Apache API a little 'Swiftier'. Also remember that you can use this not only to deliver dynamic content, but you can also use it to add new authentication modules to Apache, or write new filter modules (say one which converts XML to JSON on demand).

Who

mod_swift is brought to you by The Always Right Institute and ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of. We don't like people who are wrong.

There is a #mod_swift channel on the Noze.io Slack.