remove encode() where unnecessary, add /get/{object} route
parent
3cf3343326
commit
0464d2712e
|
@ -9,7 +9,7 @@ use anyhow::{anyhow, Result};
|
|||
use diesel::prelude::*;
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use log::{debug, trace};
|
||||
use log::debug;
|
||||
|
||||
use crate::addressing::Address;
|
||||
use crate::hash::{decode, encode, hash, Hash, Hashable};
|
||||
|
@ -59,10 +59,10 @@ impl std::fmt::Display for InnerEntry {
|
|||
}
|
||||
|
||||
impl EntryValue {
|
||||
fn to_str(&self) -> Result<String> {
|
||||
pub fn to_str(&self) -> Result<String> {
|
||||
let (type_char, content) = match self {
|
||||
EntryValue::Value(value) => ('J', serde_json::to_string(value)?),
|
||||
EntryValue::Address(address) => ('O', encode(address.encode()?)),
|
||||
EntryValue::Address(address) => ('O', address.to_string()),
|
||||
EntryValue::Invalid => return Err(anyhow!("Cannot serialize invalid Entity value.")),
|
||||
};
|
||||
|
||||
|
@ -74,7 +74,7 @@ impl EntryValue {
|
|||
impl std::fmt::Display for EntryValue {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
let (entry_type, entry_value) = match self {
|
||||
EntryValue::Address(address) => ("ADDRESS", encode(address.encode().unwrap())),
|
||||
EntryValue::Address(address) => ("ADDRESS", address.to_string()),
|
||||
EntryValue::Value(value) => ("VALUE", serde_json::to_string(value).unwrap()),
|
||||
EntryValue::Invalid => ("INVALID", "INVALID".to_string()),
|
||||
};
|
||||
|
@ -178,11 +178,11 @@ impl Handler<InsertFile> for DbExecutor {
|
|||
let connection = &self.0.get()?;
|
||||
|
||||
debug!(
|
||||
"Inserting {} ({})...",
|
||||
"Inserting {} ({}) @ {}...",
|
||||
&msg.file.path,
|
||||
encode(&msg.file.hash)
|
||||
encode(&msg.file.hash),
|
||||
Address::Hash(Hash((&msg.file.hash).clone()))
|
||||
);
|
||||
trace!("{:?}", msg.file);
|
||||
|
||||
Ok(diesel::insert_into(files::table)
|
||||
.values(msg.file)
|
||||
|
|
|
@ -81,6 +81,7 @@ fn main() -> std::io::Result<()> {
|
|||
.data(state.clone())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(routes::get_raw)
|
||||
.service(routes::get_object)
|
||||
.service(routes::get_lookup)
|
||||
.service(routes::api_refresh)
|
||||
.service(
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use crate::addressing::Address;
|
||||
use crate::database::{Entry, EntryValue};
|
||||
use crate::hash::{decode, encode, Hash};
|
||||
use actix::prelude::*;
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::error::ErrorInternalServerError;
|
||||
use actix_web::{error, get, post, web, Error, HttpResponse};
|
||||
use anyhow::Result;
|
||||
use log::debug;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::hash::{decode, Hash};
|
||||
use serde_json::json;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
|
@ -35,6 +39,42 @@ pub async fn get_raw(state: web::Data<State>, hash: web::Path<String>) -> Result
|
|||
}
|
||||
}
|
||||
|
||||
#[get("/get/{address_str}")]
|
||||
pub async fn get_object(
|
||||
state: web::Data<State>,
|
||||
address_str: web::Path<String>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let response: Result<Vec<Entry>> = state
|
||||
.db
|
||||
.send(crate::database::RetrieveObject {
|
||||
target: Address::decode(
|
||||
&decode(address_str.into_inner()).map_err(ErrorInternalServerError)?,
|
||||
)
|
||||
.map_err(ErrorInternalServerError)?,
|
||||
})
|
||||
.await?;
|
||||
|
||||
debug!("{:?}", response);
|
||||
|
||||
let entries = response.map_err(error::ErrorInternalServerError)?;
|
||||
let mut result: HashMap<String, serde_json::Value> = HashMap::new();
|
||||
for entry in entries {
|
||||
result.insert(
|
||||
encode(entry.identity.0),
|
||||
json!({
|
||||
"target": entry.target.to_string(),
|
||||
"key": entry.key,
|
||||
"value": match entry.value {
|
||||
EntryValue::Value(value) => ("VALUE", value),
|
||||
EntryValue::Address(address) => ("ADDR", json!(address.to_string())),
|
||||
EntryValue::Invalid => ("INVALID", json!("INVALID")),
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
Ok(HttpResponse::Ok().json(result))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct LookupRequest {
|
||||
query: String,
|
||||
|
|
Loading…
Reference in New Issue