Skip to content

Commit

Permalink
update building shell
Browse files Browse the repository at this point in the history
  • Loading branch information
whisperpine committed Nov 17, 2023
1 parent e4dde7a commit 7ae5657
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
12 changes: 10 additions & 2 deletions .docker/local/build-and-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

# Build docker image and run locally.

echo ":: building image..."
red_echo() {
echo -e "\033[31m$@\033[0m"
}

green_echo() {
echo -e "\033[32m$@\033[0m"
}

green_echo ":: building image..."
docker build --pull -t axum-demo ../..
if [ $? -ne 0 ]; then
echo ":: failed to build docker image"
red_echo ":: failed to build docker image"
exit 1
fi
docker compose up -d
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ path = "src/lib.rs"

[profile.release]
lto = "thin"
panic = "abort"
strip = "symbols"

[features]
default = ["mimalloc"]
Expand Down
27 changes: 24 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ mod tests;

use error::AppError;
pub use form::{log_form, show_form};
// pub use mongo::log_mongo;

use anyhow::Result;
use axum::extract::{Form, Json, Path};
Expand All @@ -27,12 +26,34 @@ use serde::{Deserialize, Serialize};
use tracing::info;
use uuid::Uuid;

pub fn app() -> axum::Router {
use crate::service::{buffer_error_handler, timeout_error_handler};
use axum::error_handling::HandleErrorLayer;
use axum::routing::get;
use std::time::Duration;
use tower::ServiceBuilder;

axum::Router::new()
.route("/", get(handler_root).post(register_user))
.route("/mongo", get(mongo::log_mongo))
.route("/form", get(show_form).post(log_form))
.route("/path/:path_id", get(log_path))
.route("/error", get(app_error))
.fallback(handler_404)
.layer(
ServiceBuilder::new()
.layer(HandleErrorLayer::new(buffer_error_handler))
.buffer(100)
.layer(HandleErrorLayer::new(timeout_error_handler))
.timeout(Duration::from_millis(500)),
)
}

/// Graceful shutdown
///
/// Shutdown the server when pressing Ctrl+C.
/// Shutdown the server when pressing `Ctrl+C`.
pub async fn shutdown() {
use tokio::signal;

let ctrl_c = async {
signal::ctrl_c()
.await
Expand Down
28 changes: 2 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(unsafe_op_in_unsafe_fn)]
#![forbid(unsafe_code)]
#![deny(clippy::disallowed_types)]

/// Set mimalloc as heap memory allocator when then `mimalloc` feature is enabled.
Expand All @@ -22,7 +22,7 @@ async fn main() -> anyhow::Result<()> {
tracing::info!("listening at http://localhost:{}", addr.port());

axum::Server::bind(&addr)
.serve(app().into_make_service())
.serve(axum_demo::app().into_make_service())
.with_graceful_shutdown(axum_demo::shutdown())
.await?;

Expand Down Expand Up @@ -50,27 +50,3 @@ fn print_libc_linkage() {
fn print_libc_linkage() {
tracing::info!("the C runtime is linked dynamically");
}

fn app() -> axum::Router {
use axum::error_handling::HandleErrorLayer;
use axum::routing::get;
use axum_demo::service::{buffer_error_handler, timeout_error_handler};
use axum_demo::*;
use std::time::Duration;
use tower::ServiceBuilder;

axum::Router::new()
.route("/", get(handler_root).post(register_user))
.route("/mongo", get(mongo::log_mongo))
.route("/form", get(show_form).post(log_form))
.route("/path/:path_id", get(log_path))
.route("/error", get(app_error))
.fallback(handler_404)
.layer(
ServiceBuilder::new()
.layer(HandleErrorLayer::new(buffer_error_handler))
.buffer(100)
.layer(HandleErrorLayer::new(timeout_error_handler))
.timeout(Duration::from_millis(500)),
)
}

0 comments on commit 7ae5657

Please sign in to comment.