#![allow(unused_variables)]
#![allow(dead_code)]
extern crate rusty_express;
use rusty_express::prelude::*;
use std::thread;
use std::time::Duration;
fn main() {
let mut server = HttpServer::new();
let model = Model::new();
ServerContext::set_context(Box::new(model));
let mut router = Route::new();
router
.get(RequestPath::Explicit("/"), simple_response)
.get(RequestPath::Explicit("/index"), simple_index);
server.def_router(router);
server.listen_and_serve(
8080,
Some(|sender| {
thread::sleep(Duration::from_secs(30));
println!("Get ready to shutdown the server...");
if let Err(_) = sender.send(ControlMessage::Terminate) {
eprintln!("Failed to send the server shutdown message...");
}
}),
);
}
pub fn simple_response(req: &Box<Request>, resp: &mut Box<Response>) {
work_with_context(req, resp);
resp.send("Hello world from rusty server!\n");
resp.status(200);
}
pub fn simple_index(req: &Box<Request>, resp: &mut Box<Response>) {
work_with_context(req, resp);
resp.send("Hello world from the index page!\n");
}
fn work_with_context(req: &Box<Request>, resp: &mut Box<Response>) {
if let Err(e) = ServerContext::update_context(req, resp) {
eprintln!("Error on updating the server context: {}", e);
}
if let Err(e) = ServerContext::process_with_context(req, resp) {
eprintln!("Error on updating the server context: {}", e);
}
}
struct Model {
count: u32,
}
impl Model {
#[inline]
pub fn new() -> Self {
Model { count: 0 }
}
#[inline]
pub fn new_with(d: u32) -> Self {
Model { count: d }
}
#[inline]
fn add_one(&mut self) {
self.count += 1;
}
#[inline]
fn get_count(&self) -> u32 {
self.count
}
}
impl Clone for Model {
fn clone(&self) -> Self {
Model { count: self.count }
}
}
impl ContextProvider for Model {
fn update(&mut self, req: &Box<Request>, resp: &mut Box<Response>) -> Result<(), &'static str> {
self.add_one();
Ok(())
}
fn process(&self, _req: &Box<Request>, _resp: &mut Box<Response>) -> Result<(), &'static str> {
println!("Visit count: {}", self.count);
Ok(())
}
}