Quickstart for Swift on Fastly Compute@Edge

Overview

Fastly Compute@Edge Quickstart for Swift

This is an example Fastly Compute@Edge app using Andrew Barba's Swift Compute Runtime SDK.

It's designed to demonistrate some simple functionality:

  1. Routing
  2. Accessing request object (method, url, headers)
  3. Writing repsponse (JSON and text)
  4. Setting response status (200 vs. 404)

See the SDK Reference for more info.

Synopsis

The following is the code for this demo.

import Compute

struct Response: Codable {
    let version: Int
    let message: String
}

@main
struct ComputeApp {
    static func main() async throws {
        try await onIncomingRequest { req, res in
            let logger = try Logger(name: "QuickstartLog")
            switch (req.method, req.url.path) {
            case (.post, "/quickstart"):
                if let accept = req.headers.get("Accept") {
                    if accept.caseInsensitiveCompare("text/plain") == .orderedSame {
                        try logger.write("swift quickstart responded with 200")
                        try await res.status(200).send("Hello, Swift World!")
                        return
                    }
                }
                let content = Response(version: 1, message: "Hello, Swift World!")
                try logger.write("swift quickstart responded with 200")
                try await res.status(200).send(content)
            default:
                try logger.write("swift quickstart responded with 404")
                try await res.status(404).send()
            }
        }
    }
}

Prerequisites

  1. SwiftWasm toolchain
  2. Fastly CLI

Usage

$ fastly compute init
$ /Library/Developer/Toolchains/swift-wasm-5.6.0-RELEASE.xctoolchain/usr/bin/swift build --triple wasm32-unknown-wasi --product FastlyComputeQuickstart -c release
$ fastly compute pack --wasm-binary=./.build/release/FastlyComputeQuickstart.wasm
$ fastly compute deploy
You might also like...
A Swift port of the Cassowary linear constraint solver

Cassowary Swift A Swift port of the Cassowary linear constraints solver. Tested on OS X, iOS and Linux. Example usage let solver = Solver() let left

An easy way to create and layout UI components for iOS (Swift version).
An easy way to create and layout UI components for iOS (Swift version).

Introduction Cupcake is a framework that allow you to easily create and layout UI components for iOS 8.0+. It use chaining syntax and provides some fr

Lightweight Swift framework for Apple's Auto-Layout
Lightweight Swift framework for Apple's Auto-Layout

I am glad to share with you a lightweight Swift framework for Apple's Auto-Layout. It helps you write readable and compact UI code using simple API. A

An Impressive Auto Layout DSL for  iOS, tvOS & OSX. & It is written in pure swift.
An Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.

KVConstraintKit KVConstraintKit is a DSL to make easy & impressive Auto Layout constraints on iOS, tvOS & OSX with Swift Installation Using CocoaPods

A compact but full-featured Auto Layout DSL for Swift
A compact but full-featured Auto Layout DSL for Swift

Mortar allows you to create Auto Layout constraints using concise, simple code statements. Use this: view1.m_right |=| view2.m_left - 12.0 Instead of:

The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. Objective-C and Swift compatible.
The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. Objective-C and Swift compatible.

The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. PureLayout extends UIView/NSView, NSArray, and NSLayoutConstrai

A Swift Autolayout DSL for iOS & OS X
A Swift Autolayout DSL for iOS & OS X

SnapKit is a DSL to make Auto Layout easy on both iOS and OS X. ⚠️ To use with Swift 4.x please ensure you are using = 4.0.0 ⚠️ ⚠️ To use with Swift

Auto Layout In Swift Made Easy

Swiftstraints Swiftstraints can turn verbose auto-layout code: let constraint = NSLayoutConstraint(item: blueView, attr

swiftUIviews is an online collection of beautifly designed swiftUIViews by the swift community
swiftUIviews is an online collection of beautifly designed swiftUIViews by the swift community

swiftUIViews | 👋 swiftUIviews is an online collection of beautifly designed swiftUIViews by the swift community. Feelin like contributing? Follow the

Releases(v0.1.0)
Owner
John Wang
Creator, Developer, PM
John Wang
VidyoPlatform Basic CustomLayouts Reference App for iOS (Swift)VidyoPlatform Basic CustomLayouts Reference App for iOS (Swift)

VidyoPlatform Basic CustomLayouts Reference App for iOS (Swift) VidyoPlatform reference application highlighting how to integrate video chat into a na

Taras Melko 0 Nov 19, 2021
100-days-swift-project-8 - The eighth project from 100 days of Swift course

7 Swifty Words This is the eighth project from Hacking With Swift 100 days of Sw

Bruno Guirra 0 Jan 24, 2022
SwiftLanguageWeather-master - Swift Language Weather is an iOS weather app developed in Swift 4

Swift Language Weather SwiftWeather has renamed to Swift Language Weather. Becau

Kushal Shingote 1 Feb 5, 2022
Fancy Swift implementation of the Visual Format Language (experimental and doesn't work with the recent version of Swift)

VFLToolbox Autolayout is awesome! VFL a.k.a Visual Format Language is even more awesome because it allows you to shorten constraints setting code. The

0xc010d 144 Jun 29, 2022
BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift!

BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift! Deprecated BrickKit is being phased out at Wayfair, and the

Wayfair Tech – Archive 608 Sep 15, 2022
FlexLayout adds a nice Swift interface to the highly optimized facebook/yoga flexbox implementation. Concise, intuitive & chainable syntax.

FlexLayout adds a nice Swift interface to the highly optimized Yoga flexbox implementation. Concise, intuitive & chainable syntax. Flexbox is an incre

layoutBox 1.7k Dec 30, 2022
A powerful Swift programmatic UI layout framework.

Build dynamic and beautiful user interfaces like a boss, with Swift. Neon is built around how user interfaces are naturally and intuitively designed.

Mike 4.6k Dec 26, 2022
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]

Extremely Fast views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainabl

layoutBox 2.1k Dec 22, 2022
Simple static table views for iOS in Swift.

Simple static table views for iOS in Swift. Static's goal is to separate model data from presentation. Rows and Sections are your “view models” for yo

Venmo 1.3k Jan 5, 2023
A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:

Cartography ?? ?? Using Cartography, you can set up your Auto Layout constraints in declarative code and without any stringly typing! In short, it all

Robb Böhnke 7.3k Jan 4, 2023