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
100
101
102
103
|
use log::{debug, error, info};
use std::env;
use surf::Url;
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
// TODO json logging in the future?
let logger_env = env_logger::Env::new().filter_or("RUST_LOG", "info");
env_logger::init_from_env(logger_env);
info!("Starting the kittybox server...");
let backend_uri: String;
match env::var("BACKEND_URI") {
Ok(val) => {
debug!("Backend URI: {}", val);
backend_uri = val
}
Err(_) => {
error!("BACKEND_URI is not set, cannot find a database");
std::process::exit(1);
}
};
let token_endpoint: Url;
match env::var("TOKEN_ENDPOINT") {
Ok(val) => {
debug!("Token endpoint: {}", val);
match Url::parse(&val) {
Ok(val) => token_endpoint = val,
_ => {
error!("Token endpoint URL cannot be parsed, aborting.");
std::process::exit(1)
}
}
}
Err(_) => {
error!("TOKEN_ENDPOINT is not set, will not be able to authorize users!");
std::process::exit(1)
}
}
let authorization_endpoint: Url;
match env::var("AUTHORIZATION_ENDPOINT") {
Ok(val) => {
debug!("Auth endpoint: {}", val);
match Url::parse(&val) {
Ok(val) => authorization_endpoint = val,
_ => {
error!("Authorization endpoint URL cannot be parsed, aborting.");
std::process::exit(1)
}
}
}
Err(_) => {
error!("AUTHORIZATION_ENDPOINT is not set, will not be able to confirm token and ID requests using IndieAuth!");
std::process::exit(1)
}
}
let media_endpoint: Option<String> = env::var("MEDIA_ENDPOINT").ok();
let internal_token: Option<String> = env::var("KITTYBOX_INTERNAL_TOKEN").ok();
let cookie_secret: String = match env::var("COOKIE_SECRET").ok() {
Some(value) => value,
None => {
if let Some(filename) = env::var("COOKIE_SECRET_FILE").ok() {
use async_std::io::ReadExt;
let mut file = async_std::fs::File::open(filename).await?;
let mut temp_string = String::new();
file.read_to_string(&mut temp_string).await?;
temp_string
} else {
error!("COOKIE_SECRET or COOKIE_SECRET_FILE is not set, will not be able to log in users securely!");
std::process::exit(1);
}
}
};
let host = env::var("SERVE_AT")
.ok()
.unwrap_or_else(|| "0.0.0.0:8080".to_string());
if backend_uri.starts_with("redis") {
println!("The Redis backend is deprecated.");
std::process::exit(1);
} else if backend_uri.starts_with("file") {
let app = kittybox::get_app_with_file(
token_endpoint,
authorization_endpoint,
backend_uri,
media_endpoint,
cookie_secret,
internal_token,
)
.await;
app.listen(host).await
} else {
println!("Unknown backend, not starting.");
std::process::exit(1);
}
}
|