Skip to content

Commit 7b24eda

Browse files
committed
add rsync module, add lock module, done app
1 parent 2cb0021 commit 7b24eda

File tree

19 files changed

+474
-99
lines changed

19 files changed

+474
-99
lines changed

.cache/AmongUsBlack.png

3.18 KB
Loading

.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
STORE_PATH=""
2-
CACHE_PATH=""
1+
STORE_PATH="./.store"
2+
CACHE_PATH="./.cache"

.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ debug
33
**/dist
44
**/.DS_Store
55
**/Cargo.lock
6-
.store/*
7-
.ref/*
8-
!.store/.gitkeep
9-
!.ref/.gitkeep
6+
.store
7+
.cache
8+
static

.ref/.gitkeep

Whitespace-only changes.

.static/scalar.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Scalar</title>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
</head>
8+
<body>
9+
<script id="api-reference" type="application/json">
10+
$spec
11+
</script>
12+
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
13+
</body>
14+
</html>

.store/.gitkeep

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/app.rs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,65 @@
2020
//! - Мб рейтлимит
2121
//! - Бан лист (:))
2222
23+
pub mod lock;
2324
pub mod logger;
2425
pub mod png;
2526
pub mod skin;
2627

27-
use ohkami::{Ohkami, Route, claw::status};
28+
use std::sync::Arc;
2829

29-
use crate::app::skin::skin_router;
30+
use ohkami::{
31+
Ohkami, Route,
32+
claw::{content::Html, status},
33+
fang::Context,
34+
openapi,
35+
};
36+
use tokio::fs;
37+
38+
const DOC_HTML_TEMPLATE: &str = include_str!("../.static/scalar.html");
39+
const DOC_HTML_PATH: &str = "./static/doc.html";
40+
41+
use crate::{app::skin::skin_router, cache::Cache, rsync::lock::Lock};
42+
#[inline]
3043
async fn health_check() -> status::NoContent {
3144
status::NoContent
3245
}
3346

34-
pub async fn app() {
35-
Ohkami::new((
47+
async fn doc() -> Html {
48+
Html(fs::read_to_string(DOC_HTML_PATH).await.expect("wtf"))
49+
}
50+
51+
pub struct AppState<'a> {
52+
pub lock: Lock,
53+
pub cache: Cache<'a>,
54+
}
55+
56+
pub async fn app(
57+
lock: Lock,
58+
cache: Cache<'static>,
59+
) {
60+
let router = Ohkami::new((
61+
Context::new(Arc::new(AppState {
62+
lock,
63+
cache,
64+
})),
3665
"/skin".By(skin_router()),
3766
"/uvs".GET(health_check),
3867
"/health".GET(health_check),
39-
))
40-
.howl("localhost:3000")
68+
));
69+
70+
let bytes = router.__openapi_document_bytes__(openapi::OpenAPI {
71+
title: "DDNET Tee generator",
72+
version: "1",
73+
servers: &[],
74+
});
75+
fs::write(
76+
DOC_HTML_PATH,
77+
DOC_HTML_TEMPLATE.replace("$spec", &String::from_utf8(bytes).unwrap()),
78+
)
4179
.await
80+
.expect("wtf");
81+
82+
let router = Ohkami::new(("/doc".GET(doc), "/".By(router)));
83+
router.howl("localhost:3000").await;
4284
}

src/app/lock.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::sync::Arc;
2+
3+
use ohkami::{IntoResponse, Json, fang::Context};
4+
5+
use crate::{app::AppState, error::Error};
6+
7+
#[inline(always)]
8+
/// Represent GET method to return list of skins
9+
pub async fn lock_handler<'a>(
10+
Context(state): Context<'a, Arc<AppState<'a>>>
11+
) -> Result<impl IntoResponse + 'a, Error> {
12+
Ok(Json(serde_json::to_string_pretty::<Vec<String>>(
13+
state
14+
.lock
15+
.store
16+
.iter()
17+
.map(|x| x.key().to_string())
18+
.collect::<Vec<_>>()
19+
.as_ref(),
20+
)?))
21+
}

src/app/logger.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use ohkami::{FangAction, Request, Response};
33
#[derive(Clone)]
44
pub struct LogRequest;
55
impl FangAction for LogRequest {
6+
#[inline(always)]
67
async fn fore<'a>(
78
&'a self,
89
req: &'a mut Request,

src/app/png.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ pub struct Png(pub Vec<u8>);
77

88
impl IntoContent for Png {
99
const CONTENT_TYPE: &'static str = "image/png";
10-
10+
#[inline(always)]
1111
fn into_content(self) -> Result<std::borrow::Cow<'static, [u8]>, impl std::fmt::Display> {
1212
Result::<_, std::convert::Infallible>::Ok(Cow::Owned(self.0))
1313
}
14-
14+
#[inline(always)]
1515
fn openapi_responsebody() -> impl Into<openapi::schema::SchemaRef> {
1616
openapi::schema::SchemaRef::Reference("png")
1717
}

0 commit comments

Comments
 (0)