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
#![doc = include_str!("../README.md")]

pub use app_router::router;
pub use config::CONFIG;
pub use controller::db_utils::{clear_invalid, get_one, ivec_to_u32, set_one, u8_slice_to_u32};
pub use controller::{feed::cron_feed, tantivy::Tan};
pub use error::AppError;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

mod app_router;
mod config;
mod controller;
mod error;

use data_encoding::HEXLOWER;
use once_cell::sync::Lazy;
use ring::digest::{Context, Digest, SHA256};
use sled::Db;
use std::{
    env,
    fs::File,
    io::{BufReader, Read},
};
use tracing::info;

const GIT_COMMIT: &str = env!("GIT_COMMIT");

/// Returns SHA256 of the current running executable.
/// Cookbook: [Calculate the SHA-256 digest of a file](https://rust-lang-nursery.github.io/rust-cookbook/cryptography/hashing.html)
static CURRENT_SHA256: Lazy<String> = Lazy::new(|| {
    fn sha256_digest<R: Read>(mut reader: R) -> Digest {
        let mut context = Context::new(&SHA256);
        let mut buffer = [0; 1024];

        loop {
            let count = reader.read(&mut buffer).unwrap();
            if count == 0 {
                break;
            }
            context.update(&buffer[..count]);
        }
        context.finish()
    }

    let file = env::current_exe().unwrap();
    let input = File::open(file).unwrap();
    let reader = BufReader::new(input);
    let digest = sha256_digest(reader);

    HEXLOWER.encode(digest.as_ref())
});

pub static DB: Lazy<Db> = Lazy::new(|| {
    info!("sha256: {}", *CURRENT_SHA256);
    info!(VERSION);
    info!(GIT_COMMIT);

    let db_url = &CONFIG.db;
    let config = sled::Config::default().path(db_url);
    let db = config.open().unwrap();
    info!(%db_url);
    db
});