snarkify-sdk 0.1.0-alpha.11

Snarkify Rust SDK for Streamlined Serverless Prover Development and Deployment
Documentation
#![doc = include_str!("../README.md")]
#![allow(unstable_name_collisions)]

use std::{env, io};

use crate::prover::ProofHandler;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
use cloudevents::Event;
use std::time::Duration;
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_BYTES: usize = 10 * 1024 * 1024;
const DEFAULT_PORT: u16 = 8080;
const DEFAULT_WORKER_NUM: usize = 1;
/// Default duration a server keeps a connection open waiting for additional requests.
const DEFAULT_KEEP_ALIVE_TIMEOUT_SECONDS: u64 = 3600;

#[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")
        .ok()
        .and_then(|port| port.parse().ok())
        .unwrap_or(DEFAULT_PORT);
    let keep_alive_timeout_seconds: u64 = env::var("KEEP_ALIVE_TIMEOUT_SECONDS")
        .ok()
        .and_then(|timeout| timeout.parse().ok())
        .unwrap_or(DEFAULT_KEEP_ALIVE_TIMEOUT_SECONDS);

    // Create the HTTP server
    HttpServer::new(|| {
        App::new()
            .app_data(web::PayloadConfig::new(DEFAULT_PAYLOAD_LIMIT_BYTES))
            .app_data(web::JsonConfig::default().limit(DEFAULT_PAYLOAD_LIMIT_BYTES))
            .wrap(actix_web::middleware::Logger::default())
            .route(
                "/",
                web::post().to(|req: HttpRequest, mut event: Event| async move {
                    // Adding custom headers to the event
                    for (header_name, header_value) in req.headers() {
                        let header_name_lower = header_name.as_str().to_lowercase();
                        if header_name_lower.eq(prover::READ_INPUT_FROM_URL_HEADER) {
                            if let Ok(value) = header_value.to_str() {
                                event.set_extension(header_name_lower.as_str(), value.to_string());
                            }
                        }
                    }
                    T::handle(event).await
                }),
            )
            .route("/", web::get().to(HttpResponse::MethodNotAllowed))
            .route("/health/{_:(readiness|liveness)}", web::get().to(HttpResponse::Ok))
    })
    .keep_alive(Duration::from_secs(keep_alive_timeout_seconds))
    .bind(("0.0.0.0", port))?
    .workers(
        env::var("WORKER_NUM")
            .ok()
            .and_then(|worker_num| worker_num.parse().ok())
            .unwrap_or(DEFAULT_WORKER_NUM),
    )
    .run()
    .await
}