Added basic axum server
This commit is contained in:
3
server/src/database.rs
Normal file
3
server/src/database.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod postgres;
|
||||
pub use postgres::apply_migrations;
|
||||
pub use postgres::connect;
|
37
server/src/database/postgres.rs
Normal file
37
server/src/database/postgres.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use sqlx::{
|
||||
migrate::MigrateDatabase, postgres::PgPoolOptions, Connection, PgConnection, PgPool, Postgres,
|
||||
};
|
||||
|
||||
pub async fn connect() -> Result<PgPool, sqlx::Error> {
|
||||
tracing::info!("Initializing database connection");
|
||||
|
||||
let database_url =
|
||||
dotenvy::var("DATABASE_URL").expect("`DATABASE_URL` environment variable not set");
|
||||
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(&database_url)
|
||||
.await?;
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
|
||||
pub async fn apply_migrations() -> Result<(), sqlx::Error> {
|
||||
let uri = dotenvy::var("DATABASE_URL").expect("`DATABASE_URL` environment variable not set");
|
||||
let uri = uri.as_str();
|
||||
|
||||
if !Postgres::database_exists(uri).await? {
|
||||
tracing::info!("Creating database...");
|
||||
Postgres::create_database(uri).await?;
|
||||
}
|
||||
|
||||
tracing::info!("Applying migrations...");
|
||||
|
||||
let mut conn = PgConnection::connect(uri).await?;
|
||||
sqlx::migrate!()
|
||||
.run(&mut conn)
|
||||
.await
|
||||
.expect("Error while running database migrations");
|
||||
|
||||
Ok(())
|
||||
}
|
1
server/src/lib.rs
Normal file
1
server/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod database;
|
45
server/src/main.rs
Normal file
45
server/src/main.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use axum::{http::StatusCode, routing::get, Router};
|
||||
use tokio::net::TcpListener;
|
||||
use tracing::Level;
|
||||
use tracing_subscriber::FmtSubscriber;
|
||||
|
||||
use wrbapp_server::database;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
// Initialize logging
|
||||
let subscriber = FmtSubscriber::builder()
|
||||
.with_max_level(Level::INFO)
|
||||
.finish();
|
||||
|
||||
tracing::subscriber::set_global_default(subscriber)
|
||||
.expect("Error while initialized tracing subscriber");
|
||||
|
||||
// Initialize database connection
|
||||
database::apply_migrations()
|
||||
.await
|
||||
.expect("Database migrations failed");
|
||||
|
||||
database::connect()
|
||||
.await
|
||||
.expect("Database connection failed");
|
||||
|
||||
// Serve app
|
||||
let app = Router::new().route("/", get(hello_world));
|
||||
|
||||
let listener = TcpListener::bind("127.0.0.1:3000")
|
||||
.await
|
||||
.expect("Error while initializing listener");
|
||||
|
||||
tracing::info!("Listening on {}", listener.local_addr().unwrap());
|
||||
|
||||
axum::serve(listener, app)
|
||||
.await
|
||||
.expect("Error while serving axum application");
|
||||
}
|
||||
|
||||
async fn hello_world() -> Result<String, (StatusCode, String)> {
|
||||
Ok("Hello world".to_string())
|
||||
}
|
Reference in New Issue
Block a user