about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/main.rs
blob: 453a2dc618ae02ab28e37f54f28d12bb89dc6603 (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
use actix_web::{App, HttpServer, middleware::Logger, web::Data};
use clap::Parser;
use utoipa::OpenApi;

use crate::cli::{CliArgs, Command};

mod api;
mod app;
mod cli;
mod storage;

#[actix_web::main]
#[expect(
    clippy::needless_for_each,
    reason = "utoipa generates this, we can't change it"
)]
async fn main() -> Result<(), std::io::Error> {
    #[derive(OpenApi)]
    #[openapi(
    paths(
            api::get::product_by_id,
            api::get::products,
            api::set::register_product,
            api::set::associate_barcode
        ),
    // security(
    //     (),
    //     ("my_auth" = ["read:items", "edit:items"]),
    //     ("token_jwt" = [])
    // ),
    )]
    struct ApiDoc;

    env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

    let args = CliArgs::parse();

    match args.command {
        Command::Serve => {
            let host = "127.0.0.1";
            let port = 8080;
            let data = Data::new(
                app::App::new()
                    .await
                    .map_err(|err| std::io::Error::other(main::Error::AppInit(err)))?,
            );

            eprintln!("Serving at http://{host}:{port}");

            HttpServer::new(move || {
                App::new()
                    .wrap(Logger::default())
                    .app_data(Data::clone(&data))
                    .configure(api::get::register_paths)
                    .configure(api::set::register_paths)
            })
            .bind((host, port))?
            .run()
            .await
        }
        Command::OpenApi => {
            let openapi = ApiDoc::openapi();
            println!("{}", openapi.to_pretty_json().expect("Comp-time constant"));
            Ok(())
        }
    }
}

pub(crate) mod main {
    use crate::app::app_create;

    #[derive(thiserror::Error, Debug)]
    pub(crate) enum Error {
        #[error("Failed to initialize shared application state: {0}")]
        AppInit(#[from] app_create::Error),
    }
}