Skip to content
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

Open
scorphus opened this issue Jun 16, 2016 · 1 comment
Open

How to reuse a db client and modularize handlers? #22

scorphus opened this issue Jun 16, 2016 · 1 comment

Comments

@scorphus
Copy link
Contributor

Hi! This is what I tried to do:

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 mut handlers = Handlers{db: Client::new("http://0.0.0.0:5984/").unwrap()};
    app.route("/", &[Get], "hello", handlers.hello);
    app.run("0.0.0.0:5000");
}

I can't pass handlers.hello as a ViewFunc to app.route(). How could I do what I just meant by this code? As you can see, I'm pretty new to Rust.

Thanks.

@scorphus
Copy link
Contributor Author

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:

   Compiling handers v0.1.0 (file:///Users/pablo/Workspace/handers)
src/main.rs:34:56: 34:64 error: capture of moved value: `handlers` [E0382]
src/main.rs:34     app.route("/logo.png", &[Get], "logo", move |resp| handlers.logo(resp));
                                                                      ^~~~~~~~
src/main.rs:33:37: 33:48 note: value moved (into closure) here
src/main.rs:33     app.route("/", &[Get], "hello", move |resp| handlers.hello(resp));
                                                   ^~~~~~~~~~~
src/main.rs:34:56: 34:64 help: run `rustc --explain E0382` to see a detailed explanation
src/main.rs:34:56: 34:64 note: move occurs because `handlers` has type `Handlers`, which does not implement the `Copy` trait
error: aborting due to previous error
error: Could not compile `handers`.

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:

   Compiling handlers v0.1.0 (file:///.../handlers)
src/main.rs:12:10: 12:14 error: the trait `Copy` may not be implemented for this type; field `db` does not implement `Copy` [E0204]
src/main.rs:12 #[derive(Copy, Clone)]
                        ^~~~
src/main.rs:12:10: 12:14 note: in this expansion of #[derive(Copy)] (defined in src/main.rs)
src/main.rs:12:10: 12:14 help: run `rustc --explain E0204` to see a detailed explanation
error: aborting due to previous error
error: Could not compile `handlers`.

@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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant