Skip to content

Commit

Permalink
First draft at routes disks and events
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Meade committed Jun 27, 2024
1 parent df9b1cc commit 739836f
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 2 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# wipac-disk-tracking
Archival media tracking service for WIPAC

## API
The REST API for the wipac-disk-tracking service is documented below.

The API is rooted at:

/api/v#

Where # is the version number of the API under use.

### Disks
Disks represent a unique archival disk.
While the disk entity does carry some immutable identifying information, it
is mostly a container for archival disk events.

#### Routes
These routes are implemented to work with disks:

GET /disks/:disk_id Get the data for a given disk
GET /disks/:disk_id/events Get all of the events for a given disk
GET /disks/:disk_id/events/:event_id Get the data for a given event for a given disk
GET /disks/:disk_id/search?query Find a disk based on a key-value query

### Events
Events represent a record of an event involving an archival disk.
There are four distinct events that are tracked by the system.

sighted This disk was observed to be loaded in a host that processes archival disks
formatted This disk was given a file system to make it ready for archival purposes
opened This disk was given a label and was designated for active archival activity
closed This disk was determined to full/finished and archival activity stopped

The format for each of these events is specified with a JSON Schema file.

#### Routes
These routes are implemented to work with events:

POST /events Create a new event
GET /events/:event_id Get the data for a given event
GET /events/search?query Find an event based on key-value query
13 changes: 13 additions & 0 deletions doc/TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# TODO.md
Some notes to keep track of progress

## JSON Schema
Look at [JSON Schema](https://json-schema.org/) and in particular some validators written in Rust:

https://json-schema.org/implementations#validators-rust

Rust
boon 2020-122019-09070604 Apache License 2.0
jsonschema-rs
Fast due to compiling schema into a validation tree; 2019-09 and 2020-12 are partially supported
2020-122019-09070604 MIT
16 changes: 14 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// main.rs

use gotham::state::State;
pub mod routes;

use gotham::{
router::{build_simple_router, builder::DrawRoutes, Router},
state::State,
};
use log::info;

const HELLO_WORLD: &str = "Hello World!\n";
Expand All @@ -9,13 +14,20 @@ pub fn say_hello(state: State) -> (State, &'static str) {
(state, HELLO_WORLD)
}

fn build_router() -> Router {
build_simple_router(|route| {
route.delegate("/api/v1").to_router(routes::v1::router());
})
}

pub fn main() {
// initialize logging, configured by environment
env_logger::init();
// start the service
let addr = "0.0.0.0:8080";
info!("Listening for requests at http://{}", addr);
gotham::start(addr, || Ok(say_hello)).unwrap();
// gotham::start(addr, || Ok(say_hello)).unwrap();
let _ = gotham::start(addr, build_router());
}

// --------------------------------------------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// routes.rs

pub mod v1;
17 changes: 17 additions & 0 deletions src/routes/v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// v1.rs

pub mod disks;
pub mod events;

pub use disks::{DiskResource, DiskResources};
pub use events::{EventResource, EventResources};

use gotham::router::{build_simple_router, Router};
use gotham_restful::DrawResources;

pub fn router() -> Router {
build_simple_router(|route| {
route.resource::<DiskResource>("disks");
route.resource::<EventResource>("events");
})
}
84 changes: 84 additions & 0 deletions src/routes/v1/disks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// disks.rs

use gotham::{router::response::StaticResponseExtender, state::StateData};
use gotham_restful::{endpoint, gotham::hyper::Method, read, search, Resource, Success};
use log::{error, info};
use serde::{Deserialize, Serialize};

use crate::routes::v1::{EventResource, EventResources};

#[derive(Resource, Serialize)]
#[resource(get_disk_by_id)]
#[resource(get_event_by_disk_id_and_event_id)]
#[resource(get_events_by_disk_id)]
#[resource(find_disk_by_query)]
pub struct DiskResource {
pub id: u64,
}

#[derive(Serialize)]
pub struct DiskResources {
pub disks: Vec<DiskResource>,
}

// --------------------------------------------------------------------------------------------------------------------

#[derive(Clone, Deserialize, StateData, StaticResponseExtender)]
struct DiskSearchQuery {
serial_number: String,
}

#[search]
fn find_disk_by_query(query: DiskSearchQuery) -> Success<DiskResources> {
info!("find_disk_by_query()");
DiskResources {
disks: Vec::new()
}.into()
}

// --------------------------------------------------------------------------------------------------------------------

#[read]
fn get_disk_by_id(disk_id: u64) -> Success<DiskResource> {
info!("get_disk_by_id()");
DiskResource { id: disk_id }.into()
}

// --------------------------------------------------------------------------------------------------------------------

#[derive(Clone, Deserialize, StateData, StaticResponseExtender)]
struct DiskAndEvent {
disk_id: u64,
event_id: u64,
}

#[endpoint(
uri = ":disk_id/events/:event_id",
method = "Method::GET",
params = false,
body = false
)]
fn get_event_by_disk_id_and_event_id(disk_and_event: DiskAndEvent) -> Success<EventResource> {
info!("get_event_by_disk_id_and_event_id()");
EventResource { id: disk_and_event.event_id }.into()
}

// --------------------------------------------------------------------------------------------------------------------

#[derive(Clone, Deserialize, StateData, StaticResponseExtender)]
struct Disk {
disk_id: u64,
}

#[endpoint(
uri = ":disk_id/events",
method = "Method::GET",
params = false,
body = false
)]
fn get_events_by_disk_id(disk: Disk) -> Success<EventResources> {
info!("get_events_by_disk_id()");
EventResources {
events: Vec::new()
}.into()
}
56 changes: 56 additions & 0 deletions src/routes/v1/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// events.rs

use gotham::{router::response::StaticResponseExtender, state::StateData};
use gotham_restful::{create, read, search, Resource, Success};
use log::{error, info};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

#[derive(Resource, Serialize)]
#[resource(create_event)]
#[resource(find_event_by_query)]
#[resource(get_event_by_id)]
pub struct EventResource {
pub id: u64,
}

#[derive(Serialize)]
pub struct EventResources {
pub events: Vec<EventResource>,
}

// --------------------------------------------------------------------------------------------------------------------

#[derive(Clone, Deserialize, StateData, StaticResponseExtender)]
struct EventBody {
serial_number: String,
}

#[create]
fn create_event(body: EventBody) -> Success<EventResource> {
info!("create_event()");
EventResource { id: 0 }.into()
}

// --------------------------------------------------------------------------------------------------------------------

#[derive(Clone, Deserialize, StateData, StaticResponseExtender)]
struct EventSearchQuery {
serial_number: String,
}

#[search]
fn find_event_by_query(query: EventSearchQuery) -> Success<EventResources> {
info!("find_event_by_query()");
EventResources {
events: Vec::new(),
}.into()
}

// --------------------------------------------------------------------------------------------------------------------

#[read]
fn get_event_by_id(id: u64) -> Success<EventResource> {
info!("get_event_by_id()");
EventResource { id }.into()
}

0 comments on commit 739836f

Please sign in to comment.