1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
mod session;
mod template;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};
use askama::{Template};
use axum::extract::{Path, Query, State};
use axum::http::{header, StatusCode};
use axum::response::{Html, IntoResponse, Response};
use axum::Router;
use axum::routing::{get};
use rust_embed::Embed;
use crate::session::{Session};
use crate::template::{IndexTemplate, SessionTemplate};
#[derive(Embed)]
#[folder = "assets/"]
struct Asset;
struct AppState {
sessions: Mutex<HashMap<String, Session>>
}
impl AppState {
fn new() -> Self {
AppState {
sessions: Mutex::new(HashMap::new())
}
}
}
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(serve_index))
.route("/dist/{*path}", get(serve_static))
.route("/session/{id}", get(visit_session).put(create_session))
.with_state(Arc::new(AppState::new()));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
fn serve_template(template: impl Template) -> Response {
match template.render() {
Ok(html) => Html(html).into_response(),
Err(err) => {
eprintln!("An error occurred while rendering a template: {}", err);
(StatusCode::INTERNAL_SERVER_ERROR, "Render error").into_response()
}
}
}
async fn serve_index(State(state): State<Arc<AppState>>) -> Response {
let sessions = state.sessions.lock().unwrap();
serve_template(IndexTemplate {
sessions: &sessions
})
}
async fn serve_static(Path(path): Path<String>) -> Response {
match Asset::get(path.as_str()) {
Some(content) => {
let mime = match path.split('.').last() {
Some("js") => "application/javascript",
_ => "application/octet-stream"
};
([(header::CONTENT_TYPE, mime)], content.data).into_response()
}
None => (StatusCode::NOT_FOUND, "404 Not Found").into_response()
}
}
async fn visit_session(Path(id): Path<String>, Query(query): Query<HashMap<String, String>>, State(state): State<Arc<AppState>>) -> Response {
let passcode = query.get("passcode");
let sessions = state.sessions.lock().unwrap();
match sessions.get(&id) {
Some(session) => {
match passcode {
Some(passcode) if passcode.as_str() == session.passcode =>
serve_template(SessionTemplate {
session: &session
}),
_ => (StatusCode::FORBIDDEN, "Incorrect session passcode").into_response()
}
},
None => (StatusCode::NOT_FOUND, "Session does not exist").into_response()
}
}
async fn create_session(Path(id): Path<String>, Query(query): Query<HashMap<String, String>>, State(state): State<Arc<AppState>>) -> Response {
let name = query.get("name").map(|name| name.clone()).unwrap_or("Unknown".to_string());
let passcode = SystemTime::now().duration_since(UNIX_EPOCH).map(|duration| duration.subsec_nanos()).unwrap_or(675603000).to_string();
let mut sessions = state.sessions.lock().unwrap();
let session = Session::new(name, passcode.clone());
sessions.insert(id, session);
(StatusCode::CREATED, passcode).into_response()
}
|