56 lines
1.3 KiB
Rust
56 lines
1.3 KiB
Rust
|
use sqlx::{query, Connection, SqliteConnection};
|
||
|
|
||
|
use crate::auth::UserToken;
|
||
|
|
||
|
// TODO: allow configuring this via envar
|
||
|
const DB_URI_DEFAULT: &str = "sqlite://sqlite.db";
|
||
|
|
||
|
pub trait Database {
|
||
|
async fn get_token(&self, username: &str) -> UserToken;
|
||
|
}
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct SqliteDatabase {
|
||
|
conn: SqliteConnection,
|
||
|
}
|
||
|
|
||
|
impl SqliteDatabase {
|
||
|
pub async fn open() -> Self {
|
||
|
let mut db = Self {
|
||
|
conn: SqliteConnection::connect(DB_URI_DEFAULT)
|
||
|
.await
|
||
|
.expect("Failed to open SQLite database"),
|
||
|
};
|
||
|
|
||
|
db.init().await;
|
||
|
|
||
|
db
|
||
|
}
|
||
|
|
||
|
pub async fn init(&mut self) {
|
||
|
query(
|
||
|
"CREATE TABLE IF NOT EXISTS user_tokens (
|
||
|
id INTEGER PRIMARY KEY NOT NULL,
|
||
|
username VARCHAR(255),
|
||
|
token NCHAR(30),
|
||
|
created DATETIME,
|
||
|
valid BOOLEAN,
|
||
|
);",
|
||
|
)
|
||
|
.execute(&mut self.conn)
|
||
|
.await
|
||
|
.expect("Failed to initialize table user_tokens");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Database for SqliteDatabase {
|
||
|
async fn get_token(&self, username: &str) -> UserToken {
|
||
|
todo!()
|
||
|
// query(
|
||
|
// "
|
||
|
// SELECT token, created, valid
|
||
|
// ",
|
||
|
// )
|
||
|
}
|
||
|
}
|