Created
June 9, 2018 13:51
-
-
Save gfreezy/0d1ca5fcc6af32325ef69e3023c25c47 to your computer and use it in GitHub Desktop.
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
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