Skip to content

Commit

Permalink
pre-kubecon patches (#226)
Browse files Browse the repository at this point in the history
* enabling cors for all

Signed-off-by: danbugs <[email protected]>

* updated slight version

Signed-off-by: danbugs <[email protected]>

* kubecon patch

Signed-off-by: danbugs <[email protected]>

Signed-off-by: danbugs <[email protected]>
  • Loading branch information
danbugs authored Oct 22, 2022
1 parent 95f60d6 commit 3a6486e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ wit-bindgen-wasmtime = { git = "https://github.com/Mossaka/wit-bindgen", rev = "
url = "2.2"
wit-error-rs = { git = "https://github.com/danbugs/wit-error-rs", rev = "05362f1a4a3a9dc6a1de39195e06d2d5d6491a5e" }
routerify = "3"
routerify-cors = "3"
crossbeam-utils = "0.8"
crossbeam-channel = "0.5"
slight-events-api = { path = "../events-api" }
Expand Down
16 changes: 11 additions & 5 deletions crates/http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use http::*;
use hyper::{Body, Server};
use routerify::ext::RequestExt;
use routerify::{Router, RouterBuilder, RouterService};
use routerify_cors::enable_cors_all;
use slight_common::{impl_resource, Buildable, Builder, Ctx, HostState};
use slight_events_api::ResourceMap;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
Expand Down Expand Up @@ -229,14 +230,15 @@ impl<T: Buildable + Send + Sync + 'static> http::Http for Http<T> {

// The outer builder is used to define the route paths, while creating a scope
// for the inner builder which passes states to the route handler.
let mut outer_builder: RouterBuilder<Body, anyhow::Error> =
Router::builder().data(instance_builder);
let mut outer_builder: RouterBuilder<Body, http::Error> = Router::builder()
.middleware(enable_cors_all())
.data(instance_builder);

// There is a one-to-one mapping between the outer router's scope and inner router builder.
let mut inner_routes = vec![];
for route in router.routes.iter() {
// per route state
let mut inner_builder: RouterBuilder<Body, anyhow::Error> = Router::builder();
let mut inner_builder: RouterBuilder<Body, http::Error> = Router::builder();
inner_builder = inner_builder.data(route.clone());
match route.method {
Methods::GET => {
Expand Down Expand Up @@ -289,7 +291,7 @@ impl<T: Buildable + Send + Sync + 'static> http::Http for Http<T> {

async fn handler<T: Buildable + Send + Sync + 'static>(
request: hyper::Request<Body>,
) -> Result<hyper::Response<Body>> {
) -> Result<hyper::Response<Body>, http::Error> {
log::debug!("received request: {:?}", &request);
let (parts, body) = request.into_parts();

Expand Down Expand Up @@ -331,7 +333,11 @@ async fn handler<T: Buildable + Send + Sync + 'static>(
// handler.handle_http(&mut store, req)
// }).await???;
// let rt = tokio::runtime::Handle::current();
let res = handler.handle_http(&mut store, req).await??;
let res = handler
.handle_http(&mut store, req)
.await
.map_err(|e| anyhow::anyhow!(e.to_string()))?
.map_err(|e| anyhow::anyhow!(e.to_string()))?;
// let res = rt.block_on(async {
// handler.handle_http(&mut store, req)
// })??;
Expand Down
2 changes: 1 addition & 1 deletion crates/mq/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl mq::Mq for Mq {

tracing::log::info!("Opening implementor {}", &state.implementor);

let inner = Self::Mq::new(&state.implementor, &state, &state.name).await;
let inner = Self::Mq::new(&state.implementor, &state, name).await;

state
.resource_map
Expand Down
42 changes: 42 additions & 0 deletions examples/multi_capability-demo-clang/bindings/kv.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ void kv_expected_unit_error_free(kv_expected_unit_error_t *ptr) {
kv_error_free(&ptr->val.err);
}
}
void kv_list_string_free(kv_list_string_t *ptr) {
for (size_t i = 0; i < ptr->len; i++) {
kv_string_free(&ptr->ptr[i]);
}
canonical_abi_free(ptr->ptr, ptr->len * 8, 4);
}
void kv_expected_list_string_error_free(kv_expected_list_string_error_t *ptr) {
if (!ptr->is_err) {
kv_list_string_free(&ptr->val.ok);
} else {
kv_error_free(&ptr->val.err);
}
}
void kv_expected_observable_error_free(kv_expected_observable_error_t *ptr) {
if (!ptr->is_err) {
kv_observable_free(&ptr->val.ok);
Expand Down Expand Up @@ -186,6 +199,35 @@ void kv_kv_set(kv_kv_t self, kv_string_t *key, kv_payload_t *value, kv_expected_
}
}*ret0 = expected;
}
__attribute__((import_module("kv"), import_name("kv::keys")))
void __wasm_import_kv_kv_keys(int32_t, int32_t);
void kv_kv_keys(kv_kv_t self, kv_expected_list_string_error_t *ret0) {
int32_t ptr = (int32_t) &RET_AREA;
__wasm_import_kv_kv_keys((self).idx, ptr);
kv_expected_list_string_error_t expected;
switch ((int32_t) (*((uint8_t*) (ptr + 0)))) {
case 0: {
expected.is_err = false;

expected.val.ok = (kv_list_string_t) { (kv_string_t*)(*((int32_t*) (ptr + 4))), (size_t)(*((int32_t*) (ptr + 8))) };
break;
}
case 1: {
expected.is_err = true;
kv_error_t variant;
variant.tag = (int32_t) (*((uint8_t*) (ptr + 4)));
switch ((int32_t) variant.tag) {
case 0: {
variant.val.error_with_description = (kv_string_t) { (char*)(*((int32_t*) (ptr + 8))), (size_t)(*((int32_t*) (ptr + 12))) };
break;
}
}

expected.val.err = variant;
break;
}
}*ret0 = expected;
}
__attribute__((import_module("kv"), import_name("kv::delete")))
void __wasm_import_kv_kv_delete(int32_t, int32_t, int32_t, int32_t);
void kv_kv_delete(kv_kv_t self, kv_string_t *key, kv_expected_unit_error_t *ret0) {
Expand Down
14 changes: 14 additions & 0 deletions examples/multi_capability-demo-clang/bindings/kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ extern "C"
} val;
} kv_expected_unit_error_t;
void kv_expected_unit_error_free(kv_expected_unit_error_t *ptr);
typedef struct {
kv_string_t *ptr;
size_t len;
} kv_list_string_t;
void kv_list_string_free(kv_list_string_t *ptr);
typedef struct {
bool is_err;
union {
kv_list_string_t ok;
kv_error_t err;
} val;
} kv_expected_list_string_error_t;
void kv_expected_list_string_error_free(kv_expected_list_string_error_t *ptr);
typedef struct {
bool is_err;
union {
Expand All @@ -74,6 +87,7 @@ extern "C"
void kv_kv_open(kv_string_t *name, kv_expected_kv_error_t *ret0);
void kv_kv_get(kv_kv_t self, kv_string_t *key, kv_expected_payload_error_t *ret0);
void kv_kv_set(kv_kv_t self, kv_string_t *key, kv_payload_t *value, kv_expected_unit_error_t *ret0);
void kv_kv_keys(kv_kv_t self, kv_expected_list_string_error_t *ret0);
void kv_kv_delete(kv_kv_t self, kv_string_t *key, kv_expected_unit_error_t *ret0);
void kv_kv_watch(kv_kv_t self, kv_string_t *key, kv_expected_observable_error_t *ret0);
#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion slight/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "slight"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["DeisLabs Engineering Team"]

Expand Down

0 comments on commit 3a6486e

Please sign in to comment.