Created
April 21, 2024 15:10
-
-
Save kotobukid/adaa743df418e8de140c42cf01d01516 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use std::net::SocketAddr; | |
use std::sync::Arc; | |
use axum::response::IntoResponse; | |
use axum::{Extension, Router}; | |
use axum::routing::get; | |
use clap::Error; | |
use dotenv::dotenv; | |
use redis::{Client, RedisResult}; | |
use tokio; | |
use tokio::sync::{Mutex, MutexGuard}; | |
use tower_http::trace::TraceLayer; | |
use redis::{AsyncCommands}; | |
use redis::aio::MultiplexedConnection; | |
use tokio::net::TcpListener; | |
async fn get_handler(Extension(redis): Extension<Arc<Mutex<MultiplexedConnection>>>) -> impl IntoResponse { | |
let mut conn: MutexGuard<MultiplexedConnection> = redis.lock().await; | |
let res = conn.incr::<&str, i32, i32>("key", 1).await; | |
match res { | |
Ok(_num) => { | |
let result: RedisResult<String> = conn.get("key").await; | |
let value: String = result.unwrap_or_else(|_| "?".to_string()); | |
format!("hello {value}") | |
} | |
Err(_err) => { | |
"value type error".to_string() | |
} | |
} | |
} | |
#[tokio::main] | |
async fn main() -> Result<(), Error> { | |
dotenv().ok(); | |
let host: String = env::var("REDIS_HOST").unwrap(); | |
let client: Client = Client::open(host).expect("Redis connect failed(1)"); | |
let redis_con = client.get_multiplexed_async_connection().await.unwrap(); | |
let redis: Arc<Mutex<MultiplexedConnection>> = Arc::new(Mutex::new(redis_con)); | |
let layer_redis: Extension<Arc<Mutex<MultiplexedConnection>>> = Extension(redis); | |
let port = 3000; | |
let app = Router::new() | |
.route("/", get(get_handler)) // .layer()を後ろの行に記述しないと、Extensionで取得できない | |
.layer(layer_redis); | |
let addr: SocketAddr = SocketAddr::from(([127, 0, 0, 1], port)); | |
let listener: TcpListener = TcpListener::bind(addr).await.unwrap(); | |
axum::serve(listener, app.layer(TraceLayer::new_for_http())) | |
.await | |
.unwrap(); | |
Ok::<(), Error>(()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "sample" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
axum = "0.7.4" | |
http = "1.1.0" | |
tower = "0.4.13" | |
tracing-subscriber = "0.3.18" | |
tokio = { version = "1.36.0", features = ["full"] } | |
tracing = "0.1.40" | |
tower-http = { version = "0.5.1", features = ["fs", "trace"] } | |
serde = { version = "1.0.197", features = ["derive"] } | |
clap = { version = "4.5.2", features = ["derive"] } | |
hyper = "1.2.0" | |
sqlx = { version = "0.7.4", features = ["runtime-tokio-rustls", "postgres", "chrono"] } | |
axum-extra = { version = "0.9.3", features = ["cookie"] } | |
cookie = { version = "0.18.1", features = ["secure", "percent-encode"] } | |
serde_json = "1.0.114" | |
bcrypt = "0.15.1" | |
chrono = { version = "0.4.37", features = ["serde"] } | |
dotenv = "0.15.0" | |
async-trait = "0.1.79" | |
redis = { version = "0.25.2", features = ["tokio-rustls-comp"] } | |
rand = "0.8.5" | |
http-body-util = "0.1.1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment