Giter VIP home page Giter VIP logo

http's Introduction

HTTP

Asynchronous HTTP server/client

Package.swift

.package(url: "https://github.com/swiftstack/http.git", .branch("dev"))

Quick Start [source]

First we need to create a root fiber and run the event loop:

// main.swift
import HTTP
import Async

async {
    // entry point for async http server
}

loop.run()

Simple server running "http://localhost:8080":

// async body
let server = try Server(host: "localhost", port: 8080)
try registerRoutes(in: server)
try server.start()

Simple get route:

// routes.swift

import HTTP

func registerRoutes(in server: Server) throws {
    server.route(get: "/hello") {
        return "Hey there!"
    }
}

At this point our server is ready to be friendly:

$ swift run main
$ curl http://localhost:8080/hello
> Hey there!

More advanced version:

struct User: Decodable {
    let name: String
}

func helloHandler(user: User) -> String {
    return "Hello \(user.name)"
}

let application = Application(basePath: "/v1")
application.route(get: "/hello", to: helloHandler)
server.addApplication(application)

Most advanced version:

struct User: Decodable {
    let name: String
}

struct Greeting: Encodable {
    let message: String
}

func helloHandler(user: User) -> Greeting {
    return .init(message: "Hello, \(user.name)!")
}

struct SwiftMiddleware: Middleware {
    static func chain(with handler: @escaping RequestHandler) -> RequestHandler {
        return { request in
            if request.url.query?["name"] == "swift" {
                return Response(string: "๐Ÿค˜")
            }
            return try handler(request)
        }
    }
}

let application = Application(basePath: "/v2")

application.route(
    get: "/hello",
    through: [SwiftMiddleware.self],
    to: helloHandler)

server.addApplication(application)
$ swift run main
$ curl http://localhost:8080/v2/hello?name=swift
> ๐Ÿค˜

The same route using human readable urls

struct User: Decodable {
    let name: String
}

struct Greeting: Encodable {
    let message: String
}

func helloHandler(user: User) -> Greeting {
    return .init(message: "Hello, \(user.name)!")
}

struct SwiftMiddleware: Middleware {
    static func chain(with handler: @escaping RequestHandler) -> RequestHandler {
        return { request in
            if request.url.path.split(separator: "/").last == "swift" {
                return Response(string: "๐Ÿค˜")
            }
            return try handler(request)
        }
    }
}

let application = Application(basePath: "/v3")

application.route(
    get: "/hello/:name",
    through: [SwiftMiddleware.self],
    to: helloHandler)

server.addApplication(application)
$ swift run main
$ curl http://localhost:8080/v3/hello/swift
> ๐Ÿค˜

http's People

Contributors

tonyfreeman avatar mahadshahib avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.