pub struct Server { /* private fields */ }Expand description
The editor’s HTTP server, configured for one repository.
Implementations§
Source§impl Server
impl Server
Sourcepub fn new(app: impl App) -> Self
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?
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}Sourcepub fn index_html(self, html: &'static str) -> Self
pub fn index_html(self, html: &'static str) -> Self
Serve a bundle of the repository’s own in place of the embedded one.
Sourcepub fn child_env(self, var: &'static str) -> Self
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?
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}Sourcepub fn command(self, command: &'static str) -> Self
pub fn command(self, command: &'static str) -> Self
The subcommand that reaches Server::run, used when re-invoking the
binary as a detached worker.
Sourcepub fn default_port(self, port: u16) -> Self
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?
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}Sourcepub fn worker_args(self, args: impl IntoIterator<Item = String>) -> Self
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.
Sourcepub fn before_launch(self, f: impl Fn() + Send + 'static) -> Self
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.
Sourcepub fn run(self, args: ServerArgs) -> Result<()>
pub fn run(self, args: ServerArgs) -> Result<()>
Examples found in repository?
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}