feat: add configurability
This commit is contained in:
parent
89ea0995bb
commit
8257255dc2
10 changed files with 1406 additions and 26 deletions
|
|
@ -11,3 +11,15 @@ repos:
|
|||
hooks:
|
||||
- id: sqlfluff-lint
|
||||
args: [--dialect, "postgres"]
|
||||
- repo: https://github.com/doublify/pre-commit-rust
|
||||
rev: v1.0
|
||||
hooks:
|
||||
- id: fmt
|
||||
- id: cargo-check
|
||||
- id: clippy
|
||||
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
|
||||
rev: v9.10.0
|
||||
hooks:
|
||||
- id: commitlint
|
||||
stages: [commit-msg]
|
||||
additional_dependencies: ["@commitlint/config-conventional"]
|
||||
|
|
|
|||
1331
Cargo.lock
generated
1331
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
15
Cargo.toml
15
Cargo.toml
|
|
@ -23,11 +23,24 @@ serde_json = "1"
|
|||
axum = { version = "0.7" }
|
||||
# tower-http = { version = "0.5", features = ["fs"] }
|
||||
# tower-cookies = "0.10"
|
||||
# # Others
|
||||
# Others
|
||||
config = "0.13"
|
||||
# lazy-regex = "3"
|
||||
# async-trait = "0.1"
|
||||
# strum_macros = "0.25"
|
||||
# uuid = { version = "1", features = ["v4", "fast-rng"] }
|
||||
|
||||
[dependencies.sqlx]
|
||||
version = "0.7"
|
||||
default-features = false
|
||||
features = [
|
||||
"runtime-tokio-rustls",
|
||||
"macros",
|
||||
"postgres",
|
||||
"uuid",
|
||||
"chrono",
|
||||
"migrate"
|
||||
]
|
||||
|
||||
[dev-dependencies]
|
||||
reqwest = "0.11"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,14 @@
|
|||
TODO: Add general information about this project
|
||||
|
||||
TODO: Explain usage of docker vs podman
|
||||
|
||||
## TODO: explain DB migration
|
||||
|
||||
To migrate a already deployed and running database, use
|
||||
```sh
|
||||
SKIP_DB_RUN=true ./scripts/init_db.sh
|
||||
```
|
||||
|
||||
## Contribution
|
||||
|
||||
Conventions are enforced through commitizen using pre-commit.
|
||||
|
|
|
|||
7
configuration.yaml
Normal file
7
configuration.yaml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
application_port: 8000
|
||||
database:
|
||||
host: "127.0.0.1"
|
||||
port: 5432
|
||||
username: "postgres"
|
||||
password: "password"
|
||||
database_name: "newsletter"
|
||||
|
|
@ -15,6 +15,9 @@ if ! [ -x "$(command -v sqlx)" ]; then
|
|||
exit 1
|
||||
fi
|
||||
|
||||
# instead of podman, docker could also be used instead
|
||||
CONTAINER_MANAGER="${CONTAINERIZER:=podman}"
|
||||
|
||||
DB_USER=${POSTGRES_USER:=postgres}
|
||||
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
|
||||
DB_NAME="${POSTGRES_DB:=newsletter}"
|
||||
|
|
@ -23,7 +26,7 @@ DB_PORT="${POSTGRES_PORT:=5432}"
|
|||
# Allow to skip container run if a containerized Postgres database is already running
|
||||
if [[ -z "${SKIP_DB_RUN}" ]]
|
||||
then
|
||||
podman run \
|
||||
${CONTAINER_MANAGER} run \
|
||||
-e POSTGRES_USER=${DB_USER} \
|
||||
-e POSTGRES_PASSWORD=${DB_PASSWORD} \
|
||||
-e POSTGRES_DB=${DB_NAME} \
|
||||
|
|
@ -39,7 +42,9 @@ done
|
|||
|
||||
>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!"
|
||||
|
||||
export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME}
|
||||
DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME}
|
||||
|
||||
export DATABASE_URL
|
||||
sqlx database create
|
||||
sqlx migrate run
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
#[derive(serde::Deserialize)]
|
||||
pub struct Settings {
|
||||
pub database: DatabaseSettings,
|
||||
pub application_port: u16,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct DatabaseSettings {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
pub database_name: String,
|
||||
}
|
||||
|
||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
// Initialise our configuration reader
|
||||
let settings = config::Config::builder()
|
||||
// Add configuration values from a file named `configuration.yaml`.
|
||||
.add_source(config::File::new(
|
||||
"configuration.yaml",
|
||||
config::FileFormat::Yaml,
|
||||
))
|
||||
.build()?;
|
||||
// Try to convert the configuration values it read into
|
||||
// our Settings type
|
||||
settings.try_deserialize::<Settings>()
|
||||
}
|
||||
|
||||
impl DatabaseSettings {
|
||||
pub fn connection_string(&self) -> String {
|
||||
format!(
|
||||
"postgres://{}:{}@{}:{}/{}",
|
||||
self.username, self.password, self.host, self.port, self.database_name
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
use tokio::net::TcpListener;
|
||||
use learn_axum::startup;
|
||||
use learn_axum::configuration::get_configuration;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let addr = format!("127.0.0.1:{}", "3000");
|
||||
let configuration = get_configuration().expect("Failed to read configuration.");
|
||||
let addr = format!("127.0.0.1:{}", configuration.application_port);
|
||||
let listener = TcpListener::bind(addr).await.unwrap(); //.expect("Unable to bind to port");
|
||||
startup::run(listener).await.unwrap();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,7 +78,9 @@ async fn spawn_app() -> TestApp {
|
|||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
tokio::spawn(async move {
|
||||
axum::serve(listener, learn_axum::startup::app()).await.unwrap();
|
||||
});
|
||||
axum::serve(listener, learn_axum::startup::app())
|
||||
.await
|
||||
.unwrap();
|
||||
});
|
||||
TestApp { addr }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue