Skip to content
forked from vapor/http

HTTP, WebSockets, Streams, SMTP, etc. Core transport layer used in https://github.com/vapor/vapor

License

Notifications You must be signed in to change notification settings

hungrxyz/engine

Β 
Β 

Repository files navigation

Engine

Swift Build Status CircleCI Code Coverage Codebeat Slack Status

Engine is a collection of low level transport protocols implemented in pure Swift intended for use in server side and client side applications. It is used as the core transport layer in Vapor.

🐧 Linux Ready

Deploy

πŸ“¦ Examples

Check out the HTTP, SMTP, and WebSockets examples in Sources/. You can clone this repository and run them on your computer.

πŸ“˜ Overview

HTTP.Client

import HTTP

let response = try Client<TCPClientStream>.get("https://pokeapi.co/api/v2/pokemon/")
print(response)

HTTP.Server

import HTTP

final class MyResponder: Responder {
    func respond(to request: Request) throws -> Response {
        let body = "Hello World".makeBody()
        return Response(body: body)
    }
}

let server = try Server<TCPServerStream, Parser<Request>, Serializer<Response>>(port: port)

print("visit https://localhost:\(port)/")
try server.start(responder: MyResponder()) { error in
    print("Got error: \(error)")
}

WebSocket Client

import Engine
import WebSockets

try WebSocket.connect(to: url) { ws in
    print("Connected to \(url)")

    ws.onText = { ws, text in
        print("[event] - \(text)")
    }

    ws.onClose = { ws, _, _, _ in
        print("\n[CLOSED]\n")
    }
}

WebSocket Server

import HTTP
import WebSockets

final class MyResponder: Responder {
    func respond(to request: Request) throws -> Response {
        return try request.upgradeToWebSocket { ws in
            print("[ws connected]")

            ws.onText = { ws, text in
                print("[ws text] \(text)")
                try ws.send("πŸŽ™ \(text)")
            }

            ws.onClose = { _, code, reason, clean in
                print("[ws close] \(clean ? "clean" : "dirty") \(code?.description ?? "") \(reason ?? "")")
            }
        }
    }
}

let server = try Server<TCPServerStream, Parser<Request>, Serializer<Response>>(port: port)

print("Connect websocket to https://localhost:\(port)/")
try server.start(responder: MyResponder()) { error in
    print("Got server error: \(error)")
}

SMTP

import SMTP

let credentials = SMTPCredentials(
    user: "server-admin-login",
    pass: "secret-server-password"
)

let from = EmailAddress(name: "Password Reset",
                        address: "[email protected]")
let to = "[email protected]"
let email: Email = Email(from: from,
                         to: to,
                         subject: "Vapor SMTP - Simple",
                         body: "Hello from Vapor SMTP πŸ‘‹")

let client = try SMTPClient<TCPClientStream>.makeGmailClient()
try client.send(email, using: credentials)

Emails using the Gmail client are blocked by default unless you enable access for less-secure apps in your Gmail account settings: https://support.google.com/accounts/answer/6010255?hl=en-GB

πŸ› Architecture

HTTP.Server

The HTTPServer is responsible for listening and accepting remote connections, then relaying requests and responses between the received connection and the responder.

HTTP.Client

The HTTPClient is responsible for establishing remote connections and relaying requests and responses between the remote connection and the caller.

πŸ“– Documentation

Visit the Vapor web framework's documentation for instructions on how to use this package.

πŸ’§ Community

Join the welcoming community of fellow Vapor developers in slack.

πŸ”§ Compatibility

This package has been tested on macOS and Ubuntu.

About

HTTP, WebSockets, Streams, SMTP, etc. Core transport layer used in https://github.com/vapor/vapor

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 100.0%