Skip to content

Commit

Permalink
Merge pull request #14 from radu-matei/toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
radu-matei committed Jan 27, 2022
2 parents 299cfc5 + 92826dd commit ffaa969
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 36 deletions.
15 changes: 7 additions & 8 deletions crates/http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use hyper::{
service::{make_service_fn, service_fn},
Body, Request, Response, Server,
};
use spin_http_v01::{Method, SpinHttpV01, SpinHttpV01Data};
use std::{net::SocketAddr, str::FromStr};
use std::{sync::Arc, time::Instant};
use spin_http::{Method, SpinHttp, SpinHttpData};
use std::{net::SocketAddr, str::FromStr, sync::Arc, time::Instant};
use url::Url;
use wasmtime::{Instance, Store};

wit_bindgen_wasmtime::import!("crates/http/spin_http_v01.wit");
wit_bindgen_wasmtime::import!("wit/ephemeral/spin-http.wit");

type ExecutionContext = spin_engine::ExecutionContext<SpinHttpV01Data>;
type RuntimeContext = spin_engine::RuntimeContext<SpinHttpV01Data>;
type ExecutionContext = spin_engine::ExecutionContext<SpinHttpData>;
type RuntimeContext = spin_engine::RuntimeContext<SpinHttpData>;

#[derive(Clone)]
pub struct HttpEngine(pub Arc<ExecutionContext>);
Expand All @@ -40,7 +39,7 @@ impl HttpEngine {
instance: Instance,
req: Request<Body>,
) -> Result<Response<Body>, Error> {
let r = SpinHttpV01::new(&mut store, &instance, |host| host.data.as_mut().unwrap())?;
let r = SpinHttp::new(&mut store, &instance, |host| host.data.as_mut().unwrap())?;

let (parts, bytes) = req.into_parts();
let bytes = hyper::body::to_bytes(bytes).await?.to_vec();
Expand All @@ -56,7 +55,7 @@ impl HttpEngine {
// let params: &Vec<(&str, &str)> = &params.into_iter().map(|(k, v)| (&**k, &**v)).collect();
let params = &Vec::new();

let req = spin_http_v01::Request {
let req = spin_http::Request {
method,
uri,
headers,
Expand Down
8 changes: 4 additions & 4 deletions crates/http/tests/rust-http-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use spin_http_v01::{Request, Response};
use spin_http::{Request, Response};

wit_bindgen_rust::export!("../../spin_http_v01.wit");
wit_bindgen_rust::export!("../../../../wit/ephemeral/spin-http.wit");

struct SpinHttpV01 {}
struct SpinHttp {}

impl spin_http_v01::SpinHttpV01 for SpinHttpV01 {
impl spin_http::SpinHttp for SpinHttp {
fn handler(req: Request) -> Response {
let body = Some(
format!(
Expand Down
11 changes: 5 additions & 6 deletions crates/redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use anyhow::Result;
use async_trait::async_trait;
use futures::StreamExt;
use redis::Client;
use spin_redis_trigger_v01::*;
use spin_redis_trigger::{SpinRedisTrigger, SpinRedisTriggerData};
use std::{sync::Arc, time::Instant};
use wasmtime::{Instance, Store};

wit_bindgen_wasmtime::import!("crates/redis/wit/spin_redis_trigger_v01.wit");
wit_bindgen_wasmtime::import!("wit/ephemeral/spin-redis-trigger.wit");

type ExecutionContext = spin_engine::ExecutionContext<SpinRedisTriggerV01Data>;
type RuntimeContext = spin_engine::RuntimeContext<SpinRedisTriggerV01Data>;
type ExecutionContext = spin_engine::ExecutionContext<SpinRedisTriggerData>;
type RuntimeContext = spin_engine::RuntimeContext<SpinRedisTriggerData>;

#[derive(Clone)]
pub struct RedisEngine(pub Arc<ExecutionContext>);
Expand All @@ -34,8 +34,7 @@ impl RedisEngine {
instance: Instance,
payload: &[u8],
) -> Result<()> {
let r =
SpinRedisTriggerV01::new(&mut store, &instance, |host| host.data.as_mut().unwrap())?;
let r = SpinRedisTrigger::new(&mut store, &instance, |host| host.data.as_mut().unwrap())?;

let _ = r.handler(&mut store, payload)?;
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions crates/redis/tests/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::str::{from_utf8, Utf8Error};

use spin_redis_trigger_v01::*;
use spin_redis_trigger::*;

wit_bindgen_rust::export!("../../wit/spin_redis_trigger_v01.wit");
wit_bindgen_rust::export!("../../../../wit/ephemeral/spin-redis-trigger.wit");

struct SpinRedisTriggerV01 {}
struct SpinRedisTrigger {}

impl spin_redis_trigger_v01::SpinRedisTriggerV01 for SpinRedisTriggerV01 {
impl spin_redis_trigger::SpinRedisTrigger for SpinRedisTrigger {
fn handler(payload: Payload) -> Result<(), Error> {
println!("Message: {}", from_utf8(&payload)?);
Ok(())
Expand Down
11 changes: 0 additions & 11 deletions crates/redis/wit/spin_redis_trigger_v01.wit

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,3 @@ enum http-error {
request-error,
runtime-error,
}

// The entrypoint for an HTTP handler.
handler: function(req: request) -> response
4 changes: 4 additions & 0 deletions wit/ephemeral/spin-http.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use * from http-types

// The entrypoint for an HTTP handler.
handler: function(req: request) -> response
4 changes: 4 additions & 0 deletions wit/ephemeral/spin-redis-trigger.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use * from types

// The entrypoint for a Redis handler.
handler: function(payload: payload) -> expected<_, error>
7 changes: 7 additions & 0 deletions wit/ephemeral/types.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// General purpose error.
enum error {
success,
error,
}

type payload = list<u8>
15 changes: 15 additions & 0 deletions wit/ephemeral/wasi-cache.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// A simple cache interface.

use * from types

// Set the payload for the given key.
// Implementations may choose to ignore the time-to-live (in seconds) argument.
// TODO (@radu-matei): perhaps return the number of bytes written?
set: function(key: string, value: payload, ttl: option<u32>) -> expected<_, error>

// Get the payload stored in the cache for the given key.
// If not found, return a success result with an empty payload.
get: function(key: string) -> expected<payload, error>

// Delete the cache entry for the given key.
delete: function(key: string) -> expected<_, error>
20 changes: 20 additions & 0 deletions wit/ephemeral/wasi-log.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Log levels.
enum level {
trace,
debug,
info,
warn,
error,
fatal
}

// General log function.
log: function(msg: string, lvl: level)

// Specialized log functions.
trace: function(msg: string)
debug: function(msg: string)
info: function(msg: string)
warn: function(msg: string)
error: function(msg: string)
fatal: function(msg: string)
4 changes: 4 additions & 0 deletions wit/ephemeral/wasi-outbound-http.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use * from http-types

// Send an HTTP request and return a response or a potential error.
request: function(req: request) -> expected<response, http-error>

0 comments on commit ffaa969

Please sign in to comment.