pub struct Routes { /* private fields */ }
Expand description
§Routes
This struct defines the routes. It uses a hashmap to do this.
HashMap<Route, Content>
where content is the return content (ie, html or json).
Implementations§
Source§impl Routes
impl Routes
Sourcepub async fn add_route(&mut self, route: String, content: Route)
pub async fn add_route(&mut self, route: String, content: Route)
§Add Route
Adds a new route to the routes hashmap. If the route already exists, its value is updated
Sourcepub async fn get_route(
&self,
request: String,
user_addr: SocketAddr,
is_secure: bool,
) -> Result<DataType, &str>
pub async fn get_route( &self, request: String, user_addr: SocketAddr, is_secure: bool, ) -> Result<DataType, &str>
§Get Route
This function takes in the response string from the TcpStream
and searches the hashmap
for the callback function associated with the route. It then checks that the route is valid,
and runs it asynchrynously (using the request so that the callback can make use of the request data)
This function only runs the callback - handling POST and GET requests is up to the callback.
If this function detects a request for static content - which it can only detect if the data is stored in
/static/
, then it will return early with the static content, and not run any functions.
If an error handler is not set, and a route is not found, a panic will occur.