2023-04-26 05:19:49 +00:00
|
|
|
use std::{net::SocketAddr, time::Duration};
|
2022-04-10 06:00:33 +00:00
|
|
|
|
|
|
|
use axum::{
|
2023-04-26 05:19:49 +00:00
|
|
|
async_trait,
|
|
|
|
extract::{FromRef, FromRequestParts, State},
|
|
|
|
http::{request::Parts, StatusCode},
|
|
|
|
routing::get,
|
|
|
|
Router,
|
|
|
|
};
|
2023-04-28 20:42:43 +00:00
|
|
|
use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions};
|
|
|
|
// use tokio::net::TcpListener;
|
2022-04-10 06:00:33 +00:00
|
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
tracing_subscriber::registry()
|
2023-04-26 05:19:49 +00:00
|
|
|
.with(
|
|
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
2023-04-28 20:42:43 +00:00
|
|
|
.unwrap_or_else(|_| "ww_main=debug".into()),
|
2023-04-26 05:19:49 +00:00
|
|
|
)
|
2022-04-10 06:00:33 +00:00
|
|
|
.with(tracing_subscriber::fmt::layer())
|
|
|
|
.init();
|
|
|
|
|
2023-04-28 20:42:43 +00:00
|
|
|
let db_filename = {
|
|
|
|
std::env::var("DATABASE_FILE").unwrap_or_else(|_| {
|
|
|
|
let home =
|
|
|
|
std::env::var("HOME").expect("Could not determine $HOME for finding db file");
|
|
|
|
format!("{home}/.witch-watch.db")
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let conn_opts = SqliteConnectOptions::new()
|
|
|
|
.foreign_keys(true)
|
|
|
|
.auto_vacuum(sqlx::sqlite::SqliteAutoVacuum::Incremental)
|
|
|
|
.filename(&db_filename);
|
2022-04-10 06:00:33 +00:00
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
// setup connection pool
|
|
|
|
let pool = SqlitePoolOptions::new()
|
|
|
|
.max_connections(5)
|
|
|
|
.connect_timeout(Duration::from_secs(3))
|
2023-04-28 20:42:43 +00:00
|
|
|
.connect_with(conn_opts)
|
2023-04-26 05:19:49 +00:00
|
|
|
.await
|
|
|
|
.expect("can't connect to database");
|
2022-04-10 06:00:33 +00:00
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
// build our application with some routes
|
|
|
|
let app = Router::new()
|
|
|
|
.route(
|
|
|
|
"/",
|
|
|
|
get(using_connection_pool_extractor).post(using_connection_extractor),
|
|
|
|
)
|
|
|
|
.with_state(pool);
|
|
|
|
|
|
|
|
// run it with hyper
|
2023-04-28 20:42:43 +00:00
|
|
|
//let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
|
2023-04-26 05:19:49 +00:00
|
|
|
//axum::serve(listener, app).await.unwrap();
|
2023-04-28 20:42:43 +00:00
|
|
|
tracing::info!("binding to 0.0.0.0:3000");
|
2023-04-26 05:19:49 +00:00
|
|
|
axum::Server::bind(&SocketAddr::from(([0, 0, 0, 0], 3000)))
|
2022-04-10 06:00:33 +00:00
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
// we can extract the connection pool with `State`
|
|
|
|
async fn using_connection_pool_extractor(
|
|
|
|
State(pool): State<SqlitePool>,
|
|
|
|
) -> Result<String, (StatusCode, String)> {
|
2023-04-28 20:42:43 +00:00
|
|
|
sqlx::query_scalar("select 'hello world from sqlite get'")
|
2023-04-26 05:19:49 +00:00
|
|
|
.fetch_one(&pool)
|
|
|
|
.await
|
|
|
|
.map_err(internal_error)
|
2022-04-10 06:00:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
// we can also write a custom extractor that grabs a connection from the pool
|
|
|
|
// which setup is appropriate depends on your application
|
|
|
|
struct DatabaseConnection(sqlx::pool::PoolConnection<sqlx::Sqlite>);
|
2022-04-10 06:00:33 +00:00
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl<S> FromRequestParts<S> for DatabaseConnection
|
|
|
|
where
|
|
|
|
SqlitePool: FromRef<S>,
|
|
|
|
S: Send + Sync,
|
|
|
|
{
|
|
|
|
type Rejection = (StatusCode, String);
|
2022-04-10 06:00:33 +00:00
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
|
|
|
let pool = SqlitePool::from_ref(state);
|
2022-04-10 06:00:33 +00:00
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
let conn = pool.acquire().await.map_err(internal_error)?;
|
2022-04-10 06:00:33 +00:00
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
Ok(Self(conn))
|
2022-04-10 06:00:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
async fn using_connection_extractor(
|
|
|
|
DatabaseConnection(conn): DatabaseConnection,
|
|
|
|
) -> Result<String, (StatusCode, String)> {
|
|
|
|
let mut conn = conn;
|
2023-04-28 20:42:43 +00:00
|
|
|
sqlx::query_scalar("select 'hello world from sqlite post'")
|
2023-04-26 05:19:49 +00:00
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await
|
|
|
|
.map_err(internal_error)
|
2022-04-10 06:00:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
/// Utility function for mapping any error into a `500 Internal Server Error`
|
|
|
|
/// response.
|
|
|
|
fn internal_error<E>(err: E) -> (StatusCode, String)
|
|
|
|
where
|
|
|
|
E: std::error::Error,
|
|
|
|
{
|
|
|
|
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
|
2022-04-10 06:00:33 +00:00
|
|
|
}
|