Skip to content

Instantly share code, notes, and snippets.

@dweidenfeld
Created October 9, 2015 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dweidenfeld/20662812b55c79ae4d50 to your computer and use it in GitHub Desktop.
Save dweidenfeld/20662812b55c79ae4d50 to your computer and use it in GitHub Desktop.
CrawlStore - Trait does not live long enough
use nickel::router::Router;
use nickel::HttpRouter;
use controller::Controller;
use db::Database;
#[allow(dead_code)]
pub struct JobController<'c, D> where D: Database { // Error: The parameter type D may not live long enough
db: &'c D
}
impl<'c, D> JobController<'c, D> where D: Database {
pub fn new(db: &'c D) -> Self {
JobController {
db: db
}
}
}
impl<'c, D> Controller for JobController<'c, D> where D: Database {
fn register(&self, router: &mut Router) {
router.get("/job/:id", middleware! {|request|
format!("requesting {:?}", request.param("id"));
});
}
}
mod job;
mod queue;
use nickel::router::Router;
use db::Database;
pub trait Controller {
fn register(&self, router: &mut Router);
}
pub struct ControllerFactory<'c> {
controller: Vec<&'c Controller>
}
impl<'c> ControllerFactory<'c> {
pub fn with_db<D>(db: &'c D) -> Self where D: Database {
let jc = job::JobController::new(db);
//let qc = queue::QueueController::new(db.clone());
ControllerFactory {
controller: vec![jc]
}
}
pub fn register(&self, router: &mut Router) {
for controller in &self.controller {
controller.register(router);
}
}
}
use time::Tm;
#[allow(dead_code)]
pub struct Job {
id: Option<&'static str>,
name: &'static str,
collections: Vec<&'static str>,
start_urls: Vec<&'static str>,
whitelist: Option<Vec<&'static str>>,
blacklist: Option<Vec<&'static str>>,
load: u8
}
#[allow(dead_code)]
pub struct QueueItem {
url: &'static str,
last_index_date: Option<Tm>,
next_index_date: Option<Tm>,
last_modified: Option<Tm>,
job: Job
}
pub trait Database {
fn save_job(&self, job: Job) -> Job;
fn delete_job(&self, id: &'static str);
fn get_job(&self, id: &'static str) -> Job;
fn get_jobs(&self) -> Vec<Job>;
fn add_queue_item(&self, queue_item: QueueItem) -> QueueItem;
fn get_next_queue_item(&self) -> QueueItem;
fn get_queue_size(&self) -> usize;
}
pub struct MockDatabase {
job: Vec<Job>,
queue: Vec<QueueItem>
}
impl MockDatabase {
pub fn new() -> Self {
MockDatabase {
job: vec![],
queue: vec![]
}
}
}
impl Database for MockDatabase {
fn save_job(&self, job: Job) -> Job {
unimplemented!()
}
fn delete_job(&self, id: &'static str) {
unimplemented!()
}
fn get_job(&self, id: &'static str) -> Job {
unimplemented!()
}
fn get_jobs(&self) -> Vec<Job> {
unimplemented!()
}
fn add_queue_item(&self, queue_item: QueueItem) -> QueueItem {
unimplemented!()
}
fn get_next_queue_item(&self) -> QueueItem {
unimplemented!()
}
fn get_queue_size(&self) -> usize {
unimplemented!()
}
}
#[macro_use]
extern crate nickel;
extern crate getopts;
extern crate time;
mod controller;
mod db;
use std::env;
use std::process;
use std::io::Write;
use nickel::Nickel;
use getopts::Options;
use controller::{ControllerFactory, Controller};
use db::{Database, MockDatabase};
fn main() {
let args: Vec<String> = env::args().collect();
let program = args[0].clone();
let mut opts = Options::new();
opts.reqopt("m", "mongourl", "set mongodb connection url in form of mongo://...", "mongourl");
opts.optflag("h", "help", "print this help menu");
let matches = match opts.parse(&args[1..]) {
Ok(m) => { m }
Err(f) => {
writeln!(std::io::stderr(), "{}\n", f.to_string()).unwrap();
print_usage(&program, opts);
process::exit(1);
}
};
if matches.opt_present("h") {
print_usage(&program, opts);
return;
}
let db = MockDatabase::new();
let mut server = Nickel::new();
let mut router = Nickel::router();
let cf = ControllerFactory::with_db(db);
cf.register(&mut router);
server.utilize(router);
server.listen("0.0.0.0:3000");
}
fn print_usage(program: &str, opts: Options) {
let brief = format!("Usage: {} [options]", program);
print!("{}", opts.usage(&brief));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment