about summary refs log tree commit diff
path: root/kittybox-rs/src/main.rs
blob: 9e81aad5a285056bd6cc8224926ce298a71e87be (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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use kittybox::database::FileStorage;
use std::{env, time::Duration};
use tracing::{debug, error, info};
use url::Url;

#[tokio::main]
async fn main() {
    // TODO use tracing instead of log
    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
    Registry::default()
        .with(EnvFilter::from_default_env())
        .with(tracing_subscriber::fmt::layer().json())
        .init();

    info!("Starting the kittybox server...");

    let backend_uri: String = match env::var("BACKEND_URI") {
        Ok(val) => {
            debug!("Backend URI: {}", val);
            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) => 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) => 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 listen_at = match env::var("SERVE_AT")
        .ok()
        .unwrap_or_else(|| "[::]:8080".to_string())
        .parse::<std::net::SocketAddr>()
    {
        Ok(addr) => addr,
        Err(e) => {
            error!("Cannot parse SERVE_AT: {}", e);
            std::process::exit(1);
        }
    };

    let http: reqwest::Client = {
        #[allow(unused_mut)]
        let mut builder = reqwest::Client::builder().user_agent(concat!(
            env!("CARGO_PKG_NAME"),
            "/",
            env!("CARGO_PKG_VERSION")
        ));
        // TODO: add a root certificate if there's an environment variable pointing at it
        //builder = builder.add_root_certificate(reqwest::Certificate::from_pem(todo!()));

        builder.build().unwrap()
    };

    if backend_uri.starts_with("redis") {
        println!("The Redis backend is deprecated.");
        std::process::exit(1);
    } else if backend_uri.starts_with("file") {
        let database = {
            let folder = backend_uri.strip_prefix("file://").unwrap();
            let path = std::path::PathBuf::from(folder);
            match kittybox::database::FileStorage::new(path).await {
                Ok(db) => db,
                Err(err) => {
                    error!("Error creating database: {:?}", err);
                    std::process::exit(1);
                }
            }
        };

        let blobstore = {
            let variable = std::env::var("BLOBSTORE_URI")
                .unwrap();
            let folder = variable
                .strip_prefix("file://")
                .unwrap();
            let path = std::path::PathBuf::from(folder);
            kittybox::media::storage::file::FileStore::new(path)
        };

        // This code proves that different components of Kittybox can
        // be split up without hurting the app
        //
        // If needed, some features could be omitted from the binary
        // or just not spun up in the future
        //
        // For example, the frontend code could run spearately from
        // Micropub and only have read access to the database folder
        let frontend = axum::Router::new()
            .route(
                "/",
                axum::routing::get(kittybox::frontend::homepage::<FileStorage>)
                    .layer(axum::Extension(database.clone())))
            .route("/.kittybox/static/:path", axum::routing::get(kittybox::frontend::statics))
            .fallback(
                axum::routing::get(kittybox::frontend::catchall::<FileStorage>)
                    .layer(axum::Extension(database.clone())));

        // Onboarding is a bit of a special case. One might argue that
        // the onboarding makes Kittybox a monolith. This is wrong.
        // The "onboarding receiver" doesn't need any code from the
        // onboarding form - they're grouped in a single module for
        // convenience only, since modifying one usually requires
        // updating the other to match.
        //
        // For example, this "router" just groups two separate methods
        // in one request, because logically they live in the same
        // subtree. But one could manually construct only one but not
        // the other, to receive a "frontend-only" application. Of
        // course, in this scenario, one must employ a reverse proxy
        // to distinguish between GET and POST requests to the same
        // path, and route them to the correct set of endpoints with
        // write access.
        let onboarding = axum::Router::new()
            .route("/.kittybox/onboarding", kittybox::frontend::onboarding::router(
                database.clone(), http.clone()
            ));

        let micropub = axum::Router::new()
            .route("/.kittybox/micropub", kittybox::micropub::router(database.clone(), http.clone()))
            .nest("/.kittybox/micropub/client", kittybox::companion::router());

        let media = axum::Router::new()
            .nest("/.kittybox/media", kittybox::media::router(blobstore).layer(axum::Extension(http)));

        //let indieauth = kittybox::indieauth::router();

        let technical = axum::Router::new()
            .route(
                "/.kittybox/coffee",
                axum::routing::get(|| async {
                    use axum::http::{header, StatusCode};
                    (
                        StatusCode::IM_A_TEAPOT,
                        [(header::CONTENT_TYPE, "text/plain")],
                        "Sorry, can't brew coffee yet!",
                    )
                }),
            )
            .route(
                "/.kittybox/health",
                axum::routing::get(
                    |axum::Extension(db): axum::Extension<FileStorage>| async move {
                        // TODO health-check the database
                        "OK"
                    }
                )
                    .layer(axum::Extension(database))
            )
            .route(
                "/.kittybox/metrics",
                axum::routing::get(|| async { todo!() }),
            );

        let svc = axum::Router::new()
            .merge(frontend)
            .merge(onboarding)
            .merge(micropub)
            .merge(media)
            //.merge(indieauth)
            .merge(technical)
            .layer(axum::Extension(kittybox::tokenauth::TokenEndpoint(token_endpoint)))
            .layer(tower::ServiceBuilder::new()
                   .layer(tower_http::trace::TraceLayer::new_for_http())
                   .into_inner())
            .layer(tower_http::catch_panic::CatchPanicLayer::new());

        // A little dance to turn a potential file descriptor into
        // a guaranteed async network socket
        let tcp_listener: std::net::TcpListener = {
            let mut listenfd = listenfd::ListenFd::from_env();

            let tcp_listener = if let Ok(Some(listener)) = listenfd.take_tcp_listener(0) {
                listener
            } else {
                std::net::TcpListener::bind(listen_at).unwrap()
            };
            // Set the socket to non-blocking so tokio can poll it
            // properly -- this is the async magic!
            tcp_listener.set_nonblocking(true).unwrap();

            tcp_listener
        };
        info!("Listening on {}", tcp_listener.local_addr().unwrap());

        let server = hyper::server::Server::from_tcp(tcp_listener)
            .unwrap()
            // Otherwise Chrome keeps connections open for too long
            .tcp_keepalive(Some(Duration::from_secs(30 * 60)))
            .serve(svc.into_make_service())
            .with_graceful_shutdown(async move {
                // Defer to C-c handler whenever we're not on Unix
                // TODO consider using a diverging future here
                #[cfg(not(unix))]
                return tokio::signal::ctrl_c().await.unwrap();
                #[cfg(unix)]
                {
                    use tokio::signal::unix::{signal, SignalKind};

                    signal(SignalKind::terminate())
                        .unwrap()
                        .recv()
                        .await
                        .unwrap()
                }
            });

        if let Err(err) = server.await {
            error!("Error serving requests: {}", err);
            std::process::exit(1);
        }
    } else {
        println!("Unknown backend, not starting.");
        std::process::exit(1);
    }
}