diff --git a/src/config.rs b/src/config.rs index cefc272..27a426e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use crate::Error; use serde::Deserialize; +use sqlx::postgres::PgConnectOptions; use tokio::fs::read_to_string; #[derive(Debug, Deserialize)] @@ -12,7 +13,7 @@ pub struct ConfigBuilder { pub struct Database { username: String, password: String, - hostname: String, + host: String, database: String, port: u16, } @@ -67,3 +68,14 @@ pub struct Web { pub port: u16, pub ssl: bool, } + +impl Database { + pub fn connect_options(&self) -> PgConnectOptions { + PgConnectOptions::new() + .database(&self.database) + .host(&self.host) + .username(&self.username) + .password(&self.password) + .port(self.port) + } +} diff --git a/src/main.rs b/src/main.rs index 989630c..78f1fb9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use actix_web::{App, HttpServer, web}; +use sqlx::{Executor, PgPool, Pool, Postgres}; use std::time::SystemTime; mod config; use config::{Config, ConfigBuilder}; @@ -8,6 +9,7 @@ type Error = Box; #[derive(Clone)] struct Data { + pub pool: Pool, pub config: Config, pub start_time: SystemTime, } @@ -18,7 +20,20 @@ async fn main() -> Result<(), Error> { let web = config.web.clone(); + let pool = PgPool::connect_with(config.database.connect_options()).await?; + + pool.execute(r#"CREATE TABLE IF NOT EXISTS users ( + uuid uuid UNIQUE NOT NULL, + username varchar(32) UNIQUE NOT NULL, + display_name varchar(64), + password varchar(512) NOT NULL, + email varchar(100) UNIQUE NOT NULL, + email_verified integer NOT NULL DEFAULT '0', + PRIMARY KEY (uuid) + )"#).await?; + let data = Data { + pool, config, start_time: SystemTime::now(), };