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
use crate::context::PuffContext;
use crate::errors::Result;
use crate::program::{Runnable, RunnableCommand};
use crate::web::server::Router;
use clap::{ArgMatches, Command};
use crate::program::commands::HttpServerConfig;
use std::process::ExitCode;
pub struct ServerCommand(Option<Box<dyn FnOnce() -> Router + 'static>>);
impl ServerCommand {
pub fn new(r: Router) -> Self {
Self(Some(Box::new(|| r)))
}
pub fn new_with_router_init<F: FnOnce() -> Router + 'static>(f: F) -> Self {
Self(Some(Box::new(f)))
}
}
impl RunnableCommand for ServerCommand {
fn cli_parser(&self) -> Command {
HttpServerConfig::add_command_options(Command::new("runserver"))
}
fn make_runnable(&mut self, args: &ArgMatches, context: PuffContext) -> Result<Runnable> {
let config = HttpServerConfig::new_from_args(args);
let router_fn = self.0.take().expect("Already ran.");
let fut = async move {
let app = router_fn().into_axum_router(context);
let server = config.server_builder().serve(app.into_make_service());
server.await?;
Ok(ExitCode::SUCCESS)
};
Ok(Runnable::new(fut))
}
}