dev-nm17-2 #25

Open
nm17 wants to merge 6 commits from dev-nm17-2 into master
5 changed files with 98 additions and 37 deletions
Showing only changes of commit 1f827114eb - Show all commits

73
Cargo.lock generated
View file

@ -23,7 +23,7 @@ version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9"
dependencies = [ dependencies = [
"getrandom", "getrandom 0.2.15",
"once_cell", "once_cell",
"version_check", "version_check",
] ]
@ -785,7 +785,19 @@ checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"wasi", "wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8"
dependencies = [
"cfg-if",
"libc",
"wasi 0.13.3+wasi-0.2.2",
"windows-targets",
] ]
[[package]] [[package]]
@ -1093,11 +1105,14 @@ dependencies = [
"hex", "hex",
"hifitime", "hifitime",
"lazy_static", "lazy_static",
"log",
"nom", "nom",
"ntex", "ntex",
"phf", "phf",
"ppp",
"regex", "regex",
"rust_decimal", "rust_decimal",
"rust_decimal_macros",
"serde", "serde",
"serde_json", "serde_json",
"serde_qs", "serde_qs",
@ -1107,6 +1122,7 @@ dependencies = [
"thiserror", "thiserror",
"tokio", "tokio",
"ufmt", "ufmt",
"uuid",
] ]
[[package]] [[package]]
@ -1223,9 +1239,9 @@ dependencies = [
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.22" version = "0.4.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e"
[[package]] [[package]]
name = "memchr" name = "memchr"
@ -1261,7 +1277,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
dependencies = [ dependencies = [
"libc", "libc",
"wasi", "wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
@ -1685,6 +1701,15 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "ppp"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a7a2049cd2570bd67bf0228e86bf850f8ceb5190a345c471d03a909da6049e0"
dependencies = [
"thiserror",
]
[[package]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.20" version = "0.2.20"
@ -1774,7 +1799,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [ dependencies = [
"getrandom", "getrandom 0.2.15",
] ]
[[package]] [[package]]
@ -1883,6 +1908,16 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "rust_decimal_macros"
version = "1.36.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da991f231869f34268415a49724c6578e740ad697ba0999199d6f22b3949332c"
dependencies = [
"quote",
"rust_decimal",
]
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.24" version = "0.1.24"
@ -2422,9 +2457,13 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.11.0" version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a" checksum = "93d59ca99a559661b96bf898d8fce28ed87935fd2bea9f05983c1464dd6c71b1"
dependencies = [
"getrandom 0.3.1",
"serde",
]
[[package]] [[package]]
name = "version_check" name = "version_check"
@ -2438,6 +2477,15 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasi"
version = "0.13.3+wasi-0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
dependencies = [
"wit-bindgen-rt",
]
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.99" version = "0.2.99"
@ -2612,6 +2660,15 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "wit-bindgen-rt"
version = "0.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
dependencies = [
"bitflags",
]
[[package]] [[package]]
name = "write16" name = "write16"
version = "1.0.0" version = "1.0.0"

View file

@ -32,3 +32,7 @@ ufmt = { version = "0.2.0", features = ["std"] }
futures-util = { version = "0.3.30", features = ["tokio-io"] } futures-util = { version = "0.3.30", features = ["tokio-io"] }
snafu = "0.8.5" snafu = "0.8.5"
clap-verbosity-flag = "3.0.2" clap-verbosity-flag = "3.0.2"
ppp = "2.3.0"
log = "0.4.26"
uuid = { version = "1.14.0", features = ["serde", "v4"] }
rust_decimal_macros = "1.36.0"

View file

@ -2,25 +2,23 @@ use crate::web_server::app_error::{self, AppError};
use crate::web_server::old_app_api::types::AppInitRequest; use crate::web_server::old_app_api::types::AppInitRequest;
use crate::web_server::NMAppState; use crate::web_server::NMAppState;
use serde_json::{json}; use serde_json::json;
use snafu::ResultExt; use snafu::ResultExt;
use crate::insert_header; use crate::insert_header;
use fred::interfaces::KeysInterface; use fred::interfaces::KeysInterface;
use ntex::http::StatusCode; use ntex::http::StatusCode;
use ntex::web; use ntex::web;
pub async fn app_init( pub async fn app_init(
_body: AppInitRequest<'_>, _body: AppInitRequest,
app_state: &NMAppState, app_state: &NMAppState,
) -> Result<web::HttpResponse, AppError> { ) -> Result<web::HttpResponse, AppError> {
let _: () = app_state let _: () = app_state
.redis_client .redis_client
.set("test", 123, None, None, true) .set("test", 123, None, None, true)
.await.context(app_error::ServerRedisSnafu)?; .await
.context(app_error::ServerRedisSnafu)?;
Ok(web::HttpResponse::build(StatusCode::OK).body("Hello world!")) Ok(web::HttpResponse::build(StatusCode::OK).body("Hello world!"))
} }

View file

@ -13,9 +13,14 @@ use crate::web_server::NMAppState;
use crate::web_server::old_app_api::handlers::{app_init, version}; use crate::web_server::old_app_api::handlers::{app_init, version};
use crate::web_server::old_app_api::types::{AppInitRequest, MandatoryParams}; use crate::web_server::old_app_api::types::{AppInitRequest, MandatoryParams};
use crate::web_server::utils::redis::is_api_key_valid; use crate::web_server::utils::redis::is_api_key_valid;
use crate::web_server::NMAppState;
use ntex::http::HeaderMap;
use ntex::util::Bytes;
use ntex::web::types::State;
use ntex::web::{self, HttpRequest};
use snafu::{whatever, ResultExt};
use super::app_error; use super::app_error::{self, Utf8Snafu};
/// Обработчик запросов от приложений. /// Обработчик запросов от приложений.
/// ///
@ -31,18 +36,18 @@ pub async fn old_api_handler(
return Err(AppError::RequestTooLarge); return Err(AppError::RequestTooLarge);
} }
let body_bytes = body_bytes.as_bytes(); let mandatory_params: MandatoryParams =
serde_json::from_slice(&body_bytes).context(app_error::JsonSnafu {})?; // TODO: Simd-JSON
let mandatory_params: MandatoryParams<'_> = serde_json::from_slice(body_bytes).context(app_error::JsonSnafu {})?; // TODO: Simd-JSON
// Ignore clippy singlematch // Ignore clippy singlematch
if mandatory_params.cmd.as_ref() == "version" { return version((), &app_state).await } if mandatory_params.cmd.as_ref() == "version" { return version((), &app_state).await }
is_api_key_valid(&app_state.redis_client, mandatory_params.api_key.as_ref()).await?; is_api_key_valid(&app_state.redis_client, mandatory_params.api_key.as_ref()).await?;
match mandatory_params.cmd.as_ref() { match mandatory_params.cmd.as_str() {
"appInit" => { "appInit" => {
let body: AppInitRequest = serde_json::from_slice(body_bytes).context(app_error::JsonSnafu {})?; let body: AppInitRequest =
serde_json::from_slice(&body_bytes).context(app_error::JsonSnafu {})?;
app_init(body, &app_state).await app_init(body, &app_state).await
} }

View file

@ -1,18 +1,16 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::borrow::Cow; use std::borrow::Cow;
use uuid::Uuid;
// fn<'de, D>(D) -> Result<T, D::Error> where D: Deserializer<'de> // fn<'de, D>(D) -> Result<T, D::Error> where D: Deserializer<'de>
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AppInitRequest<'a> { pub struct AppInitRequest {
#[serde(borrow)] pub version: String,
pub version: Cow<'a, str>,
#[serde(borrow)] pub platform: String,
pub platform: Cow<'a, str>,
#[serde(borrow)] pub model: String,
pub model: Cow<'a, str>,
pub width: u64, pub width: u64,
} }
@ -29,21 +27,20 @@ pub struct AddLikeRequest {
/// получить [MandatoryParams], другой в зависимости от `cmd`. Это позволяет не добвалять поля из /// получить [MandatoryParams], другой в зависимости от `cmd`. Это позволяет не добвалять поля из
/// этой структуры в каждом специфичном типе. /// этой структуры в каждом специфичном типе.
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MandatoryParams<'a> { pub struct MandatoryParams {
#[serde(borrow)] pub cmd: String,
pub cmd: Cow<'a, str>,
#[serde(borrow)] pub lang: String,
pub lang: Cow<'a, str>,
/// Уникальный ID клиента. /// Уникальный ID клиента.
/// ///
/// Используется на подобии как куки PHPSESSID в php. /// Используется на подобии как куки PHPSESSID в php.
/// ///
/// См. также: <https://www.php.net/manual/en/book.session.php> /// См. также: <https://www.php.net/manual/en/book.session.php>
#[serde(borrow)] pub uuid: Uuid,
pub uuid: Cow<'a, str>,
#[serde(borrow)] /// API ключ приложения
pub api_key: Cow<'a, str>, ///
/// Может быть указан в теле запроса, а может быть и заголовке Narodmon-Api-Key.
pub api_key: Option<String>,
} }