Skip to content

Instantly share code, notes, and snippets.

@gfreezy
Created June 9, 2018 13:51
Show Gist options
  • Save gfreezy/0d1ca5fcc6af32325ef69e3023c25c47 to your computer and use it in GitHub Desktop.
Save gfreezy/0d1ca5fcc6af32325ef69e3023c25c47 to your computer and use it in GitHub Desktop.
extern crate bitcask_rs;
extern crate actix_web;
extern crate actix;
extern crate failure;
extern crate futures;
use failure::{Fail, Error};
use futures::future::{Future, result};
use actix_web::{server, App, HttpRequest, Responder, FutureResponse, HttpResponse, AsyncResponder};
use std::path::PathBuf;
use actix::prelude::{Message, Actor, SyncContext, Handler, SyncArbiter, Addr, Syn};
fn index(_req: HttpRequest<AppState>) -> impl Responder {
"Hello world!"
}
fn get(req: HttpRequest<AppState>) -> FutureResponse<HttpResponse, Error> {
result(Ok(HttpResponse::Ok()
.content_type("text/html")
.body(format!("Hello!"))))
.responder()
}
//
//fn set(req: HttpRequest<AppState>) -> impl Responder {}
//
//fn delete(req: HttpRequest<AppState>) -> impl Responder {}
struct Get(String);
struct Set(String, Vec<u8>);
struct Delete(String);
impl Message for Get {
type Result = Result<Option<Vec<u8>>, Error>;
}
impl Message for Set {
type Result = Result<(), Error>;
}
impl Message for Delete {
type Result = Result<(), Error>;
}
struct BitcaskActor(bitcask_rs::Bitcask);
impl Actor for BitcaskActor {
type Context = SyncContext<Self>;
}
impl Handler<Get> for BitcaskActor {
type Result = Result<Option<Vec<u8>>, Error>;
fn handle(&mut self, msg: Get, _: &mut Self::Context) -> Self::Result {
self.0.get(msg.0)
}
}
impl Handler<Set> for BitcaskActor {
type Result = Result<(), Error>;
fn handle(&mut self, msg: Set, _: &mut Self::Context) -> Self::Result {
self.0.set(msg.0, msg.1)
}
}
impl Handler<Delete> for BitcaskActor {
type Result = Result<(), Error>;
fn handle(&mut self, msg: Delete, _: &mut Self::Context) -> Self::Result {
self.0.delete(msg.0)
}
}
struct AppState {
addr: Addr<Syn, BitcaskActor>
}
fn main() {
let config = bitcask_rs::ConfigBuilder::default().path(PathBuf::from("target/store3")).build().unwrap();
let bitcask = bitcask_rs::Bitcask::new(config);
let addr = SyncArbiter::start(2, move || BitcaskActor(bitcask.clone()));
server::new(move ||
App::with_state(AppState { addr: addr.clone() })
.resource("/", |r| r.f(index))
.resource("/get", |r| r.route().a(get))
// .resource("/set", |r| r.f(set))
// .resource("/delete", |r| r.f(delete))
).bind("127.0.0.1:8088")
.unwrap()
.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment