about summary refs log tree commit diff
path: root/src/main.rs
blob: 778aa4aa2fd1df56acfc55051f69fe1a5202ecff (plain) (blame)
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
use std::env;
use log::{error,info,debug};
use surf::Url;
use kittybox_micropub as micropub;

#[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 Micropub server...");

    let redis_uri: String;
    match env::var("REDIS_URI") {
        Ok(val) => {
            debug!("Redis connection: {}", val);
            redis_uri = val
        },
        Err(_) => {
            error!("REDIS_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 media_endpoint: Option<String> = env::var("MEDIA_ENDPOINT").ok();

    let host = env::var("SERVE_AT").ok().unwrap_or_else(|| "0.0.0.0:8080".to_string());
    let app = micropub::get_app_with_redis(token_endpoint, redis_uri, media_endpoint).await;
    app.listen(host).await
}