Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12241771
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/axum/Cargo.toml b/axum/Cargo.toml
index eb14dba..4fd6ab3 100644
--- a/axum/Cargo.toml
+++ b/axum/Cargo.toml
@@ -1,35 +1,35 @@
[package]
name = "limiting-factor-axum"
-version = "0.1.0"
+version = "0.2.0"
authors = [
"Sébastien Santoro <dereckson@espace-win.org>",
]
description = "Library to create a REST API with axum"
readme = "README.md"
keywords = [
"API",
"axum",
"REST",
]
categories = [
"web-programming",
]
license = "BSD-2-Clause"
repository = "https://devcentral.nasqueron.org/source/limiting-factor/"
edition = "2024"
[dependencies]
axum = "0.8.4"
limiting-factor-core = { path="../core", version="1.0.0" }
serde = { version = "^1.0.226", features = [ "derive" ], optional = true }
http-body-util = "0.1.3"
tokio = "1.47.1"
log = "0.4.28"
[features]
default = ["minimal"]
minimal = ["serialization"]
full = ["serialization"]
serialization = ["serde"]
diff --git a/axum/src/api/replies.rs b/axum/src/api/replies.rs
index 20cf749..f4be90f 100644
--- a/axum/src/api/replies.rs
+++ b/axum/src/api/replies.rs
@@ -1,62 +1,72 @@
/* -------------------------------------------------------------
Limiting Factor :: axum :: API :: replies
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Project: Nasqueron
License: BSD-2-Clause
------------------------------------------------------------- */
//! # API standard and JSON responses.
//!
//! This module provides useful traits and methods to craft API replies from an existing type.
use axum::http::StatusCode;
use axum::Json;
#[cfg(feature = "serialization")]
use serde::Serialize;
/* -------------------------------------------------------------
JSON responses
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
pub type ApiJsonResponse<T> = Result<Json<T>, (StatusCode, Json<String>)>;
/// This trait allows to consume an object into an HTTP response.
pub trait ApiResponse<T> {
/// Consumes the value and creates a JSON or a Status result response.
fn into_json_response(self) -> ApiJsonResponse<T>;
}
impl<T> ApiResponse<T> for Json<T> {
fn into_json_response(self) -> ApiJsonResponse<T> {
Ok(self)
}
}
#[cfg(feature = "serialization")]
impl<T> ApiResponse<T> for T where T: Serialize {
fn into_json_response(self) -> ApiJsonResponse<T> {
Ok(Json(self))
}
}
// -------------------------------------------------------------
// Failures
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pub trait FailureResponse {
fn status_code(&self) -> StatusCode;
fn response(&self) -> String;
}
+impl FailureResponse for StatusCode {
+ fn status_code(&self) -> StatusCode {
+ self.clone()
+ }
+
+ fn response(&self) -> String {
+ self.canonical_reason().unwrap_or_default().to_string()
+ }
+}
+
impl<T, E> ApiResponse<T> for Result<T, E>
where T: ApiResponse<T>, E: FailureResponse
{
fn into_json_response(self) -> ApiJsonResponse<T> {
match self {
Ok(value) => value.into_json_response(),
Err(error) => Err((error.status_code(), Json(error.response())))
}
}
}
diff --git a/axum/src/app.rs b/axum/src/app.rs
index ab5cdcc..05c8c60 100644
--- a/axum/src/app.rs
+++ b/axum/src/app.rs
@@ -1,87 +1,94 @@
/* -------------------------------------------------------------
Limiting Factor :: axum :: App
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Project: Nasqueron
License: BSD-2-Clause
------------------------------------------------------------- */
use axum::Router;
use log::{error, info};
use tokio::net::TcpListener;
/* -------------------------------------------------------------
Re-exports from core
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
pub use limiting_factor_core::app::ServerConfig;
/* -------------------------------------------------------------
Main application server
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
pub struct App {
pub config: ServerConfig,
router: Router,
}
impl Default for App {
fn default() -> Self {
Self {
config: ServerConfig::from_env(),
router: Router::new(),
}
}
}
impl App {
pub fn new (config: ServerConfig, router: Router) -> Self {
Self {
config,
router,
}
}
pub fn from_config(config: ServerConfig) -> Self {
Self {
config,
router: Router::new(),
}
}
pub fn with_config(mut self, config: ServerConfig) -> Self {
self.config = config;
self
}
+ pub fn from_router(router: Router) -> Self {
+ Self {
+ config: ServerConfig::from_env(),
+ router,
+ }
+ }
+
fn resolve_router(&self) -> Router {
if self.config.mount_point == "/" {
return self.router.clone();
}
Router::new()
.nest(&*self.config.mount_point, self.router.clone())
}
pub async fn run(self) -> bool {
let app = self.resolve_router();
let socket_address = self.config.get_socket_address();
info!("🚀 Starting server");
match TcpListener::bind(&socket_address).await {
Ok(listener) => {
info!("Listening to {}", socket_address);
axum::serve(listener, app).await.unwrap();
true
}
Err(error) => {
error!("{}", error);
false
}
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Oct 12, 06:20 (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3064646
Default Alt Text
(5 KB)
Attached To
Mode
rLF Limiting Factor
Attached
Detach File
Event Timeline
Log In to Comment