1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Dim is a media manager written in rust.
//! It uses Diesel as the ORM and rocket for the http/s server
//!
//! The project is split up into several crates:
//! * [`database`](database) - Holds all the database models including some frequently used db operations
//! * [`routes`](routes) - All of the routes that we expose over http are stored in there
//! * [`scanners`](scanners) - The filesystem scanner and daemon code is located here
//! ffmpeg that is used by several parts of dim
//!
//! # Building
//! Dim can easily be built with cargo build --release.
//! When built with --release, build.rs will compile the web ui and embed it into dim.
//!
//! # To run
//! Dim can be ran using docker, by pulling vgarleanu/dim-server, or locally.
//! If ran locally, make sure PostgreSQL is running with the password for postgres: dimpostgres
//!
//! # Testing
//! To test run `make test` in the root, or `cargo test` in the root of each module including the
//! root dir.

#![feature(min_specialization, let_else)]

use std::fs::create_dir_all;
use tracing_subscriber::fmt;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::EnvFilter;

/// Module contains our core initialization logic.
pub mod core;
/// Module contains all the error definitions used in dim, and returned by the web-service.
pub mod errors;
/// Module contains our external api interfaces
pub mod external;
/// Contains the code for fetching assets like posters and stills.
pub mod fetcher;
/// Inspect api for Result type
pub mod inspect;
/// Contains our custom logger for rocket
pub mod logger;
/// Sqlite CDC implementation
pub mod reactor;
/// Contains all of the routes exposed by the webapi.
pub mod routes;
/// New generation scanner infrastructure.
pub mod scanner;
/// Contains the fairing which tracks streams across rest api
pub mod stream_tracking;
/// Contains all the logic needed for streaming and on-the-fly transcoding.
pub mod streaming;
#[cfg(test)]
mod tests;
/// Tree-like structure for representing directories of files.
pub mod tree;
/// Various utilities
pub mod utils;
/// Websocket related logic.
pub mod websocket;

pub use routes::settings::get_global_settings;
pub use routes::settings::init_global_settings;
pub use routes::settings::set_global_settings;
pub use routes::settings::GlobalSettings;

/// Function builds a logger drain that drains to a json file located in logs/ and also to stdout.
pub fn setup_logging(_debug: bool) {
    let _ = create_dir_all("logs");

    if std::env::var("RUST_LOG").is_err() {
        std::env::set_var("RUST_LOG", "info");
    }

    let log_appender = tracing_appender::rolling::daily("./logs", "dim-log.log");
    let (non_blocking_file, _guard) = tracing_appender::non_blocking(log_appender);

    let subscriber = tracing_subscriber::registry()
        .with(EnvFilter::from_default_env())
        .with(fmt::layer().with_writer(std::io::stdout))
        .with(fmt::layer().json().with_writer(non_blocking_file));

    let _ = tracing::subscriber::set_global_default(subscriber);
}

#[cfg(test)]
pub fn setup_test_logging() {
    if std::env::var("RUST_LOG").is_err() {
        std::env::set_var("RUST_LOG", "info");
    }

    let subscriber = tracing_subscriber::registry()
        .with(EnvFilter::from_default_env())
        .with(
            fmt::layer()
                .with_span_events(fmt::format::FmtSpan::CLOSE | fmt::format::FmtSpan::NEW)
                .with_writer(tracing_subscriber::fmt::TestWriter::new()),
        );

    let _ = tracing::subscriber::set_global_default(subscriber);
}