feat: dockerize p4bl0t
This commit removes DATABASE_URL variable in favour of a fixed name. The project won’t panic anymore if this variable isn’t set. This removes the need for dotenvy. It also adds the necessary files to dockerize the application. Update instructions in README on how to run the project. Add possibility to compile the project without a database available. Closes #8
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
#![allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
|
||||
|
||||
use std::env;
|
||||
|
||||
use poise::serenity_prelude::{ChannelId, GuildId};
|
||||
use sqlx::SqlitePool;
|
||||
use tracing::error;
|
||||
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
|
||||
use tracing::{error, info, debug};
|
||||
|
||||
pub type Result<T> = ::std::result::Result<T, sqlx::Error>;
|
||||
|
||||
@@ -16,22 +14,23 @@ impl Database {
|
||||
/// The Sqlite database should already exist and have its
|
||||
/// migrations already executed.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the environment variable `DATABASE_URL` is not set.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function will return an error if the Sqlite pool fails to
|
||||
/// create.
|
||||
pub async fn new() -> Result<Self> {
|
||||
Ok(Self(
|
||||
SqlitePool::connect(
|
||||
&env::var("DATABASE_URL")
|
||||
.expect("Missing enviroment variable DATABASE_URL"),
|
||||
)
|
||||
.await?,
|
||||
))
|
||||
let url = "sqlite:p4bl0t.db";
|
||||
if !Sqlite::database_exists(url).await? {
|
||||
info!("Creating database");
|
||||
Sqlite::create_database(url).await?;
|
||||
info!("Database created");
|
||||
}
|
||||
debug!("Getting pool connection");
|
||||
let pool = SqlitePool::connect(url).await?;
|
||||
info!("Running migrations");
|
||||
sqlx::migrate!().run(&pool).await?;
|
||||
debug!("Database initialized");
|
||||
Ok(Self(pool))
|
||||
}
|
||||
|
||||
/// Return from database all channels registered as loggers for a
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
#![warn(clippy::style, clippy::pedantic)]
|
||||
|
||||
mod utils;
|
||||
mod db;
|
||||
mod discord;
|
||||
mod utils;
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
dotenvy::dotenv()?;
|
||||
color_eyre::install()?;
|
||||
utils::setup_logging();
|
||||
color_eyre::install()?;
|
||||
|
||||
let bot = discord::make_bot();
|
||||
bot.run().await?;
|
||||
|
||||
Reference in New Issue
Block a user