Struct afire::extension::serve_static::ServeStatic
source ยท pub struct ServeStatic {
pub serve_path: String,
pub data_dir: String,
pub disabled_files: Vec<String>,
pub not_found: fn(_: Rc<Request>, _: bool) -> Response,
pub middleware: Vec<Box<dyn Fn(Rc<Request>, &mut Response, &mut bool) + Send + Sync>>,
pub types: Vec<(String, String)>,
}
Expand description
Serve Static Content
Fieldsยง
ยงserve_path: String
Path to serve static content on
Defaults to โ/โ (root)
data_dir: String
Content Path
disabled_files: Vec<String>
Disabled file paths (relative from data dir)
not_found: fn(_: Rc<Request>, _: bool) -> Response
Page not found route
middleware: Vec<Box<dyn Fn(Rc<Request>, &mut Response, &mut bool) + Send + Sync>>
Middleware
(Request, Static Response, success [eg If file found])
types: Vec<(String, String)>
MIME Types
Implementationsยง
sourceยงimpl ServeStatic
impl ServeStatic
sourcepub fn new(data_path: impl AsRef<str>) -> Self
pub fn new(data_path: impl AsRef<str>) -> Self
Make a new Static File Server
Example
// Import Library
use afire::{Server, extension::ServeStatic, Middleware};
// Create a server for localhost on port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static file server and attach it to the afire server
ServeStatic::new("data/static").attach(&mut server);
server.start().unwrap();
Examples found in repository?
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
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static file server with a path
extension::ServeStatic::new(STATIC_DIR)
// The middleware priority is by most recently defined.
// The middleware function takes 3 parameters: the request, the response, and weather the file was loaded successfully.
// In your middleware you can modify the response and the bool.
.middleware(|req, res, _suc| {
// Print path served
println!("Served: {}", req.path);
// Return none to not mess with response
// Or in this case add a header and pass through the success value
res.headers.push(Header::new("X-Static", "true"));
})
// Function that runs when no file is found to serve
// This will run before middleware
.not_found(|_req, _dis| {
Response::new()
.status(Status::NotFound)
.text("Page Not Found!")
})
// Add an extra mime type to the server
// It has a lot already
.mime_type("key", "value")
// Set serve path
.path(STATIC_PATH)
// Attach the middleware to the server
.attach(&mut server);
// View the file at http://localhost:8080
// You should also see a favicon in the browser tab
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn disable(self, file_path: impl AsRef<str>) -> Self
pub fn disable(self, file_path: impl AsRef<str>) -> Self
Disable serving a file Path is relative to the dir being served
Example
// Import Library
use afire::{Server, extension::ServeStatic, Middleware};
// Create a server for localhost on port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static server
ServeStatic::new("data/static")
// Disable a file from being served
.disable("index.scss")
// Attach it to the afire server
.attach(&mut server);
server.start().unwrap();
sourcepub fn disable_vec(self, file_paths: &[impl AsRef<str>]) -> Self
pub fn disable_vec(self, file_paths: &[impl AsRef<str>]) -> Self
Disable serving many files at once Path is relative to the dir being served
Example
// Import Library
use afire::{Server, extension::ServeStatic, Middleware};
// Create a server for localhost on port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static server
ServeStatic::new("data/static")
// Disable a vec of files from being served
.disable_vec(&["index.scss", "index.css.map"])
// Attach it to the afire server
.attach(&mut server);
server.start().unwrap();
sourcepub fn not_found(self, f: fn(_: Rc<Request>, _: bool) -> Response) -> Self
pub fn not_found(self, f: fn(_: Rc<Request>, _: bool) -> Response) -> Self
Set the not found page.
This will run if no file is found to serve or the file is disabled. The bool in the fn parameters is if the file is blocked.
Example
// Import Library
use afire::{Response, Server, extension::ServeStatic, Middleware, Status};
// Create a server for localhost on port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static server
ServeStatic::new("data/static")
// Set a new file not found page
.not_found(|_req, _dis| Response::new().status(Status::NotFound).text("Page Not Found!"))
// Attach it to the afire server
.attach(&mut server);
server.start().unwrap();
Examples found in repository?
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
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static file server with a path
extension::ServeStatic::new(STATIC_DIR)
// The middleware priority is by most recently defined.
// The middleware function takes 3 parameters: the request, the response, and weather the file was loaded successfully.
// In your middleware you can modify the response and the bool.
.middleware(|req, res, _suc| {
// Print path served
println!("Served: {}", req.path);
// Return none to not mess with response
// Or in this case add a header and pass through the success value
res.headers.push(Header::new("X-Static", "true"));
})
// Function that runs when no file is found to serve
// This will run before middleware
.not_found(|_req, _dis| {
Response::new()
.status(Status::NotFound)
.text("Page Not Found!")
})
// Add an extra mime type to the server
// It has a lot already
.mime_type("key", "value")
// Set serve path
.path(STATIC_PATH)
// Attach the middleware to the server
.attach(&mut server);
// View the file at http://localhost:8080
// You should also see a favicon in the browser tab
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn mime_type(self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self
pub fn mime_type(self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self
Add a MIME type to the Static file Server
This extension comes with a lot of builtin MIME types but if you need to add more thats what this is for
The key is the file extension
The value is the MIME type
Example
// Import Library
use afire::{Server, extension::ServeStatic, Middleware};
// Create a server for localhost on port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static server
ServeStatic::new("data/static")
// Add a new MIME type
.mime_type(".3gp", "video/3gpp")
// Attach it to the afire server
.attach(&mut server);
server.start().unwrap();
Examples found in repository?
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
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static file server with a path
extension::ServeStatic::new(STATIC_DIR)
// The middleware priority is by most recently defined.
// The middleware function takes 3 parameters: the request, the response, and weather the file was loaded successfully.
// In your middleware you can modify the response and the bool.
.middleware(|req, res, _suc| {
// Print path served
println!("Served: {}", req.path);
// Return none to not mess with response
// Or in this case add a header and pass through the success value
res.headers.push(Header::new("X-Static", "true"));
})
// Function that runs when no file is found to serve
// This will run before middleware
.not_found(|_req, _dis| {
Response::new()
.status(Status::NotFound)
.text("Page Not Found!")
})
// Add an extra mime type to the server
// It has a lot already
.mime_type("key", "value")
// Set serve path
.path(STATIC_PATH)
// Attach the middleware to the server
.attach(&mut server);
// View the file at http://localhost:8080
// You should also see a favicon in the browser tab
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn mime_types(
self,
new_types: &[(impl AsRef<str>, impl AsRef<str>)]
) -> Self
pub fn mime_types( self, new_types: &[(impl AsRef<str>, impl AsRef<str>)] ) -> Self
Add a vector of MIME type to the Static file Server
The key is the file extension
The value is the MIME type
Ex: (โhtmlโ, โtext/htmlโ)
Example
// Import Library
use afire::{Server, extension::ServeStatic, Middleware};
// Create a server for localhost on port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static server
ServeStatic::new("data/static")
// Add a new MIME type
.mime_types(&[("3gp", "video/3gpp")])
// Attach it to the afire server
.attach(&mut server);
server.start().unwrap();
sourcepub fn middleware(
self,
f: impl Fn(Rc<Request>, &mut Response, &mut bool) + Send + Sync + 'static
) -> Self
pub fn middleware( self, f: impl Fn(Rc<Request>, &mut Response, &mut bool) + Send + Sync + 'static ) -> Self
Add a middleware to the serve static extension. Middleware here works much differently to the normal afire middleware. The middleware priority is still by most recently defined.
The middleware function takes 3 parameters: the request, the response, and weather the file was loaded successfully. In your middleware you can modify the response and the bool.
Examples found in repository?
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
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static file server with a path
extension::ServeStatic::new(STATIC_DIR)
// The middleware priority is by most recently defined.
// The middleware function takes 3 parameters: the request, the response, and weather the file was loaded successfully.
// In your middleware you can modify the response and the bool.
.middleware(|req, res, _suc| {
// Print path served
println!("Served: {}", req.path);
// Return none to not mess with response
// Or in this case add a header and pass through the success value
res.headers.push(Header::new("X-Static", "true"));
})
// Function that runs when no file is found to serve
// This will run before middleware
.not_found(|_req, _dis| {
Response::new()
.status(Status::NotFound)
.text("Page Not Found!")
})
// Add an extra mime type to the server
// It has a lot already
.mime_type("key", "value")
// Set serve path
.path(STATIC_PATH)
// Attach the middleware to the server
.attach(&mut server);
// View the file at http://localhost:8080
// You should also see a favicon in the browser tab
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn path(self, path: impl AsRef<str>) -> Self
pub fn path(self, path: impl AsRef<str>) -> Self
Set path to serve static files on
Default is โ/โ (root)
Example
// Import Library
use afire::{Server, extension::ServeStatic, Middleware};
// Create a server for localhost on port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static server
ServeStatic::new("data/static")
// Set serve path
.path("/static")
// Attach it to the afire server
.attach(&mut server);
server.start().unwrap();
Examples found in repository?
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
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Make a new static file server with a path
extension::ServeStatic::new(STATIC_DIR)
// The middleware priority is by most recently defined.
// The middleware function takes 3 parameters: the request, the response, and weather the file was loaded successfully.
// In your middleware you can modify the response and the bool.
.middleware(|req, res, _suc| {
// Print path served
println!("Served: {}", req.path);
// Return none to not mess with response
// Or in this case add a header and pass through the success value
res.headers.push(Header::new("X-Static", "true"));
})
// Function that runs when no file is found to serve
// This will run before middleware
.not_found(|_req, _dis| {
Response::new()
.status(Status::NotFound)
.text("Page Not Found!")
})
// Add an extra mime type to the server
// It has a lot already
.mime_type("key", "value")
// Set serve path
.path(STATIC_PATH)
// Attach the middleware to the server
.attach(&mut server);
// View the file at http://localhost:8080
// You should also see a favicon in the browser tab
// Start the server
// This will block the current thread
server.start().unwrap();
}
Trait Implementationsยง
sourceยงimpl Middleware for ServeStatic
impl Middleware for ServeStatic
sourceยงfn post_raw(
&self,
req: Result<Rc<Request>>,
res: &mut Result<Response>
) -> MiddleResult
fn post_raw( &self, req: Result<Rc<Request>>, res: &mut Result<Response> ) -> MiddleResult
raw
version of Middleware::post
, it is passed a Result
.
The default implementation calls Middleware::post
if the Result
is Ok
.sourceยงfn pre_raw(&self, req: &mut Result<Request>) -> MiddleResult
fn pre_raw(&self, req: &mut Result<Request>) -> MiddleResult
raw
version of Middleware::pre
, it is passed a Result
.
The default implementation calls Middleware::pre
if the Result
is Ok
.sourceยงfn pre(&self, _req: &mut Request) -> MiddleResult
fn pre(&self, _req: &mut Request) -> MiddleResult
sourceยงfn post(&self, _req: &Request, _res: &mut Response) -> MiddleResult
fn post(&self, _req: &Request, _res: &mut Response) -> MiddleResult
sourceยงfn end_raw(&self, req: &Result<Request>, res: &Result<Response>)
fn end_raw(&self, req: &Result<Request>, res: &Result<Response>)
raw
version of Middleware::end
, it is passed a Result
.
The default implementation calls Middleware::end
if the Result
is Ok
.