Skip to main content

DevServer

Struct DevServer 

Source
#[non_exhaustive]
pub struct DevServer { pub ip: IpAddr, pub port: u16, pub watch: Watch, pub dist_dir: Option<PathBuf>, pub pre_hooks: Vec<Box<dyn Hook>>, pub command: Option<Command>, pub post_hooks: Vec<Box<dyn Hook>>, pub not_found_path: Option<PathBuf>, /* private fields */ }
Available on non-WebAssembly only.
Expand description

A simple HTTP server useful during development.

It can watch the source code for changes and restart a provided command.

Serve the file from the provided dist_dir at a given IP address (127.0.0.1:8000 by default). An optional command can be provided to restart the build when changes are detected using command, xtask or cargo.

§Usage

use std::process;
use xtask_wasm::{
    anyhow::Result,
    clap,
};

#[derive(clap::Parser)]
enum Opt {
    Start(xtask_wasm::DevServer),
    Dist,
}

fn main() -> Result<()> {
    let opt: Opt = clap::Parser::parse();

    match opt {
        Opt::Dist => todo!("build project"),
        Opt::Start(dev_server) => {
            log::info!("Starting the development server...");
            dev_server
                .xtask("dist")
                .start()?;
        }
    }

    Ok(())
}

This adds a start subcommand that will run cargo xtask dist, watching for changes in the workspace and serve the files in the default dist directory (target/debug/dist) at the default IP address.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§ip: IpAddr

IP address to bind. Default to 127.0.0.1.

§port: u16

Port number. Default to 8000.

§watch: Watch

Watch configuration for detecting file-system changes.

Controls which paths are watched, debounce timing, and other watch behaviour. Watching is only active when at least one of pre_hooks, command, or post_hooks is set; if none are provided the watch thread is not started.

§dist_dir: Option<PathBuf>

Directory of all generated artifacts.

§pre_hooks: Vec<Box<dyn Hook>>

Commands executed before the main command when a change is detected.

§command: Option<Command>

Main command executed when a change is detected.

§post_hooks: Vec<Box<dyn Hook>>

Commands executed after the main command when a change is detected.

§not_found_path: Option<PathBuf>

Use another file path when the URL is not found.

Implementations§

Source§

impl DevServer

Source

pub fn address(self, ip: IpAddr, port: u16) -> Self

Set the dev-server binding address.

Source

pub fn dist_dir(self, path: impl Into<PathBuf>) -> Self

Set the directory for the generated artifacts.

The default is target/debug/dist.

Source

pub fn pre(self, command: impl Hook + 'static) -> Self

Add a command to execute before the main command when a change is detected.

Source

pub fn pres( self, commands: impl IntoIterator<Item = impl Hook + 'static>, ) -> Self

Add multiple commands to execute before the main command when a change is detected.

Source

pub fn post(self, command: impl Hook + 'static) -> Self

Add a command to execute after the main command when a change is detected.

Source

pub fn posts( self, commands: impl IntoIterator<Item = impl Hook + 'static>, ) -> Self

Add multiple commands to execute after the main command when a change is detected.

Source

pub fn command(self, command: Command) -> Self

Main command executed when a change is detected.

See xtask if you want to use an xtask command.

Source

pub fn xtask(self, name: impl AsRef<str>) -> Self

Name of the main xtask command that is executed when a change is detected.

See command to use an arbitrary command.

Source

pub fn cargo(self, subcommand: impl AsRef<str>) -> Self

Cargo subcommand executed as the main command when a change is detected.

See xtask for xtask commands or command for arbitrary commands.

Source

pub fn arg<S: AsRef<OsStr>>(self, arg: S) -> Self

Adds an argument to the main command executed when changes are detected.

§Panics

Panics if called before command, xtask or cargo.

Source

pub fn args<I, S>(self, args: I) -> Self
where I: IntoIterator<Item = S>, S: AsRef<OsStr>,

Adds multiple arguments to the main command executed when changes are detected.

§Panics

Panics if called before command, xtask or cargo.

Source

pub fn env<K, V>(self, key: K, val: V) -> Self
where K: AsRef<OsStr>, V: AsRef<OsStr>,

Inserts or updates an environment variable for the main command executed when changes are detected.

§Panics

Panics if called before command, xtask or cargo.

Source

pub fn envs<I, K, V>(self, vars: I) -> Self
where I: IntoIterator<Item = (K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>,

Inserts or updates multiple environment variables for the main command executed when changes are detected.

§Panics

Panics if called before command, xtask or cargo.

Source

pub fn not_found_path(self, path: impl Into<PathBuf>) -> Self

Use another file path when the URL is not found.

Source

pub fn request_handler<F>(self, handler: F) -> Self
where F: Fn(Request<'_>) -> Result<()> + Send + Sync + 'static,

Pass a custom request handler to the dev server.

Source

pub fn start(self) -> Result<()>

Start the server, serving the files at dist_dir.

If dist_dir has not been provided, Dist::default_debug_dir will be used.

Trait Implementations§

Source§

impl Args for DevServer

Source§

fn group_id() -> Option<Id>

Report the ArgGroup::id for this set of arguments
Source§

fn augment_args<'b>(__clap_app: Command) -> Command

Append to Command so it can instantiate Self via FromArgMatches::from_arg_matches_mut Read more
Source§

fn augment_args_for_update<'b>(__clap_app: Command) -> Command

Append to Command so it can instantiate self via FromArgMatches::update_from_arg_matches_mut Read more
Source§

impl CommandFactory for DevServer

Source§

fn command<'b>() -> Command

Build a Command that can instantiate Self. Read more
Source§

fn command_for_update<'b>() -> Command

Build a Command that can update self. Read more
Source§

impl Debug for DevServer

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DevServer

Source§

fn default() -> DevServer

Returns the “default value” for a type. Read more
Source§

impl FromArgMatches for DevServer

Source§

fn from_arg_matches(__clap_arg_matches: &ArgMatches) -> Result<Self, Error>

Instantiate Self from ArgMatches, parsing the arguments as needed. Read more
Source§

fn from_arg_matches_mut( __clap_arg_matches: &mut ArgMatches, ) -> Result<Self, Error>

Instantiate Self from ArgMatches, parsing the arguments as needed. Read more
Source§

fn update_from_arg_matches( &mut self, __clap_arg_matches: &ArgMatches, ) -> Result<(), Error>

Assign values from ArgMatches to self.
Source§

fn update_from_arg_matches_mut( &mut self, __clap_arg_matches: &mut ArgMatches, ) -> Result<(), Error>

Assign values from ArgMatches to self.
Source§

impl Parser for DevServer

Source§

fn parse() -> Self

Parse from std::env::args_os(), exit on error.
Source§

fn try_parse() -> Result<Self, Error>

Parse from std::env::args_os(), return Err on error.
Source§

fn parse_from<I, T>(itr: I) -> Self
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Parse from iterator, exit on error.
Source§

fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Parse from iterator, return Err on error.
Source§

fn update_from<I, T>(&mut self, itr: I)
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Update from iterator, exit on error. Read more
Source§

fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error>
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Update from iterator, return Err on error.

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,