mirror of
https://github.com/RGBCube/Site
synced 2025-08-01 13:37:49 +00:00
Start rewriting for axum
This commit is contained in:
parent
a5ffcce3c8
commit
52783348df
10 changed files with 417 additions and 824 deletions
1022
Cargo.lock
generated
1022
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -9,8 +9,9 @@ repositoty = "https://github.com/RGBCube/rgbcube.github.io"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { version = "4.4.1", features = [ "openssl" ] }
|
||||
anyhow = "1.0.77"
|
||||
axum = "0.7.3"
|
||||
axum-server = { version = "0.6.0", features = [ "tls-rustls" ] }
|
||||
bytes = "1.5.0"
|
||||
cargo_toml = "0.17.2"
|
||||
chrono = "0.4.31"
|
||||
|
@ -18,11 +19,13 @@ clap = { version = "4.4.12", features = [ "derive" ] }
|
|||
embed = { git = "https://github.com/RGBCube/embed-rs" }
|
||||
env_logger = "0.10.1"
|
||||
log = { version = "0.4.20", features = [ "serde" ] }
|
||||
maud = { version = "0.25.0", features = [ "actix-web" ] }
|
||||
maud = { git = "https://github.com/lambda-fairy/maud", features = [ "axum" ] }
|
||||
mime_guess = "2.0.4"
|
||||
minify-js = "0.6.0"
|
||||
openssl = "0.10.62"
|
||||
pulldown-cmark = "0.9.3"
|
||||
tokio = { version = "1.35.1", features = [ "full" ] }
|
||||
tower = "0.4.13"
|
||||
tower-http = { version = "0.5.0", features = [ "trace" ] }
|
||||
|
||||
[patch.crates-io]
|
||||
proc-macro2 = { git = "https://github.com/RGBCube/proc-macro2" }
|
||||
|
|
|
@ -72,12 +72,7 @@
|
|||
inherit cargoArtifacts;
|
||||
});
|
||||
in {
|
||||
devShells.${system}.default = crane.devShell {
|
||||
packages = with nixpkgs.legacyPackages.${system}; [
|
||||
openssl
|
||||
pkg-config
|
||||
];
|
||||
};
|
||||
devShells.${system}.default = crane.devShell {};
|
||||
|
||||
checks.${system} = {
|
||||
inherit site;
|
||||
|
|
|
@ -1,15 +1,10 @@
|
|||
use axum::Router;
|
||||
|
||||
mod internal_server_error;
|
||||
mod not_found;
|
||||
|
||||
use actix_web::{
|
||||
http::StatusCode,
|
||||
middleware::ErrorHandlers,
|
||||
};
|
||||
|
||||
pub fn handler<B: 'static>() -> ErrorHandlers<B> {
|
||||
ErrorHandlers::new()
|
||||
.handler(StatusCode::NOT_FOUND, not_found::handler)
|
||||
.handler(
|
||||
Router::new().fallback(not_found::handler).handler(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
internal_server_error::handler,
|
||||
)
|
||||
|
|
|
@ -1,27 +1,21 @@
|
|||
use std::array;
|
||||
|
||||
use actix_web::{
|
||||
dev::ServiceResponse,
|
||||
middleware::ErrorHandlerResponse,
|
||||
use maud::{
|
||||
html,
|
||||
Markup,
|
||||
};
|
||||
use maud::html;
|
||||
|
||||
use crate::{
|
||||
asset,
|
||||
page::cube,
|
||||
};
|
||||
|
||||
pub fn handler<B: 'static>(
|
||||
response: ServiceResponse<B>,
|
||||
) -> actix_web::Result<ErrorHandlerResponse<B>> {
|
||||
let (request, response) = response.into_parts();
|
||||
|
||||
let response = response.set_body(
|
||||
pub fn handler() -> Markup {
|
||||
cube::create(
|
||||
Some("404"),
|
||||
asset::Css::Shared("cube-grid.css"),
|
||||
array::from_fn(|_| {
|
||||
(html! {
|
||||
html! {
|
||||
.frame {
|
||||
a href="/" { "404" }
|
||||
}
|
||||
|
@ -29,16 +23,7 @@ pub fn handler<B: 'static>(
|
|||
.square .magenta {}
|
||||
.square .magenta {}
|
||||
.square .black {}
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
}),
|
||||
)
|
||||
.into_string(),
|
||||
);
|
||||
|
||||
Ok(ErrorHandlerResponse::Response(
|
||||
ServiceResponse::new(request, response)
|
||||
.map_into_boxed_body()
|
||||
.map_into_right_body(),
|
||||
))
|
||||
}
|
||||
|
|
64
src/main.rs
64
src/main.rs
|
@ -1,4 +1,4 @@
|
|||
#![feature(iterator_try_collect, lazy_cell, let_chains)]
|
||||
#![feature(lazy_cell, let_chains)]
|
||||
|
||||
mod asset;
|
||||
mod errors;
|
||||
|
@ -7,21 +7,16 @@ mod minify;
|
|||
mod page;
|
||||
mod routes;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::{
|
||||
net::SocketAddr,
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use actix_web::{
|
||||
main as async_main,
|
||||
middleware,
|
||||
App,
|
||||
HttpServer,
|
||||
};
|
||||
use anyhow::Context;
|
||||
use axum::Router;
|
||||
use axum_server::tls_rustls::RustlsConfig;
|
||||
use clap::Parser;
|
||||
use openssl::ssl::{
|
||||
SslAcceptor,
|
||||
SslFiletype,
|
||||
SslMethod,
|
||||
};
|
||||
use tower_http::trace::TraceLayer;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about)]
|
||||
|
@ -41,7 +36,7 @@ struct Cli {
|
|||
key: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[async_main]
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let args = Cli::parse();
|
||||
|
||||
|
@ -51,35 +46,26 @@ async fn main() -> anyhow::Result<()> {
|
|||
.format_timestamp(None)
|
||||
.init();
|
||||
|
||||
let server = HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Logger::default())
|
||||
.wrap(errors::handler())
|
||||
.service(routes::handler())
|
||||
});
|
||||
let app = Router::new()
|
||||
.merge(routes::router())
|
||||
.merge(errors::router())
|
||||
.layer(TraceLayer::new_for_http())
|
||||
.into_make_service();
|
||||
|
||||
let server = if let Some(certificate_path) = args.certificate
|
||||
let address = SocketAddr::from(([0, 0, 0, 0], args.port));
|
||||
|
||||
if let Some(certificate_path) = args.certificate
|
||||
&& let Some(key_path) = args.key
|
||||
{
|
||||
let mut builder = SslAcceptor::mozilla_intermediate_v5(SslMethod::tls()).unwrap();
|
||||
|
||||
builder
|
||||
.set_private_key_file(key_path, SslFiletype::PEM)
|
||||
.unwrap();
|
||||
builder
|
||||
.set_certificate_chain_file(certificate_path)
|
||||
.unwrap();
|
||||
|
||||
server.bind_openssl(("0.0.0.0", args.port), builder)
|
||||
} else {
|
||||
server.bind(("0.0.0.0", args.port))
|
||||
};
|
||||
|
||||
server
|
||||
.with_context(|| format!("Failed to bind to 0.0.0.0:{}", args.port))?
|
||||
.run()
|
||||
let config = RustlsConfig::from_pem_file(certificate_path, key_path)
|
||||
.await
|
||||
.with_context(|| "Failed to run HttpServer")?;
|
||||
.with_context(|| "Failed to create TLS configuration from PEM files")?;
|
||||
|
||||
axum_server::bind_rustls(address, config).serve(app).await
|
||||
} else {
|
||||
axum_server::bind(address).serve(app).await
|
||||
}
|
||||
.with_context(|| "Failed to run server")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use actix_web::get;
|
||||
use maud::Markup;
|
||||
|
||||
use crate::{
|
||||
|
@ -9,11 +8,10 @@ use crate::{
|
|||
},
|
||||
};
|
||||
|
||||
#[get("/about")]
|
||||
pub async fn handler() -> actix_web::Result<Markup> {
|
||||
Ok(text::create(
|
||||
pub async fn handler() -> Markup {
|
||||
text::create(
|
||||
Some("About"),
|
||||
Page::About,
|
||||
markdown::parse(embed::string!("about.md").as_ref()),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,13 +1,18 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
path::Path,
|
||||
path,
|
||||
sync::LazyLock,
|
||||
};
|
||||
|
||||
use actix_web::{
|
||||
get,
|
||||
web,
|
||||
HttpResponse,
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::Path,
|
||||
http::{
|
||||
header::CONTENT_TYPE,
|
||||
Response,
|
||||
StatusCode,
|
||||
},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
|
||||
|
@ -19,7 +24,7 @@ static ASSETS: LazyLock<HashMap<String, Bytes>> = LazyLock::new(|| {
|
|||
let mut assets = HashMap::new();
|
||||
|
||||
for file in embed::dir!("..").flatten() {
|
||||
let path = Path::new(file.path().as_ref())
|
||||
let path = path::Path::new(file.path().as_ref())
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
|
@ -46,19 +51,19 @@ static ASSETS: LazyLock<HashMap<String, Bytes>> = LazyLock::new(|| {
|
|||
assets
|
||||
});
|
||||
|
||||
#[get("/assets/{path}")]
|
||||
pub async fn handler(path: web::Path<String>) -> HttpResponse {
|
||||
let path = path.into_inner();
|
||||
|
||||
pub async fn handler(Path(path): Path<String>) -> Response<Body> {
|
||||
if let Some(body) = ASSETS.get(&path) {
|
||||
HttpResponse::Ok()
|
||||
.content_type(
|
||||
(
|
||||
[(
|
||||
CONTENT_TYPE,
|
||||
mime_guess::from_path(&path)
|
||||
.first_or_octet_stream()
|
||||
.essence_str(),
|
||||
)],
|
||||
Bytes::clone(body),
|
||||
)
|
||||
.body(Bytes::clone(body))
|
||||
.into_response()
|
||||
} else {
|
||||
HttpResponse::NotFound().into()
|
||||
StatusCode::NOT_FOUND.into_response()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use actix_web::get;
|
||||
use maud::{
|
||||
html,
|
||||
Markup,
|
||||
|
@ -9,9 +8,8 @@ use crate::{
|
|||
page::cube,
|
||||
};
|
||||
|
||||
#[get("/")]
|
||||
pub async fn handler() -> actix_web::Result<Markup> {
|
||||
Ok(cube::create(
|
||||
pub async fn handler() -> Markup {
|
||||
cube::create(
|
||||
None,
|
||||
asset::css::owned!("index.css"),
|
||||
[
|
||||
|
@ -46,5 +44,5 @@ pub async fn handler() -> actix_web::Result<Markup> {
|
|||
}
|
||||
},
|
||||
],
|
||||
))
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
use axum::{
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
|
||||
mod about;
|
||||
mod assets;
|
||||
mod index;
|
||||
|
||||
use actix_web::{
|
||||
web,
|
||||
Scope,
|
||||
};
|
||||
|
||||
pub fn handler() -> Scope {
|
||||
web::scope("")
|
||||
.service(index::handler)
|
||||
.service(about::handler)
|
||||
.service(assets::handler)
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route("/", get(index::handler))
|
||||
.route("/about", get(about::handler))
|
||||
.route("/assets/*path", get(assets::handler))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue