https://github.com/jthomas/openwhiskaction
Swift Package for wrapping Swift functions to support execution as OpenWhisk Actions
https://github.com/jthomas/openwhiskaction
openwhisk serverless swift
Last synced: about 1 year ago
JSON representation
Swift Package for wrapping Swift functions to support execution as OpenWhisk Actions
- Host: GitHub
- URL: https://github.com/jthomas/openwhiskaction
- Owner: jthomas
- License: apache-2.0
- Created: 2017-06-15T16:25:53.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-03-27T16:13:15.000Z (about 8 years ago)
- Last Synced: 2025-02-01T21:42:20.387Z (about 1 year ago)
- Topics: openwhisk, serverless, swift
- Language: Swift
- Size: 17.6 KB
- Stars: 0
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OpenWhiskAction
Swift Package for wrapping Swift functions to support execution as OpenWhisk actions
## installation
Install the package by adding the dependency to `Package.swift`.
```swift
let package = Package(
name: "Action",
dependencies: [
.Package(url: "https://github.com/jthomas/OpenWhiskAction.git", majorVersion: 0)
]
)
```
## usage
This package exposes a public function (`OpenWhiskAction` ) that should be called with a function reference (`([String: Any]) -> [String: Any])`) as a named parameter (`main`). The callback will be executed with the invocation parameters. Returned values will be serialised as the invocation response.
```swift
import OpenWhiskAction
func hello(args: [String:Any]) -> [String:Any] {
if let name = args["name"] as? String {
return [ "greeting" : "Hello \(name)!" ]
} else {
return [ "greeting" : "Hello stranger!" ]
}
}
OpenWhiskAction(main: hello)
```
This source file should be compiled into a binary for deployment to OpenWhisk.
## compiling with docker
OpenWhisk actions for the Swift runtime use a [custom Docker image]() as the runtime environment. Compiling the application binary using this image will ensure it is compatible with the platform runtime.
This command will run the `swift build` system within a container from this image. The host filesystem is mounted into the container at `/swift-package`. Binaries and other build artifacts will be available in `./.build/release/` after the command has executed.
```
docker run --rm -it -v $(pwd):/swift-package openwhisk/swift3action bash -e -c "cd /swift-package && swift build -v -c release"
```
## deploying to openwhisk
OpenWhisk actions can be created from a zip file containing the action artifacts. The zip file will be expanded prior to execution. In the Swift environment, the compiled Swift binary executed by the platform is expected to be at `./.build/release/Action`.
If an action is deployed from a zip file which contains this file, the runtime will execute this binary rather than compiling a new binary from source code within the zip file.
```sh
$ zip action.zip .build/release/Action
adding: .build/release/Action (deflated 67%)
$ wsk action create swift-action --kind swift:3 action.zip
ok: created action swift-action
$ wsk action invoke --blocking --result -p name "Bernie Sanders" swift-action
{
"greeting": "Hello Bernie Sanders!"
}
```