https://github.com/grokify/fastly-compute-quickstart-swift
Quickstart for Swift on Fastly Compute@Edge
https://github.com/grokify/fastly-compute-quickstart-swift
Last synced: 3 months ago
JSON representation
Quickstart for Swift on Fastly Compute@Edge
- Host: GitHub
- URL: https://github.com/grokify/fastly-compute-quickstart-swift
- Owner: grokify
- License: mit
- Created: 2022-06-23T18:39:22.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-07-16T04:56:34.000Z (almost 3 years ago)
- Last Synced: 2024-10-12T00:51:34.847Z (9 months ago)
- Language: Swift
- Homepage:
- Size: 6.84 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fastly Compute@Edge Quickstart for Swift
This is an example Fastly Compute@Edge app using [Andrew Barba's Swift Compute Runtime SDK](https://github.com/AndrewBarba/swift-compute-runtime).
It's designed to demonistrate some simple functionality:
1. Routing
1. Accessing request object (method, url, headers)
1. Writing repsponse (JSON and text)
1. Setting response status (200 vs. 404)See the [SDK Reference](https://compute-runtime.swift.cloud/documentation/compute/) for more info.
## Synopsis
The following is the code for this demo.
```swift
import Computestruct 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](https://book.swiftwasm.org/getting-started/setup.html)
1. [Fastly CLI](https://github.com/fastly/cli)## Usage
``` bash
$ 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
```