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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]

use anyhow::Result;
use clap::Parser;
use tracing_subscriber::{
	prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer,
};

pub use cog_core::{Cog, CogResponse};
pub use spec::Path;

mod errors;
mod helpers;
mod prediction;
mod routes;
mod runner;
mod server;
mod shutdown;
mod spec;
mod webhooks;

#[derive(Debug, clap::Parser)]
pub(crate) struct Cli {
	/// Dump the schema and exit
	#[clap(long)]
	dump_schema_and_exit: bool,

	/// Ignore SIGTERM and wait for a request to /shutdown (or a SIGINT) before exiting
	#[clap(long)]
	await_explicit_shutdown: bool,

	/// An endpoint for Cog to PUT output files to
	#[clap(long)]
	upload_url: Option<url::Url>,
}

/// Start the server with the given cog.
///
/// # Errors
///
/// This function will return an error if the PORT environment variable is set but cannot be parsed, or if the server fails to start.
pub async fn start<T: Cog + 'static>() -> Result<()> {
	let args = Cli::parse();

	if !args.dump_schema_and_exit {
		tracing_subscriber::registry()
			.with(tracing_subscriber::fmt::layer().with_filter(
				EnvFilter::try_from_default_env().unwrap_or_else(|_| "cog_rust=info".into()),
			))
			.init();
	}

	server::start::<T>(args).await
}

#[macro_export]
macro_rules! start {
	($struct_name:ident) => {
		#[tokio::main]
		async fn main() {
			cog_rust::start::<$struct_name>().await.unwrap();
		}
	};
}