-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to reuse a db client and modularize handlers? #22
Comments
After applying #14 – actually rebasing on top of master and solving a conflict – I managed to do this: extern crate chill;
extern crate pencil;
use pencil::Pencil;
use pencil::{Request, PencilResult, Response};
use pencil::method::Get;
use pencil::helpers;
use chill::Client;
struct Handlers {
db: Client,
}
impl Handlers {
fn hello(&self, _: &mut Request) -> PencilResult {
Ok(Response::from("Hello World!"))
}
}
fn main() {
let mut app = Pencil::new("/web/hello");
let handlers = Handlers{db: Client::new("http://0.0.0.0:5984/").unwrap()};
app.route("/", &[Get], "hello", move |resp| handlers.hello(resp));
app.run("0.0.0.0:5000");
} So far so good. But no DB connections yet. So I tried: extern crate chill;
extern crate pencil;
use pencil::Pencil;
use pencil::{Request, PencilResult, Response};
use pencil::method::Get;
use pencil::helpers;
use chill::Client;
struct Handlers {
db: Client,
}
impl Handlers {
fn hello(&self, _: &mut Request) -> PencilResult {
Ok(Response::from("Hello World!"))
}
fn logo(&self, req: &mut Request) -> PencilResult {
println!("From {}", req.remote_addr());
println!("Headers {}", req.headers());
Ok(helpers::send_file("./static/logo.png", "image/png".parse().unwrap(), false).unwrap())
}
}
fn main() {
let mut app = Pencil::new("/web/hello");
let handlers = Handlers{db: Client::new("http://0.0.0.0:5984/").unwrap()};
app.route("/", &[Get], "hello", move |resp| handlers.hello(resp));
app.route("/logo.png", &[Get], "logo", move |resp| handlers.logo(resp));
app.run("0.0.0.0:5000");
} Which obviously failed:
And, finally, I gave up: extern crate chill;
extern crate pencil;
use pencil::Pencil;
use pencil::{Request, PencilResult, Response};
use pencil::method::Get;
use pencil::helpers;
use chill::Client;
#[derive(Copy, Clone)]
struct Handlers {
db: Client,
}
impl Handlers {
fn hello(&self, _: &mut Request) -> PencilResult {
Ok(Response::from("Hello World!"))
}
fn logo(&self, req: &mut Request) -> PencilResult {
println!("From {}", req.remote_addr());
println!("Headers {}", req.headers());
Ok(helpers::send_file("./static/logo.png", "image/png".parse().unwrap(), false).unwrap())
}
}
fn main() {
let mut app = Pencil::new("/web/hello");
let handlers = Handlers{db: Client::new("http://0.0.0.0:5984/").unwrap()};
app.route("/", &[Get], "hello", move |resp| handlers.hello(resp));
app.route("/logo.png", &[Get], "logo", move |resp| handlers.logo(resp));
app.run("0.0.0.0:5000");
} Because of:
@fengsp: Can you please help me achieve what I meant by this code? Thank you! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! This is what I tried to do:
I can't pass
handlers.hello
as aViewFunc
toapp.route()
. How could I do what I just meant by this code? As you can see, I'm pretty new to Rust.Thanks.
The text was updated successfully, but these errors were encountered: