• This repository has been archived on 23/Nov/2021
  • Stars
    star
    703
  • Rank 64,412 (Top 2 %)
  • Language
    Swift
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⚠️ Historical HTTP API - please use https://github.com/swift-server/async-http-client instead

Swift Server Project HTTP APIs

⚠️ This project is unmaintained experimental legacy code. It has been obsoleted by SwiftNIO which contains the recommended HTTP API of the Swift Server Work Group.

It remains here for historical interest only.

Getting Started

Hello World

The following code implements a very simple "Hello World!" server:

import Foundation
import HTTP

func hello(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing { 
    response.writeHeader(status: .ok) 
    response.writeBody("Hello, World!") 
    response.done() 
    return .discardBody 
} 

let server = HTTPServer()
try! server.start(port: 8080, handler: hello)

RunLoop.current.run()

The hello() function receives a HTTPRequest that describes the request and a HTTPResponseWriter used to write a response.

Data that is received as part of the request body is made available to the closure that is returned by the hello() function. In the "Hello World!" example the request body is not used, so .discardBody is returned.

Echo Server

The following code implements a very simple Echo server that responds with the contents of the incoming request:

import Foundation
import HTTP

func echo(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing {
    response.writeHeader(status: .ok)
    return .processBody { (chunk, stop) in
        switch chunk {
        case .chunk(let data, let finishedProcessing):
            response.writeBody(data) { _ in
                finishedProcessing()
            }
        case .end:
            response.done()
        default:
            stop = true
            response.abort()
        }
    }
}

let server = HTTPServer()
try! server.start(port: 8080, handler: echo)

RunLoop.current.run()

As the Echo server needs to process the request body data and return it in the reponse, the echo() function returns a .processBody closure. This closure is called with .chunk when data is available for processing from the request, and .end when no more data is available.

Once any data chunk has been processed, finishedProcessing() should be called to signify that it has been handled.

When the response is complete, response.done() should be called.

API Documentation

Full Jazzy documentation of the API is available here:
https://swift-server.github.io/http/

Contributing

Feedback

We are actively seeking feedback on this prototype and your comments are extremely valuable. If you have any comments on the API design, the implementation, or any other aspects of this project, please email the swift-server-dev mailing list.

Writing Code

We also welcome code contributions. If you are developing on macOS, you may want to work within Xcode. This project uses the Swift Package Manager. To work on this project within Xcode you can run the Swift Package Manager command swift package generate-xcodeproj to generate an .xcodeproj to work on within Xcode.

Acknowledgements

This project is based on an inital proposal from @weissi on the swift-server-dev mailing list:
https://lists.swift.org/pipermail/swift-server-dev/Week-of-Mon-20170403/000422.html

More Repositories

1

swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
Swift
1,132
star
2

async-http-client

HTTP client library built on SwiftNIO
Swift
917
star
3

vscode-swift

Visual Studio Code Extension for Swift
TypeScript
622
star
4

swift-service-lifecycle

Cleanly startup and shutdown server application, freeing resources in order before exiting.
Swift
395
star
5

work-group

⚠️ Historical workgroup organisation
352
star
6

swiftly

A Swift toolchain installer and manager, written in Swift.
Swift
337
star
7

swift-backtrace

💥 Backtraces for Swift on Linux and Windows
C
295
star
8

guides

Guides for building, debugging and deploying Swift Server applications
258
star
9

sswg

Swift Server Working Group (SSWG)
181
star
10

swift-prometheus

Prometheus client library for Swift
Swift
144
star
11

RediStack

Non-blocking, event-driven Swift client for Redis.
Swift
142
star
12

swift-webauthn

A Swift library for implementing the WebAuthn spec
Swift
127
star
13

swift-openapi-vapor

Vapor Bindings for the OpenAPI Generator
Swift
79
star
14

swift-kafka-client

Swift
77
star
15

swift-devcontainer-template

Visual Studio Code Development Container for Swift
Shell
54
star
16

swift-openapi-async-http-client

AsyncHTTPClient transport for Swift OpenAPI Generator.
Swift
54
star
17

security

⚠️ Historical TLS API - please use SwiftNIO instead
Swift
49
star
18

swift-aws-lambda-events

Swift implementation of AWS Lambda Events
Swift
48
star
19

swift-openapi-hummingbird

Hummingbird transport for OpenAPI generator
Shell
27
star
20

swift-memcache-gsoc

Swift
15
star
21

sswg-collection

Dockerfile
11
star
22

swift-openapi-lambda

An AWS Lambda transport for Swift OpenAPI
Swift
8
star
23

swift-service-cache

caches for swift services
6
star
24

swift-etcd-client-gsoc

Swift
5
star