thangchung / dapr-labs

Many forms of coffeeshop implemented by C# and Rust run on WebAssembly/WASI (Spin) + Dapr. ⭐️ Star to support our work!
https://github.com/thangchung/dapr-labs
MIT License
44 stars 3 forks source link

CoffeeShop on Spin with Dapr #4

Open thangchung opened 1 year ago

thangchung commented 1 year ago

Refs: https://www.thorsten-hans.com/how-to-blog-backends-with-spin-and-fermyon-cloud/

#[http_component]
fn handle_blog_apis_rust(req: Request) -> Result<Response> {
  let mut router = spin_sdk::http::Router::default();
  router.post("/contact", requests::process_contact_request);
  router.post("/sponsor", requests::process_sponsor_request);
  router.post("/subscriptions", newsletter::subscribe);
  router.get("/search", search::search);
  router.add("/...", http::Method::OPTIONS, cors::process_preflight);
  router.handle(req)
}
pub(crate) fn search(req: Request, _params: Params) -> Result<Response>{
  println!("[SEARCH]: Endpoint invoked");
  let Some(search_term) = get_query_string_parameter(req.uri(), "query") else {
    println!("[Error]: Could not find search term");
    return Ok(http::Response::builder()
      .status(http::StatusCode::BAD_REQUEST)
      .body(None)?);
  };
  println!("[SEARCH]: Endpoint invoked with '{}'", &search_term);

  let Ok(search_url) = config::get(CFG_NAME_SEARCH_URL) else {
    println!("[Error]: Could not find Azure Search Service URL");
    return Ok(http::Response::builder()
      .status(http::StatusCode::INTERNAL_SERVER_ERROR)
      .body(None)?);
  };

  let Ok(search_api_key) = config::get(CFG_NAME_SEARCH_API_TOKEN) else {
    println!("[Error]: Could not find Azure Search API Key");
    return Ok(http::Response::builder()
      .status(http::StatusCode::INTERNAL_SERVER_ERROR)
      .body(None)?);
  };

  let query_string = build_azure_search_query_string(search_term.as_str());

  let req = http::Request::builder()
    .method(http::Method::GET)
    .header(http::header::ACCEPT, "application/json")
    .header(http::header::CONTENT_TYPE, "application/json")
    .header("api-key", search_api_key)
    .uri(format!("{}{}", search_url, query_string))
    .body(None)?;

  match spin_sdk::outbound_http::send_request(req) {
    Ok(r) => {
      println!("[SEARCH]: Will respond with search results");
      Ok(builder_with_cors(r.status())
      .body(r.body().clone())?)
    },
    Err(e) => {
      println!("[Error]: Did not receive successful response from Azure Search Service ({})", e);
      Ok(http::Response::builder()
      .status(http::StatusCode::INTERNAL_SERVER_ERROR)
      .body(None)?)
    },
  }
}
thangchung commented 1 year ago
thangchung commented 1 year ago

Refs https://github.com/thangchung/try-nomad-dapr/pull/6

thangchung commented 1 year ago

Deploy on Kubernetes:

And might apply ArgoCD => https://github.com/Archisman-Mridha/kubernetes-cicd-with-argo

thangchung commented 1 year ago
thangchung commented 1 year ago

Cannot run 2 different runtimes on a sidecar model with dapr (spin run wasm runtime, but daprd runs normal workload, so when you run it as a sidecar, it will be failed when started daprd). 2 solutions here:

thangchung commented 1 year ago

POC: uuid -> text, and map it into Rust code (Spin doesn't support uuid right now)

thangchung commented 1 year ago

Run mixed workload in k8s => https://github.com/suneetnangia/wasm-orchestration-with-spin