Skip to content

helje5/MicroExpress

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

µExpress

Swift 4 macOS tuxOS Apache 2 Travis

A micro server framework on top of the Swift Server API.

It adds an Express like API on top of the raw Swift Server API:

import MicroExpress

let app = Express()

app.get("/moo") { req, res, next in
  try res.send("Muhhh")
}
app.get("/json") { _, res, _ in
  try res.json([ "a": 42, "b": 1337 ])
}
app.get("/") { _, res, _ in
  try res.send("Homepage")
}

app.listen(1337)

This package is part of the Always Right Institute's blog series about the Swift Server Workgroup's offical Swift HTTP API.

Please checkout Part 2 of our blog series to learn what this is about. This is a tiny framework, for a more full featured, synchronous Express-like API in Swift, have a look at ExExpress (as used in ApacheExpress). Noze.io comes w/ an asynchronous variant.

Using the Package

Micro Hello World in 5 minutes:

$ mkdir MicroHelloWorld && cd MicroHelloWorld
$ swift package init --type executable

Update Package.swift to include the dependency:

// swift-tools-version:4.0
import PackageDescription

let package = Package(
  name: "MicroHelloWorld",
  dependencies: [
    .package(url: "https://github.com/AlwaysRightInstitute/MicroExpress.git", 
             branch: "branches/swift-server-http-api")
  ],
  targets: [
    .target(name: "MicroHelloWorld",
            dependencies: [ "MicroExpress" ])
  ]
)

Change the main.swift from print("Hello World") into:

import MicroExpress

let app = Express()

app.get("/") { req, res, next in
  try res.send("Hello World")
}

app.listen(1337)
$ swift build
$ swift run
Started server on port 1337 with 4 serial queues of each type and 8 accept sockets

Done. Access via: http://localhost:1337/

Building the Package

Xcode

Important: This creates a few schemes in the Xcode project. Make sure to select the right one when building & running.

$ swift package generate-xcodeproj
Fetching https://github.com/swift-server/http
Cloning https://github.com/swift-server/http
Resolving https://github.com/swift-server/http at 0.1.0
generated: ./MicroExpress.xcodeproj

$ open MicroExpress.xcodeproj

macOS /Linux Command Line

$ swift build
Fetching https://github.com/swift-server/http
Cloning https://github.com/swift-server/http
Resolving https://github.com/swift-server/http at 0.1.0
Compile CHTTPParser http_parser.c
Compile Swift Module 'HTTP' (11 sources)
Compile Swift Module 'MicroExpress' (9 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/MicroExpress

Linux via macOS Docker

$ docker run --rm \
  -v "${PWD}:/src" \
  -v "${PWD}/.docker.build:/src/.build" \
  swift:4.0.3 \
  bash -c 'cd /src && swift build'
Unable to find image 'swift:4.0.3' locally
4.0.3: Pulling from library/swift
8f7c85c2269a: Pull complete 
...
9783e1c76d2b: Pull complete 
Digest: sha256:6978675b95f749b54eab57163c663d45b25c431c6d50cb5b2983062a55cea3c6
Status: Downloaded newer image for swift:4.0.3
Compile CHTTPParser http_parser.c
Compile Swift Module 'HTTP' (11 sources)
Compile Swift Module 'MicroExpress' (9 sources)
Linking ./.build/x86_64-unknown-linux/debug/MicroExpress
Fetching https://github.com/swift-server/http
Cloning https://github.com/swift-server/http
Resolving https://github.com/swift-server/http at 0.1.0

$ file .docker.build/x86_64-unknown-linux/debug/MicroExpress
.docker.build/x86_64-unknown-linux/debug/MicroExpress: 
  ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked ...

Links

Who

MicroExpress 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.


ApacheExpress

mod_swift

ZeeQL

Noze.io

UXKit

About

A micro web server framework on top of the official Swift HTTP API

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors