Last active
May 7, 2019 00:17
-
-
Save blechatellier/a4fabf9251250cfa208b817a2d6b5b73 to your computer and use it in GitHub Desktop.
Trying to get a persistent reference working with Iron, not sure how to set an appropriate lifetime here. I want to be able to re-use the controller on different routes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "iron-lifetime" | |
version = "0.1.0" | |
edition = "2018" | |
[dependencies] | |
iron = "0.6.*" | |
router = "0.6.*" | |
persistent = "0.4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use iron::prelude::*; | |
use iron::typemap::Key; | |
use router::Router; | |
use persistent::Read; | |
pub struct Controller; | |
pub struct Rest { | |
controller: Controller, | |
} | |
impl Key for &Controller { | |
type Value = Self; | |
} | |
impl Rest { | |
pub fn run(&self) { | |
let router = Router::new(); | |
let mut chain = Chain::new(router); | |
chain.link(Read::<&Controller>::both(&self.controller)); | |
Iron::new(chain).http(format!("0.0.0.0:1234")).ok(); | |
} | |
} | |
fn main() { | |
Rest { | |
controller: Controller, | |
}.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment