mirror of https://github.com/raftario/filite.git
Ditch cfg_if and remove lib
This commit is contained in:
parent
1bbfccec9f
commit
cc0f304563
|
@ -589,7 +589,6 @@ dependencies = [
|
|||
"actix-files 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"actix-web 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"diesel 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -8,13 +8,11 @@ edition = "2018"
|
|||
actix-files = "0.1.5"
|
||||
actix-web = "1.0.8"
|
||||
base64 = "0.10.1"
|
||||
cfg-if = "0.1.10"
|
||||
chrono = "0.4.9"
|
||||
dirs = "2.0.2"
|
||||
env_logger = "0.7.0"
|
||||
futures = "0.1.29"
|
||||
num_cpus = "1.10.1"
|
||||
toml = "0.5.3"
|
||||
[dependencies.diesel]
|
||||
version = "1.4.2"
|
||||
features = ["r2d2", "sqlite"]
|
||||
|
@ -27,3 +25,6 @@ features = ["derive"]
|
|||
|
||||
[target.'cfg(debug_assertions)'.dependencies]
|
||||
dotenv = "0.14.1"
|
||||
|
||||
[target.'cfg(not(debug_assertions))'.dependencies]
|
||||
toml = "0.5.3"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use filite::setup::Config;
|
||||
use crate::setup::Config;
|
||||
|
||||
use actix_web::{web, HttpResponse, Responder};
|
||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
|
@ -60,9 +60,9 @@ pub fn get_config(config: web::Data<Config>) -> impl Responder {
|
|||
pub mod files {
|
||||
use crate::handlers::id_from_b36;
|
||||
|
||||
use filite::queries::{self, SelectFilters, SelectQuery};
|
||||
use filite::setup::Config;
|
||||
use filite::Pool;
|
||||
use crate::queries::{self, SelectFilters, SelectQuery};
|
||||
use crate::setup::Config;
|
||||
use crate::Pool;
|
||||
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::error::BlockingError;
|
||||
|
@ -170,8 +170,8 @@ pub mod files {
|
|||
pub mod links {
|
||||
use crate::handlers::{id_from_b36, timestamp_to_last_modified};
|
||||
|
||||
use filite::queries::{self, SelectFilters, SelectQuery};
|
||||
use filite::Pool;
|
||||
use crate::queries::{self, SelectFilters, SelectQuery};
|
||||
use crate::Pool;
|
||||
|
||||
use actix_web::{web, Error, HttpResponse};
|
||||
use futures::future::{self, Either};
|
||||
|
@ -219,8 +219,8 @@ pub mod links {
|
|||
pub mod texts {
|
||||
use crate::handlers::{id_from_b36, timestamp_to_last_modified};
|
||||
|
||||
use filite::queries::{self, SelectFilters, SelectQuery};
|
||||
use filite::Pool;
|
||||
use crate::queries::{self, SelectFilters, SelectQuery};
|
||||
use crate::Pool;
|
||||
|
||||
use actix_web::{web, Error, HttpResponse};
|
||||
use futures::future::{self, Either};
|
||||
|
|
17
src/lib.rs
17
src/lib.rs
|
@ -1,17 +0,0 @@
|
|||
#[macro_use]
|
||||
extern crate cfg_if;
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
#[macro_use]
|
||||
extern crate serde;
|
||||
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
|
||||
pub mod models;
|
||||
pub mod queries;
|
||||
pub mod schema;
|
||||
pub mod setup;
|
||||
|
||||
/// SQLite database connection pool
|
||||
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
34
src/main.rs
34
src/main.rs
|
@ -1,14 +1,26 @@
|
|||
#[macro_use]
|
||||
extern crate cfg_if;
|
||||
extern crate diesel;
|
||||
#[macro_use]
|
||||
extern crate serde;
|
||||
|
||||
use filite::setup::{self, Config};
|
||||
use crate::setup::Config;
|
||||
|
||||
use actix_web::{web, App, FromRequest, HttpServer};
|
||||
use std::{process};
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use std::process;
|
||||
|
||||
mod handlers;
|
||||
#[cfg(not(debug_assertions))]
|
||||
use std::fs;
|
||||
|
||||
pub mod handlers;
|
||||
pub mod models;
|
||||
pub mod queries;
|
||||
pub mod schema;
|
||||
pub mod setup;
|
||||
|
||||
/// SQLite database connection pool
|
||||
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||
|
||||
/// Performs the initial setup
|
||||
#[cfg(not(debug_assertions))]
|
||||
|
@ -37,12 +49,14 @@ fn init() -> Config {
|
|||
|
||||
fn main() {
|
||||
let config = {
|
||||
cfg_if! {
|
||||
if #[cfg(debug_assertions)] {
|
||||
Config::debug()
|
||||
} else {
|
||||
init()
|
||||
}
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
Config::debug()
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
init()
|
||||
}
|
||||
};
|
||||
setup::init_logger();
|
||||
|
|
29
src/setup.rs
29
src/setup.rs
|
@ -6,13 +6,14 @@ use actix_web::middleware::Logger;
|
|||
use diesel::r2d2::{self, ConnectionManager};
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
use dotenv;
|
||||
#[cfg(debug_assertions)]
|
||||
use std::str::FromStr;
|
||||
#[cfg(not(debug_assertions))]
|
||||
use std::fs;
|
||||
|
||||
/// Returns a path to the directory storing application data
|
||||
pub fn get_data_dir() -> PathBuf {
|
||||
|
@ -22,6 +23,7 @@ pub fn get_data_dir() -> PathBuf {
|
|||
}
|
||||
|
||||
/// Returns a path to the configuration file
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn get_config_path() -> PathBuf {
|
||||
let mut path = get_data_dir();
|
||||
path.push("config.toml");
|
||||
|
@ -44,7 +46,7 @@ macro_rules! parse_env {
|
|||
|
||||
/// Application configuration
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(default)]
|
||||
#[cfg_attr(not(debug_assertions), serde(default))]
|
||||
pub struct Config {
|
||||
/// Port to listen on
|
||||
pub port: u16,
|
||||
|
@ -58,6 +60,7 @@ pub struct Config {
|
|||
pub max_filesize: usize,
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
let port = 8080;
|
||||
|
@ -88,6 +91,7 @@ impl Default for Config {
|
|||
|
||||
impl Config {
|
||||
/// Deserialize the config file
|
||||
#[cfg(not(debug_assertions))]
|
||||
pub fn read_file() -> Result<Self, &'static str> {
|
||||
let path = get_config_path();
|
||||
let contents = if let Ok(contents) = fs::read_to_string(&path) {
|
||||
|
@ -129,6 +133,7 @@ impl Config {
|
|||
}
|
||||
|
||||
/// Serialize the config file
|
||||
#[cfg(not(debug_assertions))]
|
||||
pub fn write_file(&self) -> Result<(), &'static str> {
|
||||
let path = get_config_path();
|
||||
let contents = toml::to_string(&self).expect("Can't serialize config.");
|
||||
|
@ -192,18 +197,20 @@ pub fn init_logger() {
|
|||
env_logger::init();
|
||||
}
|
||||
|
||||
/// Returs the logger middleware
|
||||
/// Returns the logger middleware
|
||||
pub fn logger_middleware() -> Logger {
|
||||
cfg_if! {
|
||||
if #[cfg(debug_assertions)] {
|
||||
dotenv::dotenv().ok();
|
||||
if let Ok(format) = env::var("LOG_FORMAT") {
|
||||
Logger::new(&format)
|
||||
} else {
|
||||
Logger::default()
|
||||
}
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
dotenv::dotenv().ok();
|
||||
if let Ok(format) = env::var("LOG_FORMAT") {
|
||||
Logger::new(&format)
|
||||
} else {
|
||||
Logger::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
Logger::default()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue