mod_swift allows you to write native modules for the Apache Web Server in the Swift programming language.

Server Side Swift the right way.

We recommend using it with the Homebrew Apache 2.6 on macOS:

brew install httpd

or the same on Linux (tested on various Ubuntu versions).

Shows us some code!

We provide a few examples: a ‘raw’ Apache Module, a demo for the bundled Express, as well as a TodoMVC backend. But here you go, the “standard” Node example, a HelloWorld webpage:

func expressMain() {
  apache.onRequest { req, res in
    res.writeHead(200, [ "Content-Type": "text/html" ])
    try res.end("<h1>Hello World</h1>")
  }
}

Middleware using Express features like Mustache templates or JSON support:

let app = apache.express(bodyParser.urlencoded(), 
                         cookieParser(), session())
  
app.get("/express/cookies") { req, res, _ in
  try res.json(req.cookies)  // returns all cookies as JSON
}

app.get("/express/") { req, res, _ in
  let tagline = arc4random_uniform(taglines.count)

  let values : [ String : Any ] = [
    "tagline"     : taglines[tagline],
    "viewCount"   : req.session["viewCount"] ?? 0,
    "cowOfTheDay" : cows.vaca()
  ]
  try res.render("index", values)
}

Access a SQL database using Apache mod_dbd:

guard let con = req.dbdAcquire()                 else { return ... }
guard let res = con.select("SELECT * FROM pets") else { return ... }

while let row = res.next() {
  req.puts("<li>\(row[0])</li>")
}

The documentation can be found here: docs.mod-swift.org.


🏎 mod_swift for M1 Homebrew

I am pleased to announce that mod_swift now runs w/ Xcode 13.3.1, on macOS 12 M1 Macs, with the native Homebrew installed in /opt! A neat way to write Apache modules in Swift 5.

May 8, 2022 Read More

🍬 mod_swift for Swift 5

I am pleased to announce that mod_swift now runs w/ Xcode 10.2 and Swift 5! A neat way to write Apache modules in Swift 3, 4 or 5.

May 12, 2019 Read More