first
This commit is contained in:
commit
fd8c97600a
8 changed files with 1712 additions and 0 deletions
60
src/main.rs
Normal file
60
src/main.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#![allow(unused)]
|
||||
|
||||
pub use self::error::{Error, Result};
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use axum::extract::{Path, Query};
|
||||
use axum::http::{Method, Uri};
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use axum::routing::{get, get_service};
|
||||
use axum::{middleware, Json, Router};
|
||||
use axum::Server;
|
||||
use serde::Deserialize;
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
|
||||
mod error;
|
||||
mod web;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct HelloParams {
|
||||
name: Option<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let routes_all = Router::new()
|
||||
.merge(routes_hello())
|
||||
.merge(web::routes_login::routes())
|
||||
.fallback_service(routes_static());
|
||||
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
println!("->> Listening on {addr}\n");
|
||||
Server::bind(&addr)
|
||||
.serve(routes_all.into_make_service())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn routes_static() -> Router {
|
||||
Router::new().nest_service("/", get_service(ServeDir::new("./")))
|
||||
}
|
||||
|
||||
fn routes_hello() -> Router {
|
||||
Router::new().route("/hello", get(handler_hello))
|
||||
.route("/hello2/:name", get(handler_hello2))
|
||||
}
|
||||
|
||||
// e.g. `hello?name=jen`
|
||||
async fn handler_hello(Query(params): Query<HelloParams>) -> impl IntoResponse {
|
||||
println!("->> {:<12} - handler_hello - {params:?}", "HANDLER");
|
||||
let name = params.name.as_deref().unwrap_or("World!");
|
||||
Html(format!("Hello <strong>{name}</strong>!"))
|
||||
}
|
||||
|
||||
// e.g. `hello/jen`
|
||||
async fn handler_hello2(Path(name): Path<String>) -> impl IntoResponse {
|
||||
println!("->> {:<12} - handler_hello_path - {name:?}", "HANDLER");
|
||||
Html(format!("Hello <strong>{name}</strong>!"))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue