feat: add in database support
This commit is contained in:
parent
bebad3be9b
commit
26b6601f5b
2 changed files with 28 additions and 1 deletions
|
@ -1,5 +1,6 @@
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use sqlx::postgres::PgConnectOptions;
|
||||||
use tokio::fs::read_to_string;
|
use tokio::fs::read_to_string;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
@ -12,7 +13,7 @@ pub struct ConfigBuilder {
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
username: String,
|
username: String,
|
||||||
password: String,
|
password: String,
|
||||||
hostname: String,
|
host: String,
|
||||||
database: String,
|
database: String,
|
||||||
port: u16,
|
port: u16,
|
||||||
}
|
}
|
||||||
|
@ -67,3 +68,14 @@ pub struct Web {
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
pub ssl: bool,
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -1,4 +1,5 @@
|
||||||
use actix_web::{App, HttpServer, web};
|
use actix_web::{App, HttpServer, web};
|
||||||
|
use sqlx::{Executor, PgPool, Pool, Postgres};
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
mod config;
|
mod config;
|
||||||
use config::{Config, ConfigBuilder};
|
use config::{Config, ConfigBuilder};
|
||||||
|
@ -8,6 +9,7 @@ type Error = Box<dyn std::error::Error>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Data {
|
struct Data {
|
||||||
|
pub pool: Pool<Postgres>,
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub start_time: SystemTime,
|
pub start_time: SystemTime,
|
||||||
}
|
}
|
||||||
|
@ -18,7 +20,20 @@ async fn main() -> Result<(), Error> {
|
||||||
|
|
||||||
let web = config.web.clone();
|
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 {
|
let data = Data {
|
||||||
|
pool,
|
||||||
config,
|
config,
|
||||||
start_time: SystemTime::now(),
|
start_time: SystemTime::now(),
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue