Add basic SQLx
This commit is contained in:
parent
886df477a8
commit
c7e3468acc
6 changed files with 1036 additions and 11 deletions
1017
Cargo.lock
generated
1017
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -9,6 +9,10 @@ axum = "0.6.20"
|
|||
color-eyre = "0.6.2"
|
||||
envious = "0.2.2"
|
||||
serde = { version = "1.0.183", features = ["derive"] }
|
||||
sqlx = { version = "0.7.1", features = ["runtime-tokio", "postgres", "uuid", "migrate"] }
|
||||
tokio = { version = "1.31.0", features = ["full"] }
|
||||
tracing = "0.1.37"
|
||||
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
|
||||
|
||||
[profile.dev.package.sqlx-macros]
|
||||
opt-level = 3
|
||||
|
|
|
|||
5
build.rs
Normal file
5
build.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
// generated by `sqlx migrate build-script`
|
||||
fn main() {
|
||||
// trigger recompilation when a new migration is added
|
||||
println!("cargo:rerun-if-changed=migrations");
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@
|
|||
};
|
||||
in {
|
||||
devShell = pkgs.mkShell {
|
||||
nativeBuildInputs = [rust];
|
||||
nativeBuildInputs = [rust pkgs.sqlx-cli];
|
||||
RUST_PATH = "${rust}";
|
||||
RUST_DOC_PATH = "${rust}/share/doc/rust/html/std/index.html";
|
||||
};
|
||||
|
|
|
|||
2
migrations/20230814162207_accounts.sql
Normal file
2
migrations/20230814162207_accounts.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
-- Add migration script here
|
||||
CREATE TABLE accounts (id uuid PRIMARY KEY);
|
||||
17
src/main.rs
17
src/main.rs
|
|
@ -1,7 +1,8 @@
|
|||
use std::net::SocketAddr;
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
use axum::Router;
|
||||
use serde::Deserialize;
|
||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
fn default_port() -> u16 {
|
||||
|
|
@ -19,6 +20,7 @@ struct Settings {
|
|||
port: u16,
|
||||
#[serde(default = "default_address")]
|
||||
address: String,
|
||||
database_url: String,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
|
|
@ -31,6 +33,10 @@ impl Settings {
|
|||
}
|
||||
}
|
||||
|
||||
struct AppState {
|
||||
db: PgPool,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> color_eyre::Result<()> {
|
||||
color_eyre::install()?;
|
||||
|
|
@ -46,9 +52,16 @@ async fn main() -> color_eyre::Result<()> {
|
|||
|
||||
let addr: SocketAddr = format!("{}:{}", config.address, config.port).parse()?;
|
||||
|
||||
let db = PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(&config.database_url)
|
||||
.await?;
|
||||
|
||||
sqlx::migrate!().run(&db).await?;
|
||||
|
||||
tracing::info!("Listening on {addr}");
|
||||
|
||||
let router = Router::new();
|
||||
let router = Router::new().with_state(Arc::new(AppState { db }));
|
||||
|
||||
Ok(axum::Server::bind(&addr)
|
||||
.serve(router.into_make_service())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue