Skip to content

Commit

Permalink
fix pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrico Marconi committed Jul 27, 2023
1 parent 1b017a8 commit ad05a85
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 32 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=postgres://emarconi:supa_password@localhost/todo
2 changes: 1 addition & 1 deletion .github/workflows/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
SKIP_DOCKER=true ./scripts/init_db.sh
- name: Check sqlx-data.json is up-to-date
run: |
cargo sqlx prepare --check -- --bin zero2prod
cargo sqlx prepare --database-url postgres://emarconi:supa_password@localhost/todo --check -- --all-targets
- name: Run tests
run: cargo test

Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/target

.env
.session_secret
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod routes;
pub mod session;
pub mod settings;
pub mod startup;
pub mod state;
pub mod telemetry;
pub mod routes;
pub mod session;
pub mod user;
pub mod user;
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Context;
use coodo_be::{startup::Application, settings::get_settings, telemetry};
use coodo_be::{settings::get_settings, startup::Application, telemetry};

#[tokio::main(flavor = "multi_thread")]
async fn main() -> anyhow::Result<()> {
Expand Down
5 changes: 2 additions & 3 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ use crate::state::AppState;
mod session;

pub fn router() -> Router<AppState> {
Router::new()
.merge(session::routes())
}
Router::new().merge(session::routes())
}
7 changes: 3 additions & 4 deletions src/routes/session.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use axum::{Router, Json};
use axum::{Json, Router};
use axum_sessions::extractors::WritableSession;
use chrono::Duration;

use crate::{user::User, state::AppState};
use crate::{state::AppState, user::User};

pub fn routes() -> Router<AppState> {
use axum::routing::get;

Router::new()
.route("/session", get(get_session))
Router::new().route("/session", get(get_session))
}

#[tracing::instrument(skip_all, ret, name = "Get session")]
Expand Down
7 changes: 5 additions & 2 deletions src/session.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use axum::async_trait;
use axum_sessions::{async_session::{self, Session, SessionStore}, SessionLayer};
use axum_sessions::{
async_session::{self, Session, SessionStore},
SessionLayer,
};
use sqlx::{pool::PoolConnection, PgPool, Postgres};

pub fn get_session_layer(db_pool: PgPool) -> SessionLayer<UserSessionStore> {
use axum_sessions::PersistencePolicy;
use axum_sessions::PersistencePolicy;

let (session_store, secret) = UserSessionStore::new(db_pool);
SessionLayer::new(session_store, &secret)
Expand Down
24 changes: 14 additions & 10 deletions src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use std::net::TcpListener;
use axum::Router;
use sqlx::PgPool;

use crate::{settings::{Settings, DbSettings}, state::AppState, session::get_session_layer};
use crate::{
session::get_session_layer,
settings::{DbSettings, Settings},
state::AppState,
};

pub type Server =
hyper::Server<hyper::server::conn::AddrIncoming, axum::routing::IntoMakeService<Router>>;
Expand All @@ -22,7 +26,7 @@ impl Application {
let address = format!("{}:{}", &settings.app.host, settings.app.port);
TcpListener::bind(address)?
};

let server = make_server(listener, db_pool.clone())?;

Ok(Self {
Expand All @@ -38,13 +42,13 @@ impl Application {
}
}

pub fn make_server(
listener: TcpListener,
db_pool: PgPool,
) -> anyhow::Result<Server> {
use tower_http::{catch_panic::CatchPanicLayer, trace::{TraceLayer, self}};
use tracing::Level;
pub fn make_server(listener: TcpListener, db_pool: PgPool) -> anyhow::Result<Server> {
use anyhow::Context;
use tower_http::{
catch_panic::CatchPanicLayer,
trace::{self, TraceLayer},
};
use tracing::Level;

let state = AppState::new(db_pool.clone());
let router = Router::new()
Expand All @@ -53,7 +57,7 @@ pub fn make_server(
.layer(
TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
)
.layer(get_session_layer(db_pool))
.with_state(state);
Expand All @@ -71,4 +75,4 @@ pub fn get_db_pool(settings: &DbSettings) -> PgPool {
PgPoolOptions::new()
.acquire_timeout(Duration::from_secs(1))
.connect_lazy_with(settings.with_db())
}
}
4 changes: 2 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sqlx::{PgPool, Postgres, pool::PoolConnection};
use sqlx::{pool::PoolConnection, PgPool, Postgres};

#[derive(Debug, Clone)]
pub struct AppState {
Expand All @@ -13,4 +13,4 @@ impl AppState {
pub async fn db_connection(&self) -> sqlx::Result<PoolConnection<Postgres>> {
self.db_pool.acquire().await
}
}
}
6 changes: 6 additions & 0 deletions src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ impl UserHandleGenerator {
}
}

impl Default for UserHandleGenerator {
fn default() -> Self {
Self::new()
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UserHandle(String);

Expand Down
10 changes: 5 additions & 5 deletions tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ impl TestApp {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
let address = format!("http://127.0.0.1:{}", port);
let server =
coodo_be::startup::make_server(listener, pool.clone()).expect("Failed to create server");
let server = coodo_be::startup::make_server(listener, pool.clone())
.expect("Failed to create server");
let server_handle = tokio::spawn(server);

Self {
address,
port,
Expand All @@ -43,7 +43,8 @@ impl TestApp {
}

pub async fn get_user(&self, client: &mut Client) -> anyhow::Result<User> {
client.get(format!("{}/session", &self.address))
client
.get(format!("{}/session", &self.address))
.send()
.await
.context("Failed to send GET /session")?
Expand All @@ -52,4 +53,3 @@ impl TestApp {
.context("Failed to parse response body")
}
}

0 comments on commit ad05a85

Please sign in to comment.