Permalink
Fetching contributors…
Cannot retrieve contributors at this time
88 lines (71 sloc) 2.55 KB

mod_swift - mods_expressdemo

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_expressdemo

ApacheExpress is a port of the Noze.io Express and Connect frameworks, as well as some associated modules, to the mod_swift Apache API.

mods_expressdemo is a demo for those. It demos Mustache template rendering, form values, cookies, sessions, JSON support, cookie access, and most importantly: 🐄.

Well, that:

app.get("/express/form") { _, res, _ in
  try res.render("form")
}
app.post("/express/form") { req, res, _ in
  let user = req.body[string: "u"]
  
  let options : [ String : Any ] = [
    "user"      : user,
    "nouser"    : user.isEmpty,
    "viewCount" : req.session["viewCount"] ?? 0
  ]
  try res.render("form", options)
}

app.get("/express/json") { _, res, _ in
  try res.json([
    [ "firstname": "Donald",   "lastname": "Duck" ],
    [ "firstname": "Dagobert", "lastname": "Duck" ]
  ])
}

app.get("/express/cookies") { req, res, _ in
  try res.json(req.cookies)  // returns all cookies as JSON
}

app.get("/express/cows") { req, res, _ in
  let cow = cows.vaca()
  try res.send("<html><body><pre>\(cow)</pre></body></html>")
}

app.get("/express/") { req, res, _ in
  let tagline = arc4random_uniform(UInt32(taglines.count))
  
  let values : [ String : Any ] = [
    "tagline"     : taglines[Int(tagline)],
    "viewCount"   : req.session["viewCount"] ?? 0,
    "cowOfTheDay" : cows.vaca()
  ]
  try res.render("index", values)
}

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.