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
|
/*
This file is part of jellything (https://codeberg.org/metamuffin/jellything)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2024 metamuffin <metamuffin.org>
*/
#![feature(int_roundings, let_chains)]
use crate::routes::ui::{account::hash_password, admin::log::enable_logging};
use anyhow::Context;
use database::DataAcid;
use jellybase::{
database::{redb::ReadableTable, Ser, T_USER},
federation::Federation,
CONF, SECRETS,
};
use jellycommon::user::User;
use log::{error, info, warn};
use routes::build_rocket;
use tokio::fs::create_dir_all;
pub use jellybase::database;
pub mod routes;
#[rocket::main]
async fn main() {
enable_logging();
#[cfg(feature = "bypass-auth")]
log::warn!("authentification bypass enabled");
create_dir_all(&CONF.cache_path).await.unwrap();
let database = DataAcid::open(&CONF.database_path)
.context("opening database")
.unwrap();
let federation = Federation::initialize();
if let Some(username) = &CONF.admin_username
&& let Some(password) = &SECRETS.admin_password
{
let txn = database.begin_write().unwrap();
let mut users = txn.open_table(T_USER).unwrap();
let admin = users.get(username.as_str()).unwrap().map(|x| x.value().0);
users
.insert(
username.as_str(),
Ser(User {
admin: true,
name: username.clone(),
password: hash_password(&username, &password),
..admin.unwrap_or_else(|| User {
display_name: "Admin".to_string(),
..Default::default()
})
}),
)
.unwrap();
drop(users);
txn.commit().unwrap();
} else {
info!("admin account disabled")
}
let r = build_rocket(database, federation).launch().await;
match r {
Ok(_) => warn!("server shutdown"),
Err(e) => error!("server exited: {e}"),
}
}
|