Skip to content

ApacheExpress/ApacheExpress3

Repository files navigation

ApacheExpress3

Apache 2 Swift4 Swift5 macOS tuxOS Travis

ApacheExpress allows you to quickly write Server Side Swift applications which run as a native module within the Apache Web Server.

TODO: Cleanup the README. mods_expressdemo

A very basic request/response handler:

public func ApacheMain(_ cmd: OpaquePointer) {
  let app = ApacheExpress.express(cmd)
  app.server.onRequest { req, res in
    res.writeHead(200, [ "Content-Type": "text/html" ])
    try res.end("<h1>Hello World</h1>")
  }
}

Connect like reusable middleware:

public func ApacheMain(_ cmd: OpaquePointer) {
  let app = ApacheExpress.express(cmd)
  
  app.use { req, res, next in
    console.info("Request is passing Connect middleware ...")
    res.setHeader("Content-Type", "text/html; charset=utf-8")
    next()
  }
  
  app.use("/connect") { req, res, next in
    try res.write("<p>This is a random cow:</p><pre>")
    try res.write(vaca())
    try res.write("</pre>")
    res.end()
  }
}

And the Apache Express is about to leave:

public func ApacheMain(_ cmd: OpaquePointer) {
  let app = apache.express(cookieParser(), session())

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

  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)
  }
}

Yes. All that is running within Apache.

Documentation

ApacheExpress documentation can be found at: docs.apacheexpress.io.

Who

ApacheExpress is brought to you by ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of.

There is an #apacheexpress channel on the Noze.io Slack.