Skip to main content

Server

Struct Server 

Source
pub struct Server { /* private fields */ }
Expand description

The editor’s HTTP server, configured for one repository.

Implementations§

Source§

impl Server

Source

pub fn new(app: impl App) -> Self

Build a server for an app.

Panics when a table takes one of the reserved names, because such a table is unreachable: the control endpoints and the stop subcommand are matched first. Panics, too, when a table’s file is not a bare name, since every file is resolved against the one Data/ directory.

Examples found in repository?
examples/library/main.rs (lines 1046-1053)
1038fn main() -> anyhow::Result<()> {
1039    // The tables belong to the example, not to whoever ran it.
1040    std::env::set_current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/library"))?;
1041
1042    let command = ServerArgs::augment_help(Cli::command(), "the front page", DEFAULT_PORT);
1043    let cli = Cli::from_arg_matches(&command.get_matches())?;
1044
1045    match cli.command {
1046        Command::Web(args) => Server::new(Library {
1047            books: Books,
1048            genres: Genres,
1049            branches: Branches,
1050            on_loan: OnLoan,
1051            branch_cards: BranchCards,
1052            branch_detail: BranchDetail,
1053        })
1054        .child_env("LIBRARY_EXAMPLE_CHILD")
1055        .default_port(DEFAULT_PORT)
1056        .run(args),
1057    }
1058}
Source

pub fn index_html(self, html: &'static str) -> Self

Serve a bundle of the repository’s own in place of the embedded one.

Source

pub fn child_env(self, var: &'static str) -> Self

The environment variable marking the detached worker process. A repository whose server is registered as a system service keeps its own name here, so the service entry does not have to change.

Examples found in repository?
examples/library/main.rs (line 1054)
1038fn main() -> anyhow::Result<()> {
1039    // The tables belong to the example, not to whoever ran it.
1040    std::env::set_current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/library"))?;
1041
1042    let command = ServerArgs::augment_help(Cli::command(), "the front page", DEFAULT_PORT);
1043    let cli = Cli::from_arg_matches(&command.get_matches())?;
1044
1045    match cli.command {
1046        Command::Web(args) => Server::new(Library {
1047            books: Books,
1048            genres: Genres,
1049            branches: Branches,
1050            on_loan: OnLoan,
1051            branch_cards: BranchCards,
1052            branch_detail: BranchDetail,
1053        })
1054        .child_env("LIBRARY_EXAMPLE_CHILD")
1055        .default_port(DEFAULT_PORT)
1056        .run(args),
1057    }
1058}
Source

pub fn command(self, command: &'static str) -> Self

The subcommand that reaches Server::run, used when re-invoking the binary as a detached worker.

Source

pub fn default_port(self, port: u16) -> Self

The port to bind when the command line names none. Each app on a machine takes its own, so one editor never lands on another’s port.

Examples found in repository?
examples/library/main.rs (line 1055)
1038fn main() -> anyhow::Result<()> {
1039    // The tables belong to the example, not to whoever ran it.
1040    std::env::set_current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/library"))?;
1041
1042    let command = ServerArgs::augment_help(Cli::command(), "the front page", DEFAULT_PORT);
1043    let cli = Cli::from_arg_matches(&command.get_matches())?;
1044
1045    match cli.command {
1046        Command::Web(args) => Server::new(Library {
1047            books: Books,
1048            genres: Genres,
1049            branches: Branches,
1050            on_loan: OnLoan,
1051            branch_cards: BranchCards,
1052            branch_detail: BranchDetail,
1053        })
1054        .child_env("LIBRARY_EXAMPLE_CHILD")
1055        .default_port(DEFAULT_PORT)
1056        .run(args),
1057    }
1058}
Source

pub fn worker_args(self, args: impl IntoIterator<Item = String>) -> Self

Arguments to pass on to the detached worker, after the table and the port.

The worker is a fresh invocation of this binary, and it is given only the table and the port, so a flag the user passed the parent does not reach it. A repository whose subcommand takes a flag the serving process needs—one naming a companion service, say—forwards it here. The worker inherits the environment either way, so a setting that already lives in a variable needs no forwarding.

What is forwarded lands on the worker’s command line, so each argument has to be one the editor’s subcommand declares. It must be a flag and not a positional, because the table is the only positional that command line has—a forwarded positional is refused outright—and it must not repeat --port or the table, which are passed already. An argument that breaks these rules leaves the worker unable to parse its own command line, and the launch then fails with what the worker said.

Source

pub fn before_launch(self, f: impl Fn() + Send + 'static) -> Self

Work to do once, in the process the user invoked, before a server is started or reused: bringing up a companion service, say. It does not run in the detached worker.

Source

pub fn run(self, args: ServerArgs) -> Result<()>

Examples found in repository?
examples/library/main.rs (line 1056)
1038fn main() -> anyhow::Result<()> {
1039    // The tables belong to the example, not to whoever ran it.
1040    std::env::set_current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/library"))?;
1041
1042    let command = ServerArgs::augment_help(Cli::command(), "the front page", DEFAULT_PORT);
1043    let cli = Cli::from_arg_matches(&command.get_matches())?;
1044
1045    match cli.command {
1046        Command::Web(args) => Server::new(Library {
1047            books: Books,
1048            genres: Genres,
1049            branches: Branches,
1050            on_loan: OnLoan,
1051            branch_cards: BranchCards,
1052            branch_detail: BranchDetail,
1053        })
1054        .child_env("LIBRARY_EXAMPLE_CHILD")
1055        .default_port(DEFAULT_PORT)
1056        .run(args),
1057    }
1058}

Auto Trait Implementations§

§

impl !RefUnwindSafe for Server

§

impl !Sync for Server

§

impl !UnwindSafe for Server

§

impl Freeze for Server

§

impl Send for Server

§

impl Unpin for Server

§

impl UnsafeUnpin for Server

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.