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
#![doc = include_str!("../README.md")]
#![allow(unstable_name_collisions)]

use std::{env, io};

use crate::prover::ProofHandler;
use actix_web::{web, App, HttpResponse, HttpServer};
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
use tracing_subscriber::fmt::format::FmtSpan;

pub mod datetime_serde;
pub mod prover;

/// Default payload limit for incoming requests, 10MB
const DEFAULT_PAYLOAD_LIMIT: usize = 10 * 1024 * 1024;
const DEFAULT_PORT: u16 = 8080;
const DEFAULT_WORKER_NUM: usize = 1;

#[actix_web::main]
pub async fn run<T: ProofHandler + Send + 'static>() -> io::Result<()> {
    tracing_subscriber::fmt()
        .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
        .with_env_filter(
            EnvFilter::builder()
                .with_default_directive(LevelFilter::INFO.into())
                .from_env_lossy(),
        )
        .with_ansi(false)
        .with_level(true)
        .with_target(true)
        .json()
        .flatten_event(true)
        .try_init()
        .unwrap();

    let port: u16 = env::var("PORT")
        .map(|port| port.parse().expect("Failed to parse port from env"))
        .unwrap_or(DEFAULT_PORT);

    // Create the HTTP server
    HttpServer::new(|| {
        App::new()
            .app_data(web::PayloadConfig::new(DEFAULT_PAYLOAD_LIMIT))
            .app_data(web::JsonConfig::default().limit(DEFAULT_PAYLOAD_LIMIT))
            .wrap(actix_web::middleware::Logger::default())
            .route("/", web::post().to(T::handle))
            .route("/", web::get().to(HttpResponse::MethodNotAllowed))
            .route("/health/{_:(readiness|liveness)}", web::get().to(HttpResponse::Ok))
    })
    .bind(("0.0.0.0", port))?
    .workers(DEFAULT_WORKER_NUM)
    .run()
    .await
}