first commit
This commit is contained in:
+213
@@ -0,0 +1,213 @@
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::{header, HeaderValue, StatusCode},
|
||||
response::{Html, IntoResponse, Response},
|
||||
routing::{get, post},
|
||||
Router,
|
||||
};
|
||||
use tower_http::{services::ServeDir, trace::TraceLayer};
|
||||
|
||||
use crate::{api, db, state::SharedState, websocket};
|
||||
|
||||
pub fn router(state: SharedState, static_dir: &str) -> Router {
|
||||
Router::new()
|
||||
.route("/", get(home))
|
||||
.route("/p/{slug}", get(pad))
|
||||
.route("/w/{workspace_slug}", get(workspace))
|
||||
.route("/w/{workspace_slug}/n/{note_slug}", get(note))
|
||||
.route("/health", get(health))
|
||||
.route("/api/pads", post(api::create_pad))
|
||||
.route("/api/pads/{slug}", get(api::pad_info))
|
||||
.route("/api/pads/{slug}/history", post(api::pad_history))
|
||||
.route("/api/pads/{slug}/restore", post(api::pad_restore))
|
||||
.route("/api/workspaces", post(api::create_workspace))
|
||||
.route("/api/workspaces/{workspace_slug}", get(api::workspace_info))
|
||||
.route("/api/workspaces/{workspace_slug}/open", post(api::open_workspace))
|
||||
.route("/api/workspaces/{workspace_slug}/notes", post(api::create_note))
|
||||
.route(
|
||||
"/api/workspaces/{workspace_slug}/notes/{note_slug}",
|
||||
get(api::note_info),
|
||||
)
|
||||
.route(
|
||||
"/api/workspaces/{workspace_slug}/notes/{note_slug}/history",
|
||||
post(api::history),
|
||||
)
|
||||
.route(
|
||||
"/api/workspaces/{workspace_slug}/notes/{note_slug}/restore",
|
||||
post(api::restore),
|
||||
)
|
||||
.route("/ws/p/{slug}", get(websocket::upgrade_pad))
|
||||
.route(
|
||||
"/ws/{workspace_slug}/{note_slug}",
|
||||
get(websocket::upgrade),
|
||||
)
|
||||
.nest_service("/assets", ServeDir::new(static_dir))
|
||||
.fallback(not_found)
|
||||
.layer(TraceLayer::new_for_http())
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
async fn health() -> &'static str {
|
||||
"ok"
|
||||
}
|
||||
|
||||
async fn home(State(state): State<SharedState>) -> Response {
|
||||
versioned_html(include_str!("../static/home.html"), &state.asset_version)
|
||||
}
|
||||
|
||||
async fn pad(
|
||||
State(state): State<SharedState>,
|
||||
Path(slug): Path<String>,
|
||||
) -> Response {
|
||||
match db::find_pad(&state.db, &slug).await {
|
||||
Ok(Some(_)) => versioned_html(include_str!("../static/pad.html"), &state.asset_version),
|
||||
Ok(None) => error_response(
|
||||
StatusCode::NOT_FOUND,
|
||||
"404",
|
||||
"Nie znaleziono notatki",
|
||||
"Ta notatka nie istnieje albo została usunięta.",
|
||||
"/",
|
||||
"Strona główna",
|
||||
&state.asset_version,
|
||||
),
|
||||
Err(error) => {
|
||||
tracing::error!(%error, %slug, "failed to load standalone pad");
|
||||
internal_error(&state.asset_version)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn workspace(
|
||||
State(state): State<SharedState>,
|
||||
Path(workspace_slug): Path<String>,
|
||||
) -> Response {
|
||||
match db::find_workspace(&state.db, &workspace_slug).await {
|
||||
Ok(Some(_)) => versioned_html(
|
||||
include_str!("../static/workspace.html"),
|
||||
&state.asset_version,
|
||||
),
|
||||
Ok(None) => error_response(
|
||||
StatusCode::NOT_FOUND,
|
||||
"404",
|
||||
"Nie znaleziono workspace",
|
||||
"Ten workspace nie istnieje albo został usunięty.",
|
||||
"/",
|
||||
"Strona główna",
|
||||
&state.asset_version,
|
||||
),
|
||||
Err(error) => {
|
||||
tracing::error!(%error, %workspace_slug, "failed to load workspace page");
|
||||
internal_error(&state.asset_version)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn note(
|
||||
State(state): State<SharedState>,
|
||||
Path((workspace_slug, note_slug)): Path<(String, String)>,
|
||||
) -> Response {
|
||||
let workspace = match db::find_workspace(&state.db, &workspace_slug).await {
|
||||
Ok(Some(workspace)) => workspace,
|
||||
Ok(None) => {
|
||||
return error_response(
|
||||
StatusCode::NOT_FOUND,
|
||||
"404",
|
||||
"Nie znaleziono workspace",
|
||||
"Workspace tej notatki nie istnieje albo został usunięty.",
|
||||
"/",
|
||||
"Strona główna",
|
||||
&state.asset_version,
|
||||
);
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::error!(%error, %workspace_slug, "failed to load note workspace");
|
||||
return internal_error(&state.asset_version);
|
||||
}
|
||||
};
|
||||
|
||||
match db::find_note(&state.db, workspace.id, ¬e_slug).await {
|
||||
Ok(Some(_)) => versioned_html(include_str!("../static/note.html"), &state.asset_version),
|
||||
Ok(None) => error_response(
|
||||
StatusCode::NOT_FOUND,
|
||||
"404",
|
||||
"Nie znaleziono notatki",
|
||||
"Ta notatka nie istnieje albo została usunięta.",
|
||||
&format!("/w/{workspace_slug}"),
|
||||
"Wróć do workspace",
|
||||
&state.asset_version,
|
||||
),
|
||||
Err(error) => {
|
||||
tracing::error!(%error, %workspace_slug, %note_slug, "failed to load note page");
|
||||
internal_error(&state.asset_version)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn not_found(State(state): State<SharedState>) -> Response {
|
||||
error_response(
|
||||
StatusCode::NOT_FOUND,
|
||||
"404",
|
||||
"Nie znaleziono strony",
|
||||
"Sprawdź adres albo wróć na stronę główną.",
|
||||
"/",
|
||||
"Strona główna",
|
||||
&state.asset_version,
|
||||
)
|
||||
}
|
||||
|
||||
fn internal_error(asset_version: &str) -> Response {
|
||||
error_response(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"500",
|
||||
"Błąd serwera",
|
||||
"Nie udało się wczytać strony. Spróbuj ponownie za chwilę.",
|
||||
"/",
|
||||
"Strona główna",
|
||||
asset_version,
|
||||
)
|
||||
}
|
||||
|
||||
fn error_response(
|
||||
status: StatusCode,
|
||||
code: &str,
|
||||
title: &str,
|
||||
message: &str,
|
||||
primary_url: &str,
|
||||
primary_label: &str,
|
||||
asset_version: &str,
|
||||
) -> Response {
|
||||
let html = include_str!("../static/error.html")
|
||||
.replace("__ASSET_VERSION__", &escape_html(asset_version))
|
||||
.replace("__ERROR_CODE__", &escape_html(code))
|
||||
.replace("__ERROR_TITLE__", &escape_html(title))
|
||||
.replace("__ERROR_MESSAGE__", &escape_html(message))
|
||||
.replace("__PRIMARY_URL__", &escape_html(primary_url))
|
||||
.replace("__PRIMARY_LABEL__", &escape_html(primary_label));
|
||||
|
||||
let mut response = (status, Html(html)).into_response();
|
||||
no_store(&mut response);
|
||||
response
|
||||
}
|
||||
|
||||
fn versioned_html(template: &str, asset_version: &str) -> Response {
|
||||
let html = template.replace("__ASSET_VERSION__", asset_version);
|
||||
let mut response = Html(html).into_response();
|
||||
no_store(&mut response);
|
||||
response
|
||||
}
|
||||
|
||||
fn no_store(response: &mut Response) {
|
||||
response.headers_mut().insert(
|
||||
header::CACHE_CONTROL,
|
||||
HeaderValue::from_static("no-cache, no-store, must-revalidate"),
|
||||
);
|
||||
}
|
||||
|
||||
fn escape_html(value: &str) -> String {
|
||||
value
|
||||
.replace('&', "&")
|
||||
.replace('<', "<")
|
||||
.replace('>', ">")
|
||||
.replace('"', """)
|
||||
.replace('\'', "'")
|
||||
}
|
||||
Reference in New Issue
Block a user