feat: AppState struct
This commit is contained in:
parent
a689a064f4
commit
5d8521b646
|
@ -1,15 +1,20 @@
|
|||
use axum::{routing::get, Router};
|
||||
use std::net::SocketAddr;
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
use stranger_site::routes::index::index;
|
||||
use stranger_site::{routes::index::index, AppState};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Build our state
|
||||
let shared_state = AppState {
|
||||
title: "Stranger Systems".to_string(),
|
||||
};
|
||||
// build our application with a route
|
||||
let app = Router::new()
|
||||
.route("/", get(index))
|
||||
.route("/index", get(index))
|
||||
.route("/index.html", get(index));
|
||||
.route("/index.html", get(index))
|
||||
.with_state(Arc::new(shared_state));
|
||||
|
||||
// run it
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
|
|
|
@ -1 +1,6 @@
|
|||
pub mod routes;
|
||||
|
||||
/// Shared state for the application
|
||||
pub struct AppState {
|
||||
pub title: String,
|
||||
}
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
use axum::response::Html;
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{extract::State, response::Html};
|
||||
|
||||
use crate::AppState;
|
||||
|
||||
/// Render the front page
|
||||
pub async fn index() -> Html<String> {
|
||||
Html("<h1>Hello, World!</h1>".to_string())
|
||||
pub async fn index(State(state): State<Arc<AppState>>) -> Html<String> {
|
||||
let title: &str = state.title.as_ref();
|
||||
Html(format!("<h1>Hello, World! Site Title: {title}</h1>"))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue