Skip to content

Commit

Permalink
chore: add http-file-server example
Browse files Browse the repository at this point in the history
  • Loading branch information
connorsmith256 committed Nov 30, 2023
1 parent e4a82ea commit d69d10b
Show file tree
Hide file tree
Showing 9 changed files with 3,291 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/build-http-file-server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build and test HTTP File Server
on:
pull_request:
branches: [main]
paths:
- "http-file-server/rust/**"

env:
working-directory: "./http-file-server"
jobs:
build_and_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: wasmcloud/common-actions/install-wash@main
- name: Add wasm32-unknown-unknown
run: rustup target add wasm32-unknown-unknown
- name: Build actor
run: |
wash new actor --path rust hello
wash build -p hello
working-directory: ${{ env.working-directory }}
- name: Check formatting
run: cargo fmt -- --check
shell: bash
working-directory: ${{ env.working-directory }}/hello

- name: Check lints with clippy
run: |
rustup component add clippy
cargo clippy
working-directory: ${{ env.working-directory }}/hello
77 changes: 77 additions & 0 deletions .github/workflows/release-http-file-server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Release HTTP File Server to GHCR
on:
workflow_dispatch:
push:
branches: [main]
paths:
- "http-file-server/**"
tags:
- "http-file-server-v*"
env:
# For the release action, you'll have to set the following variables
WASH_ISSUER_KEY: ${{ secrets.COSMONIC_ACCOUNT_OFFICIAL }}
WASH_SUBJECT_KEY: ${{ secrets.AWESOME_COSMONIC_HELLO_WORLD_KEY }}
working-directory: "./http-file-server"
jobs:
build_signed_actor:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: wasmcloud/common-actions/install-wash@main
- name: Add wasm32-unknown-unknown
run: rustup target add wasm32-unknown-unknown
working-directory: ${{ env.working-directory }}/rust
# Once you've written unit tests for your actor, you can uncomment
# the two lines below to automatically run tests
# - name: Test actor
# run: cargo test --target x86_64-unknown-linux-gnu -- --nocapture
- name: Build and sign wasmCloud actor
env:
WASH_ISSUER_KEY: ${{ env.WASH_ISSUER_KEY }}
WASH_SUBJECT_KEY: ${{ env.WASH_SUBJECT_KEY }}
run: wash build
shell: bash
working-directory: ${{ env.working-directory }}
- name: Upload signed actor to GH Actions
uses: actions/upload-artifact@v3
with:
name: wasmcloud-actor
path: ${{ env.working-directory }}/hello.tar

artifact_release:
needs: build_signed_actor
runs-on: ubuntu-latest
steps:
- uses: wasmcloud/common-actions/install-wash@main
- name: Download signed actor
uses: actions/download-artifact@v3
with:
name: wasmcloud-actor
path: ${{ env.working-directory }}
- name: Extract files from tar file
run: tar -xvf hello.tar
shell: bash
working-directory: ${{ env.working-directory }}
- name: Determine actor name
run: |
echo "actor-name=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[].name')" >> $GITHUB_ENV
working-directory: ${{ env.working-directory }}/hello
- name: Determine actor version
if: startswith(github.ref, 'refs/tags/') # Only run on tag push
run: |
echo "actor-version=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[].version')" >> $GITHUB_ENV
working-directory: ${{ env.working-directory }}/hello
- name: Determine actor version (main)
if: ${{ !startswith(github.ref, 'refs/tags/') }}
run: |
echo "actor-version=latest" >> $GITHUB_ENV
working-directory: ${{ env.working-directory }}/hello
- name: Push actor to GHCR
uses: wasmcloud/common-actions/oci-artifact-release@main
with:
artifact-path: ${{ env.working-directory }}/hello/build/${{ env.actor-name }}_s.wasm
oci-url: ${{ secrets.GHCR_PUSH_URL }}
oci-repository: ${{ env.actor-name }}
oci-version: ${{ env.actor-version }}
oci-username: ${{ github.actor }}
oci-password: ${{ secrets.GITHUB_TOKEN }}
42 changes: 42 additions & 0 deletions http-file-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# HTTP File Server

This actor (we like to call it "Little Blobby Tables") is a simple file server showing the basic
CRUD operations of the `wasmcloud:blobstore` contract.

Not only is this actor an example, it is also a fully-functional, HTTP-based fileserver that can be
fronted with any HTTP server implementation and any blobstore implementation (i.e. you could store
the uploaded files on a filesystem or in an s3 compatible store). It also has a full example of
integration testing for the actor

## Required Capability Claims

1. `wasmcloud:httpserver` to receive http requests
2. `wasmcloud:blobstore` to save the image to a blob
3. `wasmcloud:builtin:logging` so the actor can log

## Running this example

Click on the button below to launch the XKCD Generator on Cosmonic Now:

[![Deploy on Cosmonic](https://cosmonic.com/badge/deploy.svg)](https://new.cosmonic.app/?yaml=https://raw.githubusercontent.com/cosmonic/awesome-cosmonic/http-file-server/http-file-server.wadm.yaml)

Once everything is up and running, you can run through all of the operations by following the
annotated commands below:

```bash
export MY_WORMHOLE="your-wormhole-goes-here.cosmonic.app"

# to upload a file
export MESSAGE="Hello there\!"
curl -w "%{http_code}" -H 'Content-Type: text/plain' "https://$MY_WORMHOLE.cosmonic.app/myfile.txt" -d "$MESSAGE"

# to download a file
curl "https://$MY_WORMHOLE.cosmonic.app/myfile.txt"

# to edit a file, post again
export MESSAGE="$MESSAGE General Kenobi\!"
curl -w "%{http_code}" -H 'Content-Type: text/plain' "https://$MY_WORMHOLE.cosmonic.app/myfile.txt" -d "$MESSAGE"

# download the file again
curl "https://$MY_WORMHOLE.cosmonic.app/myfile.txt"
```
43 changes: 43 additions & 0 deletions http-file-server/http-file-server.wadm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This is a full example of how to run the blobby example.
# This requires you to have WADM running: https://github.com/wasmCloud/wadm/
# You can deploy this example with a simple command:
#
# `wash app deploy wadm.yaml`

apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: blobby
annotations:
version: v0.0.1
description: "wasmCloud HTTP Server"
spec:
components:
- name: blobby
type: actor
properties:
image: wasmcloud.azurecr.io/blobby:0.2.0
traits:
- type: spreadscaler
properties:
replicas: 1
- type: linkdef
properties:
target: httpserver
- type: linkdef
properties:
target: blobstore
values:
ROOT: "/tmp"

# Capability Providers
- name: httpserver
type: capability
properties:
image: cosmonic.azurecr.io/httpserver_wormhole:0.6.2
contract: wasmcloud:httpserver
- name: blobstore
type: capability
properties:
image: wasmcloud.azurecr.io/blobstore_fs:0.3.1
contract: wasmcloud:blobstore
2 changes: 2 additions & 0 deletions http-file-server/rust/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-unknown-unknown"
Loading

0 comments on commit d69d10b

Please sign in to comment.