add cancel endpoint

This commit is contained in:
Bevan Hunt 2020-01-04 15:17:49 -08:00
parent 1a954d01cf
commit b609dffd51
4 changed files with 26 additions and 7 deletions

2
Cargo.lock generated
View File

@ -348,7 +348,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "broker"
version = "0.3.0"
version = "0.3.1"
dependencies = [
"actix-cors 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-rt 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -1,6 +1,6 @@
[package]
name = "broker"
version = "0.3.0"
version = "0.3.1"
authors = ["Bevan Hunt <bevan@bevanhunt.com>"]
edition = "2018"
license = "MIT"

View File

@ -34,10 +34,11 @@ The side-effect of this system is that the latest event is the schema. Old event
```
- where {...} is for the event a string, timestamp is the epoch unix timestamp when you want the event to become the current event, and data is any JSON you want
#### Collection
``` /collection/{event} ```
- do a GET request where {event} is the name of the event you want the audit log
- do a GET request where {event} is the name of the event you want the list of events
``` /cancel/{uuid} ```
- do a GET request where (uuid) is the event uuid you want to cancel - this will mark the event published when it will not be published to the event stream - the event uuid can be obtained from the response from /insert or from /collection/{event}.
### Features

View File

@ -7,6 +7,7 @@ use actix_cors::Cors;
use std::collections::HashMap;
use chrono::prelude::*;
use uuid::Uuid;
use serde_json::json;
#[derive(Deserialize, Debug)]
struct Config {
@ -97,8 +98,23 @@ async fn insert(data: web::Data<MyData>, json: web::Json<JSON>) -> Result<HttpRe
let _ = data.db.compare_and_swap(versioned.clone(), None as Option<&[u8]>, Some(new_value.clone()));
let _ = web::block(move || data.db.flush()).await;
// return data to json response as 200
Ok(HttpResponse::Ok().json(json.0.data))
// return uuid to json response as 200
let record = json!({ "uuid": versioned });
Ok(HttpResponse::Ok().json(record))
}
async fn cancel(data: web::Data<MyData>, path: web::Path<Path>) -> Result<HttpResponse, Error> {
let p = &path.record;
let g = data.db.get(&p.as_bytes()).unwrap().unwrap();
let v = std::str::from_utf8(&g).unwrap().to_owned();
let mut json : JSON = serde_json::from_str(&v).unwrap();
let j = json.clone();
json.published = true;
let _ = data.db.compare_and_swap(p.as_bytes(), Some(serde_json::to_string(&j).unwrap().as_bytes()), Some(serde_json::to_string(&json).unwrap().as_bytes()));
let _ = web::block(move || { data.db.flush() }).await;
Ok(HttpResponse::Ok().json(json))
}
pub async fn broker_run(origin: String) -> std::result::Result<(), std::io::Error> {
@ -174,6 +190,7 @@ pub async fn broker_run(origin: String) -> std::result::Result<(), std::io::Erro
.route("/insert", web::post().to(insert))
.route("/events", web::get().to(new_client))
.route("/collection/{record}", web::get().to(collection))
.route("/cancel/{record}", web::get().to(cancel))
})
.bind(ip).unwrap()
.run()
@ -196,6 +213,7 @@ pub async fn broker_run(origin: String) -> std::result::Result<(), std::io::Erro
.route("/insert", web::post().to(insert))
.route("/events", web::get().to(new_client))
.route("/collection/{record}", web::get().to(collection))
.route("/cancel/{record}", web::get().to(cancel))
})
.bind(ip).unwrap()
.run()