Compare commits

..

No commits in common. "c5c8f27ae67eb97c2b625012c7dc78afe1bd0fd7" and "e8ff6960067aa57e9bcaf3bff6bc1618fcbb9125" have entirely different histories.

2 changed files with 15 additions and 22 deletions

View File

@ -8,10 +8,11 @@ CREATE TABLE IF NOT EXISTS users (
email VARCHAR NOT NULL, email VARCHAR NOT NULL,
password VARCHAR NOT NULL, password VARCHAR NOT NULL,
role INTEGER DEFAULT 1 NOT NULL, role INTEGER DEFAULT 1 NOT NULL,
UNIQUE(username), UNIQUE(username, email)
UNIQUE(email)
); );
CREATE UNIQUE INDEX ux_username ON users(username);
CREATE TABLE IF NOT EXISTS links ( CREATE TABLE IF NOT EXISTS links (
id INTEGER PRIMARY KEY NOT NULL, id INTEGER PRIMARY KEY NOT NULL,
title VARCHAR NOT NULL, title VARCHAR NOT NULL,

View File

@ -5,7 +5,6 @@ use clap::{
use dotenv::dotenv; use dotenv::dotenv;
use sqlx::{migrate::Migrator, Pool, Sqlite}; use sqlx::{migrate::Migrator, Pool, Sqlite};
use std::{ use std::{
fs::File,
io::{self, BufRead, Write}, io::{self, BufRead, Write},
path::PathBuf, path::PathBuf,
}; };
@ -214,16 +213,14 @@ async fn parse_args_to_config(config: ArgMatches<'_>, log: Logger) -> ServerConf
pub(crate) async fn setup() -> Result<Option<crate::ServerConfig>, ServerError> { pub(crate) async fn setup() -> Result<Option<crate::ServerConfig>, ServerError> {
dotenv().ok(); dotenv().ok();
// initiallize the logger
let decorator = slog_term::TermDecorator::new().build(); let decorator = slog_term::TermDecorator::new().build();
let drain = slog_term::FullFormat::new(decorator).build().fuse(); let drain = slog_term::FullFormat::new(decorator).build().fuse();
let drain = slog_async::Async::new(drain).build().fuse(); let drain = slog_async::Async::new(drain).build().fuse();
let log = slog::Logger::root(drain, slog_o!("name" => "Pslink")); let log = slog::Logger::root(drain, slog_o!("name" => "Pslink"));
// Print launch info
slog_info!(log, "Launching Pslink a 'Private short link generator'"); slog_info!(log, "Launching Pslink a 'Private short link generator'");
slog_info!(log, "logging initialized"); slog_info!(log, ".env file setup, logging initialized");
let app = generate_cli(); let app = generate_cli();
@ -239,22 +236,17 @@ pub(crate) async fn setup() -> Result<Option<crate::ServerConfig>, ServerError>
.parse::<PathBuf>() .parse::<PathBuf>()
.expect("Failed to parse Database path."); .expect("Failed to parse Database path.");
if !db.exists() { if !db.exists() {
if config.subcommand_matches("migrate-database").is_none() { let msg = format!(
let msg = format!( concat!(
concat!( "Database not found at {}!",
"Database not found at {}!", " Create a new database with: `pslink migrate-database`",
" Create a new database with: `pslink migrate-database`", "or adjust the databasepath."
"or adjust the databasepath." ),
), db.display()
db.display() );
); slog_error!(log, "{}", msg);
slog_error!(log, "{}", msg); eprintln!("{}", msg);
eprintln!("{}", msg); return Ok(None);
return Ok(None);
}
// create an empty database file the if above makes sure that this file does not exist.
File::create(db)?;
}; };
let server_config: crate::ServerConfig = parse_args_to_config(config.clone(), log).await; let server_config: crate::ServerConfig = parse_args_to_config(config.clone(), log).await;