Database setup started
This commit is contained in:
@@ -10,4 +10,5 @@ serde = { version = "1.0.217", features = ["derive"] }
|
||||
tokio = { version = "1.43.0", features = ["full"] }
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = "0.3.19"
|
||||
sigma = { path = "../sigma" }
|
||||
sigma = { path = "../sigma" }
|
||||
rusqlite = "0.37.0"
|
||||
|
||||
@@ -1,22 +1,58 @@
|
||||
use axum::{routing::get, Json, Router};
|
||||
use axum::{Json, Router, http::StatusCode, routing::get};
|
||||
#[allow(unused)]
|
||||
use log::{info, debug, warn, error, trace};
|
||||
use rusqlite::Connection;
|
||||
use sigma::repsonse::SigmaInformation;
|
||||
use tracing::instrument;
|
||||
|
||||
|
||||
const NAME: &str = "TinyIdentity";
|
||||
const VERSION: &str = "0.0.0.1";
|
||||
|
||||
const DB_PATH: &str = "identity.db";
|
||||
const DB_CREATE_USER_TABLE: &str =
|
||||
"CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
pass TEXT NOT NULL
|
||||
)";
|
||||
const DB_CREATE_META_TABLE: &str =
|
||||
"CREATE TABLE IF NOT EXISTS meta (
|
||||
key VARCHAR(16) PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
)";
|
||||
|
||||
|
||||
pub fn router() -> Router {
|
||||
info!("Loading {NAME} app routes");
|
||||
debug!("{NAME} version is {VERSION}");
|
||||
Router::new()
|
||||
.route("/", get(information))
|
||||
.route("/setup", get(dummy))
|
||||
.route("/setup", get(setup))
|
||||
.route("/me", get(dummy))
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn setup() -> Result<StatusCode, StatusCode> {
|
||||
let connection = Connection::open(DB_PATH);
|
||||
if let Err(e) = connection {
|
||||
error!("{:?}", e);
|
||||
return Err(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
let connection = connection.expect("Connection cannot be an error here.");
|
||||
|
||||
if let Err(e) = connection.execute(DB_CREATE_USER_TABLE, [],) {
|
||||
error!("{:?}", e);
|
||||
return Err(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
if let Err(e) = connection.execute(DB_CREATE_META_TABLE, [],) {
|
||||
error!("{:?}", e);
|
||||
return Err(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn dummy() -> Json<()> {
|
||||
Json(())
|
||||
|
||||
Reference in New Issue
Block a user