Skip to content

Instantly share code, notes, and snippets.

@0e4ef622
Last active June 22, 2018 03:53
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 0e4ef622/54ff213a6c7dc8ef2214e17af574170f to your computer and use it in GitHub Desktop.
Save 0e4ef622/54ff213a6c7dc8ef2214e17af574170f to your computer and use it in GitHub Desktop.
very primitive not secure message wall
[package]
name = "webtesto"
version = "0.1.0"
authors = ["Matthew Tran <0e4ef622@gmail.com>"]
[dependencies]
actix-web = "0.6"
actix = "^0.5.8"
futures = "0.1"
serde_derive = "1.0"
serde = "1.0"
extern crate actix_web;
extern crate actix;
extern crate futures;
#[macro_use]
extern crate serde_derive;
extern crate serde;
use actix_web::{ server::HttpServer, App, HttpRequest, HttpResponse, AsyncResponder, Error, HttpMessage };
use actix_web::http::Method;
use actix::prelude::*;
use futures::future::Future;
use std::fmt;
struct MsgDB {
msgs: Vec<Msg>,
}
#[derive(Clone)]
struct Msg {
content: String,
}
impl fmt::Display for Msg {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "msg content: {}", self.content)
}
}
struct AddMsg {
msg: Msg,
}
impl Actor for MsgDB {
type Context = SyncContext<Self>;
}
impl Message for AddMsg {
type Result = ();
}
impl Handler<AddMsg> for MsgDB {
type Result = ();
fn handle(&mut self, msg: AddMsg, _: &mut Self::Context) -> Self::Result {
self.msgs.push(msg.msg);
}
}
struct MsgList;
impl Message for MsgList {
type Result = Result<Vec<Msg>, Error>;
}
impl Handler<MsgList> for MsgDB {
type Result = Result<Vec<Msg>, Error>;
fn handle(&mut self, _: MsgList, _: &mut Self::Context) -> Self::Result {
Ok(self.msgs.clone())
}
}
fn index(req: HttpRequest<Addr<Syn, MsgDB>>) -> Box<Future<Item=HttpResponse, Error=Error>> {
req.state().send(MsgList {})
.from_err()
.and_then(|res| {
let mut s = "<html><body><form action='/' method='post'><input type='text' name='msg'/><button type='submit'>Add</button></form><span>".to_owned();
match res {
Ok(m) => {
for msg in m {
s += &(msg.content + "<br/>");
}
},
Err(_) => unimplemented!(),
}
Ok(HttpResponse::Ok().body(s + "</span></body></html>"))
})
.responder()
}
#[derive(Deserialize)]
struct FormData {
msg: String,
}
fn add(req: HttpRequest<Addr<Syn, MsgDB>>) -> Box<Future<Item=HttpResponse, Error=Error>> {
req.clone().urlencoded::<FormData>()
.from_err()
.and_then(move |data| {
req.state().send(AddMsg {msg: Msg { content: data.msg } }).wait();
index(req)
})
.responder()
}
fn main() {
let sys = actix::System::new("what");
let addr = SyncArbiter::start(1, || MsgDB { msgs: vec![] });
HttpServer::new(move || {
App::with_state(addr.clone())
.resource("/", |r| r.route().a(|r| if *r.method() == Method::POST { add(r) } else { index(r) }))})
//.resource("/add", |r| r.post().a(add))})
.bind("0.0.0.0:2000").unwrap()
.start();
println!("started");
let _ = sys.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment