aboutsummaryrefslogtreecommitdiff
path: root/apps/servers/octopus/src/main.rs
blob: c8660bb92218a38cef6121142ccb86627def6d1a (plain)
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
//! Octopus git monorepo explorer
//!
//! This file is the entry-point to the octopus server (previously
//! called `webgit` - some mentions to this name are still in the
//! code).
//!
//! Because of the way that the libgit2 rust abstraction handles
//! state, we can't embed a handle to the repository into the
//! application app state; instead each endpoint (meaning route) will
//! load and index the repository itself.  Because all operations in
//! supergit are lazy this does not add too much overhead.  If there
//! are operations that take too long, we can start building an
//! explicit cache for them.

#[macro_use]
extern crate log;

pub(crate) mod git;
pub(crate) mod pages;
pub(crate) mod project;
pub(crate) mod repo;
pub(crate) mod state;
pub(crate) mod templ_data;

use actix_files as fs;
use actix_web::{web, App, HttpServer};
use std::io;
use std::path::PathBuf;

#[actix_rt::main]
async fn main() -> io::Result<()> {
    std::env::set_var("RUST_LOG", "actix_server=info,webgit=trace");
    env_logger::init();
    let root = PathBuf::new();

    // Check that we know where the repo is.
    if std::env::var("OCTOPUS_REPOSITORY").is_err() {
        error!("Failed to determine repository path!  Please set the `OCTOPUS_REPOSITORY` env variable!");
        std::process::exit(2);
    }

    // Start a new server with a few basic routes
    HttpServer::new(move || {
        App::new()
            .service(fs::Files::new("/static", root.join("static")))
            .service(web::resource("/").route(web::get().to(pages::overview)))

            // Match on tree/* where paths can be arbitrarily recursive
            .service(web::resource("/tree/{path:[^{}]*}").route(web::get().to(pages::files)))
            .default_service(web::resource("").route(web::get().to(pages::p404)))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}