Skip to content

Commit

Permalink
Reformat (using rustfmt)
Browse files Browse the repository at this point in the history
  • Loading branch information
hesiod committed Aug 25, 2021
1 parent 63c3e30 commit c184b52
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 182 deletions.
42 changes: 24 additions & 18 deletions micro-common/src/lib.rs
@@ -1,5 +1,8 @@
use std::{array::TryFromSliceError, convert::{TryFrom, TryInto}};
use std::fmt;
use std::{
array::TryFromSliceError,
convert::{TryFrom, TryInto},
fmt,
};

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -42,7 +45,7 @@ impl fmt::Display for RequestId {
#[derive(Serialize, Deserialize, Debug)]
pub enum ImageType {
PNG,
JPEG
JPEG,
}

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -67,9 +70,9 @@ pub struct RequestSettings {
pub is_low_quality: bool,
}

pub const RESULT_EXPIRATION_SECONDS: usize = 10*60;
pub const IMAGE_EXPIRATION_SECONDS: usize = 10*60;
pub const SESSION_TTL_SECONDS: u32 = 30*60;
pub const RESULT_EXPIRATION_SECONDS: usize = 10 * 60;
pub const IMAGE_EXPIRATION_SECONDS: usize = 10 * 60;
pub const SESSION_TTL_SECONDS: u32 = 30 * 60;

pub const FABSEAL_SUBMISSION_QUEUE_LIMIT: usize = 50;

Expand All @@ -78,23 +81,26 @@ pub const FABSEAL_SUBMISSION_CONSUMER_GROUP: &str = "fs_submission_group";

const REDIS_NAMESPACE: &str = "fsdata_v1";

pub fn result_key(
request_id: RequestId
) -> String {
pub fn result_key(request_id: RequestId) -> String {
const REDIS_NAMESPACE_RESULT: &str = "result";
format!("{}:{}:{}", REDIS_NAMESPACE, REDIS_NAMESPACE_RESULT, request_id)
format!(
"{}:{}:{}",
REDIS_NAMESPACE, REDIS_NAMESPACE_RESULT, request_id
)
}

pub fn input_key(
request_id: RequestId
) -> String {
pub fn input_key(request_id: RequestId) -> String {
const REDIS_NAMESPACE_INPUT: &str = "input";
format!("{}:{}:{}", REDIS_NAMESPACE, REDIS_NAMESPACE_INPUT, request_id)
format!(
"{}:{}:{}",
REDIS_NAMESPACE, REDIS_NAMESPACE_INPUT, request_id
)
}

pub fn image_key(
request_id: RequestId
) -> String {
pub fn image_key(request_id: RequestId) -> String {
const REDIS_NAMESPACE_IMAGE: &str = "image";
format!("{}:{}:{}", REDIS_NAMESPACE, REDIS_NAMESPACE_IMAGE, request_id)
format!(
"{}:{}:{}",
REDIS_NAMESPACE, REDIS_NAMESPACE_IMAGE, request_id
)
}
19 changes: 4 additions & 15 deletions micro/src/main.rs
Expand Up @@ -4,8 +4,7 @@ use actix_web::{cookie::SameSite, web::Data};
use fabseal_micro_common::SESSION_TTL_SECONDS;
use rand::Rng;

use actix_web::middleware::Logger;
use actix_web::{web, App, HttpServer};
use actix_web::{middleware::Logger, web, App, HttpServer};

use actix_redis::RedisActor;

Expand All @@ -21,10 +20,7 @@ use time::Duration;

const COOKIE_DURATION: Duration = Duration::hour();

fn create_redis_session(
settings: Settings,
key: &[u8]
) -> RedisSession {
fn create_redis_session(settings: Settings, key: &[u8]) -> RedisSession {
let s = RedisSession::new(settings.redis.address.clone(), key)
.ttl(SESSION_TTL_SECONDS)
.cookie_name("fabseal_session")
Expand All @@ -37,15 +33,8 @@ fn create_redis_session(
s.cookie_secure(false)
} else {
match settings.domain {
Some(d) => {
s
.cookie_secure(true)
.cookie_domain(&d)
},
_ => {
s
.cookie_secure(true)
},
Some(d) => s.cookie_secure(true).cookie_domain(&d),
_ => s.cookie_secure(true),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions micro/src/settings.rs
@@ -1,4 +1,4 @@
use config::{ConfigError, Config, File, Environment};
use config::{Config, ConfigError, Environment, File};

use serde::Deserialize;

Expand Down Expand Up @@ -53,4 +53,4 @@ impl Settings {
// You can deserialize (and thus freeze) the entire configuration as
s.try_into()
}
}
}
73 changes: 30 additions & 43 deletions micro/src/site/create.rs
@@ -1,8 +1,8 @@
use actix::Addr;
use actix_web::{HttpResponse, Result as AWResult, get, post, web};
use actix_session::Session;
use actix_multipart as mp;
use actix_redis::{Command, RedisActor, RespValue};
use actix_session::Session;
use actix_web::{get, post, web, HttpResponse, Result as AWResult};

use futures_util::TryStreamExt;

Expand All @@ -13,22 +13,15 @@ use log::{debug, error, info};
use fabseal_micro_common::*;
use redis_async::resp_array;

use crate::site::types::*;
use crate::site::util::*;
use crate::site::{types::*, util::*};

#[get("/public/result")]
async fn fetch_model(
info: web::Query<ResultRequestInfo>
) -> AWResult<HttpResponse> {
async fn fetch_model(info: web::Query<ResultRequestInfo>) -> AWResult<HttpResponse> {
info!("fetch_model query={:?}", info);

match info.result_type {
ResultType::Heightmap => {
Ok(HttpResponse::NotImplemented().finish())
},
ResultType::Model => {
Ok(HttpResponse::NotImplemented().finish())
},
ResultType::Heightmap => Ok(HttpResponse::NotImplemented().finish()),
ResultType::Model => Ok(HttpResponse::NotImplemented().finish()),
}
}

Expand All @@ -46,7 +39,7 @@ async fn create_new(session: Session) -> AWResult<HttpResponse> {
async fn create_upload(
session: Session,
redis: web::Data<Addr<RedisActor>>,
mut payload: mp::Multipart
mut payload: mp::Multipart,
) -> AWResult<HttpResponse> {
info!("create_upload");

Expand All @@ -57,14 +50,13 @@ async fn create_upload(
let _content_type = validate_mime_type(field.content_type())?;

let data = read_byte_chunks(&mut field).await?;
let resp = redis.send(
Command(resp_array![
let resp = redis
.send(Command(resp_array![
"SETEX",
image_key(id),
IMAGE_EXPIRATION_SECONDS.to_string(),
data
]
))
]))
.await
.map_err(redis_error("SETEX"))?
.map_err(redis_error("SETEX"))?;
Expand All @@ -85,14 +77,16 @@ async fn create_start(
let id = request_cookie(&session)?;
debug!("request-id: {}", id);

let resp1 = redis.send(Command(resp_array![
"XADD",
FABSEAL_SUBMISSION_QUEUE,
"MAXLEN",
"~",
FABSEAL_SUBMISSION_QUEUE_LIMIT.to_string(),
"*",
"request_id", &id.as_bytes()[..]
let resp1 = redis
.send(Command(resp_array![
"XADD",
FABSEAL_SUBMISSION_QUEUE,
"MAXLEN",
"~",
FABSEAL_SUBMISSION_QUEUE_LIMIT.to_string(),
"*",
"request_id",
&id.as_bytes()[..]
]))
.await
.map_err(redis_error("XADD"))?
Expand All @@ -101,39 +95,35 @@ async fn create_start(
RespValue::Error(e) => {
error!("Redis error: {}", e);
Ok(HttpResponse::InternalServerError().finish())
},
RespValue::BulkString(_) => {
Ok(HttpResponse::Accepted().finish())
},
}
RespValue::BulkString(_) => Ok(HttpResponse::Accepted().finish()),
_ => {
error!("Unexpected Redis response: {:?}", resp1);
Ok(HttpResponse::InternalServerError().finish())
},
}
}
}


#[get("/result")]
async fn create_result(
session: Session,
redis: web::Data<Addr<RedisActor>>,
info: web::Query<ResultRequestInfo>
info: web::Query<ResultRequestInfo>,
) -> AWResult<HttpResponse> {
info!("create_result query={:?}", info);
// Ok(HttpResponse::Processing().finish())

match info.result_type {
ResultType::Heightmap => {
return Ok(HttpResponse::NotImplemented().finish())
},
ResultType::Heightmap => return Ok(HttpResponse::NotImplemented().finish()),
ResultType::Model => {}
}

let id = request_cookie(&session)?;

let comm = Command(resp_array!["GET", result_key(id)]);
debug!("redis command: {:?}", comm);
let resp = redis.send(comm)
let resp = redis
.send(comm)
.await
.map_err(redis_error("GET"))?
.map_err(redis_error("GET"))?;
Expand All @@ -152,16 +142,13 @@ async fn create_finish() -> AWResult<HttpResponse> {
}

pub(crate) fn create_service(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/userupload")
.service(fetch_model)
);
cfg.service(web::scope("/userupload").service(fetch_model));
cfg.service(
web::scope("/create")
.service(create_new)
.service(create_upload)
.service(create_start)
.service(create_result)
.service(create_finish)
.service(create_finish),
);
}
}
5 changes: 2 additions & 3 deletions micro/src/site/types.rs
@@ -1,15 +1,14 @@
use serde::{Deserialize};
use serde::Deserialize;

#[derive(Deserialize, Debug)]
#[serde(rename_all(deserialize = "lowercase"))]
pub(crate) enum ResultType {
Model,
Heightmap
Heightmap,
}

#[derive(Deserialize, Debug)]
pub(crate) struct ResultRequestInfo {
#[serde(rename = "type")]
pub(crate) result_type: ResultType,
}

0 comments on commit c184b52

Please sign in to comment.