Update to Rust 2021

Also update dependencies and codebase to reflect 2021 standards and changes (tokio tests, etc)
This commit is contained in:
Nick Bland
2022-01-03 18:13:39 +10:00
parent bfcaa6f487
commit fcf0bea45f
9 changed files with 225 additions and 231 deletions
+23 -20
View File
@@ -2,6 +2,7 @@ use std::convert::{TryFrom, TryInto};
use serde_aux::field_attributes::deserialize_number_from_string;
use sqlx::postgres::{PgConnectOptions, PgSslMode};
use sqlx::ConnectOptions;
#[derive(serde::Deserialize)]
pub struct Settings {
@@ -27,6 +28,28 @@ pub struct DatabaseSettings {
pub require_ssl: bool,
}
impl DatabaseSettings {
pub fn without_db(&self) -> PgConnectOptions {
let ssl_mode = if self.require_ssl {
PgSslMode::Require
} else {
PgSslMode::Prefer
};
PgConnectOptions::new()
.host(&self.host)
.username(&self.username)
.password(&self.password)
.port(self.port)
.ssl_mode(ssl_mode)
}
pub fn with_db(&self) -> PgConnectOptions {
let mut options = self.without_db().database(&self.database_name);
options.log_statements(tracing::log::LevelFilter::Trace);
options
}
}
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
// Initialise configuration reader
let mut settings = config::Config::default();
@@ -77,24 +100,4 @@ impl TryFrom<String> for Environment {
)),
}
}
}
impl DatabaseSettings {
pub fn without_db(&self) -> PgConnectOptions {
let ssl_mode = if self.require_ssl {
PgSslMode::Require
} else {
PgSslMode::Prefer
};
PgConnectOptions::new()
.host(&self.host)
.username(&self.username)
.password(&self.password)
.port(self.port)
.ssl_mode(ssl_mode)
}
pub fn with_db(&self) -> PgConnectOptions {
self.without_db().database(&self.database_name).log_statements(log::LevelFilter::Trace)
}
}
+1
View File
@@ -1,3 +1,4 @@
#![allow(clippy::toplevel_ref_arg)]
pub mod configuration;
pub mod routes;
pub mod startup;
+1 -1
View File
@@ -5,7 +5,7 @@ use mail_app::startup::run;
use mail_app::configuration::get_configuration;
use mail_app::telemetry::{get_subscriber, init_subscriber};
#[actix_web::main]
#[tokio::main]
async fn main() -> std::io::Result<()> {
let subscriber = get_subscriber("mail_app".into(), "info".into(), std::io::stdout);
init_subscriber(subscriber);
+3 -5
View File
@@ -32,13 +32,11 @@ pub async fn subscribe(form: web::Form<FormData>, pool: web::Data<PgPool>,) -> H
skip(form, pool)
)]
pub async fn insert_subscriber(pool: &PgPool, form: &FormData) -> Result<(), sqlx::Error> {
// _connect uses PgPool from the application state as defined in startup.rs
// Use a match statement for the query as it only returns two outcomes
sqlx::query!(
r#"
INSERT INTO subscriptions (id, email, name, subscribed_at)
VALUES($1, $2, $3, $4)
"#,
INSERT INTO subscriptions (id, email, name, subscribed_at)
VALUES ($1, $2, $3, $4)
"#,
Uuid::new_v4(),
form.email,
form.name,
+3 -3
View File
@@ -1,6 +1,6 @@
use actix_web::{web, App, HttpServer};
use actix_web::dev::Server;
// use actix_web::web::Data;
use actix_web::web::Data;
use std::net::TcpListener;
use sqlx::PgPool;
use tracing_actix_web::TracingLogger;
@@ -8,13 +8,13 @@ use tracing_actix_web::TracingLogger;
use crate::routes::{health_check, subscribe};
pub fn run(listener: TcpListener, db_pool: PgPool) -> Result<Server, std::io::Error> {
let db_pool = web::Data::new(db_pool); // Wrap connection in a smart pointer
let db_pool = Data::new(db_pool);
let server = HttpServer::new(move || {
App::new()
.wrap(TracingLogger::default())
.route("/health_check", web::get().to(health_check))
.route("/subscriptions", web::post().to(subscribe))
.app_data(db_pool.clone()) // Get pointer copy and attach to application state
.app_data(db_pool.clone())
})
.listen(listener)?
.run();
+8 -9
View File
@@ -4,17 +4,16 @@ use tracing_log::LogTracer;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry, fmt::MakeWriter};
/// Compose multiple layers into a tracing compatible subscriber
pub fn get_subscriber(
pub fn get_subscriber<Sink>(
name: String,
env_filter: String,
sink: impl MakeWriter + Send + Sync + 'static,
) -> impl Subscriber + Sync + Send {
let env_filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new(env_filter));
let formatting_layer = BunyanFormattingLayer::new(
name,
sink
);
sink: Sink,
) -> impl Subscriber + Sync + Send
where
Sink: for<'a> MakeWriter<'a> + Send + Sync + 'static,
{
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter));
let formatting_layer = BunyanFormattingLayer::new(name, sink);
Registry::default()
.with(env_filter)
.with(JsonStorageLayer)