diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-10-08 08:57:30 -0400 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-10-08 08:58:47 -0400 |
commit | b727db0d64f4250742b0ebaac0149c1224a0d040 (patch) | |
tree | 7d789facb2b3f578e8182034b4cb7f0df5323476 /server | |
parent | 97598e7edfcdc97e665453d6f5cda316bd51291b (diff) |
Add db connection to server
Diffstat (limited to 'server')
-rw-r--r-- | server/migrations/20221008120534_init.down.sql | 1 | ||||
-rw-r--r-- | server/migrations/20221008120534_init.up.sql | 3 | ||||
-rw-r--r-- | server/src/main.rs | 46 |
3 files changed, 17 insertions, 33 deletions
diff --git a/server/migrations/20221008120534_init.down.sql b/server/migrations/20221008120534_init.down.sql index 3b80b80..c855f63 100644 --- a/server/migrations/20221008120534_init.down.sql +++ b/server/migrations/20221008120534_init.down.sql @@ -1,2 +1,3 @@ -- Add down migration script here drop table if exists users; +drop table if exists sessions; diff --git a/server/migrations/20221008120534_init.up.sql b/server/migrations/20221008120534_init.up.sql index bcd9c2c..301d2eb 100644 --- a/server/migrations/20221008120534_init.up.sql +++ b/server/migrations/20221008120534_init.up.sql @@ -1,7 +1,6 @@ -- Add up migration script here -create table users ( +create table sessions ( id uuid primary key, - username varchar(32) not null, access_token varchar(2048) not null, access_token_expiration timestamp with time zone not null, refresh_token varchar(512) not null diff --git a/server/src/main.rs b/server/src/main.rs index d948586..e3a84d9 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,53 +1,27 @@ use std::{collections::HashMap, env, sync::Arc}; use axum::{ - body::Body, - extract::{Extension, FromRequest, Query}, - http::{request::Parts, Request}, + extract::{Extension, Query}, response::Redirect, routing::get, Json, Router, }; -use openidconnect::{ - core::{CoreClient, CoreProviderMetadata, CoreResponseType}, - reqwest::async_http_client, - url::Url, - AccessTokenHash, AuthenticationFlow, AuthorizationCode, ClientId, ClientSecret, CsrfToken, - IssuerUrl, Nonce, OAuth2TokenResponse, PkceCodeChallenge, RedirectUrl, Scope, TokenResponse, -}; use protocol::UserInfo; use tower_cookies::{Cookie, CookieManagerLayer, Cookies}; use tower_http::trace::TraceLayer; -use tracing::{debug, info, trace}; +use tracing::info; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; mod auth; use crate::auth::{Authenticator, EndUserId}; +use sqlx::{postgres::PgPoolOptions, PgPool}; -struct ServerContext { +pub struct ServerContext { pub app_url: String, pub authenticator: Authenticator, + pub db: PgPool, } type ContextExtension = Extension<Arc<ServerContext>>; -async fn keycloak_client( - issuer_url: IssuerUrl, - client_id: ClientId, - client_secret: ClientSecret, - redirect_uri: RedirectUrl, -) -> CoreClient { - // // Use OpenID Connect Discovery to fetch the provider metadata. - let provider_metadata = CoreProviderMetadata::discover_async(issuer_url, async_http_client) - .await - .unwrap(); - - let client = - CoreClient::from_provider_metadata(provider_metadata, client_id, Some(client_secret)) - // Set the URL the user will be redirected to after the authorization process. - .set_redirect_uri(redirect_uri); - - client -} - #[tokio::main] async fn main() { dotenv::dotenv().ok(); @@ -59,6 +33,15 @@ async fn main() { .with(tracing_subscriber::fmt::layer()) .init(); + info!("Opening database connection"); + let db_url = env::var("DATABASE_URL").unwrap(); + let db_pool: PgPool = PgPoolOptions::new() + .max_connections(10) + .connect(&db_url).await.expect("db connection"); + + info!("Running db migrations"); + sqlx::migrate!().run(&db_pool).await.expect("db migration"); + let bind_address = env::var("BIND_ADDRESS").unwrap(); info!("Starting server on {}", bind_address); @@ -67,6 +50,7 @@ async fn main() { let state = Arc::new(ServerContext { app_url: app_url, authenticator: Authenticator::from_env().await, + db: db_pool, }); let app = Router::new() |