From 7962d28a6ddd5e16e62fc725d1e429625ee8b5d9 Mon Sep 17 00:00:00 2001 From: ami44 Date: Sat, 30 Dec 2017 21:47:39 +0100 Subject: [PATCH 1/3] move examples/websocket.rs to examples/websocket --- README.md | 2 +- examples/websocket/Cargo.toml | 14 ++++++++++ examples/websocket/README.md | 27 +++++++++++++++++++ .../{websocket.rs => websocket/src/main.rs} | 11 +++++--- examples/{ => websocket}/websocket-client.py | 0 5 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 examples/websocket/Cargo.toml create mode 100644 examples/websocket/README.md rename examples/{websocket.rs => websocket/src/main.rs} (85%) rename examples/{ => websocket}/websocket-client.py (100%) diff --git a/README.md b/README.md index 84ec0cd58..37fb263ef 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Some basic benchmarks could be found in this [respository](https://github.com/fa * [Basic](https://github.com/actix/actix-web/tree/master/examples/basic/) * [Stateful](https://github.com/actix/actix-web/tree/master/examples/state/) * [Mulitpart streams](https://github.com/actix/actix-web/tree/master/examples/multipart/) -* [Simple websocket session](https://github.com/actix/actix-web/tree/master/examples/websocket.rs) +* [Simple websocket session](https://github.com/actix/actix-web/tree/master/examples/websocket/) * [Tera templates](https://github.com/actix/actix-web/tree/master/examples/template_tera/) * [Diesel integration](https://github.com/actix/actix-web/tree/master/examples/diesel/) * [SSL / HTTP/2.0](https://github.com/actix/actix-web/tree/master/examples/tls/) diff --git a/examples/websocket/Cargo.toml b/examples/websocket/Cargo.toml new file mode 100644 index 000000000..3168601a2 --- /dev/null +++ b/examples/websocket/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "websocket" +version = "0.1.0" +authors = ["Nikolay Kim "] + +[[bin]] +name = "server" +path = "src/main.rs" + +[dependencies] +env_logger = "*" +futures = "0.1" +actix = "^0.3.5" +actix-web = { git = "https://github.com/actix/actix-web.git", features=["signal"] } diff --git a/examples/websocket/README.md b/examples/websocket/README.md new file mode 100644 index 000000000..374e939ac --- /dev/null +++ b/examples/websocket/README.md @@ -0,0 +1,27 @@ +# websockect + +Simple echo websocket server. + +## Usage + +### server + +```bash +cd actix-web/examples/websocket +cargo run +# Started http server: 127.0.0.1:8080 +``` + +### web client + +- [http://localhost:8080/ws/index.html](http://localhost:8080/ws/index.html) + +### python client + +- ``pip install aiohttp`` +- ``python websocket-client.py`` + +if ubuntu : + +- ``pip3 install aiohttp`` +- ``python3 websocket-client.py`` diff --git a/examples/websocket.rs b/examples/websocket/src/main.rs similarity index 85% rename from examples/websocket.rs rename to examples/websocket/src/main.rs index 2a80add1b..a517a18a3 100644 --- a/examples/websocket.rs +++ b/examples/websocket/src/main.rs @@ -10,7 +10,7 @@ extern crate env_logger; use actix::*; use actix_web::*; - +#[cfg(target_os = "linux")] use actix::actors::signal::{ProcessSignals, Subscribe}; /// do websocket handshake and start `MyWebSocket` actor fn ws_index(r: HttpRequest) -> Reply { @@ -60,7 +60,7 @@ fn main() { let _ = env_logger::init(); let sys = actix::System::new("ws-example"); - HttpServer::new( + let _addr = HttpServer::new( || Application::new() // enable logger .middleware(middleware::Logger::default()) @@ -68,11 +68,16 @@ fn main() { .resource("/ws/", |r| r.method(Method::GET).f(ws_index)) // static files .resource("/{tail:.*}", - |r| r.h(fs::StaticFiles::new("tail", "examples/static/", true)))) + |r| r.h(fs::StaticFiles::new("tail", "../static/", true)))) // start http server on 127.0.0.1:8080 .bind("127.0.0.1:8080").unwrap() .start(); + if cfg!(target_os = "linux") { // Subscribe to unix signals + let signals = Arbiter::system_registry().get::(); + signals.send(Subscribe(_addr.subscriber())); + } + println!("Started http server: 127.0.0.1:8080"); let _ = sys.run(); } diff --git a/examples/websocket-client.py b/examples/websocket/websocket-client.py similarity index 100% rename from examples/websocket-client.py rename to examples/websocket/websocket-client.py From 5741b8b372baab656123806c1f9f427f309801cf Mon Sep 17 00:00:00 2001 From: ami44 Date: Sat, 30 Dec 2017 22:43:10 +0100 Subject: [PATCH 2/3] add examples/websocket --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 242769d4e..b72da2c8d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,6 +46,7 @@ script: cd examples/diesel && cargo check && cd ../.. cd examples/tls && cargo check && cd ../.. cd examples/websocket-chat && cargo check && cd ../.. + cd examples/websocket && cargo check && cd ../.. fi - | if [[ "$TRAVIS_RUST_VERSION" == "nightly" && $CLIPPY ]]; then From d8548ad83b5e15988f5bc1e2c83f07a46893258f Mon Sep 17 00:00:00 2001 From: ami44 Date: Sun, 31 Dec 2017 08:12:26 +0100 Subject: [PATCH 3/3] update examples/diesel readme --- examples/diesel/Cargo.toml | 4 ++-- examples/diesel/README.md | 31 +++++++++++++++++++++++++++---- examples/diesel/src/main.rs | 10 +++++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/examples/diesel/Cargo.toml b/examples/diesel/Cargo.toml index 40e78e307..e5bf2bb4d 100644 --- a/examples/diesel/Cargo.toml +++ b/examples/diesel/Cargo.toml @@ -5,8 +5,8 @@ authors = ["Nikolay Kim "] [dependencies] env_logger = "0.4" -actix = "^0.3.1" -actix-web = { git = "https://github.com/actix/actix-web.git" } +actix = "^0.3.5" +actix-web = { git = "https://github.com/actix/actix-web", features=["signal"] } futures = "0.1" uuid = { version = "0.5", features = ["serde", "v4"] } diff --git a/examples/diesel/README.md b/examples/diesel/README.md index 2aa30c5db..922ba1e3b 100644 --- a/examples/diesel/README.md +++ b/examples/diesel/README.md @@ -4,17 +4,40 @@ Diesel's `Getting Started` guide using SQLite for Actix web ## Usage -install `diesel_cli` +### init database sqlite ```bash cargo install diesel_cli --no-default-features --features sqlite -``` - -```bash +cd actix-web/examples/diesel echo "DATABASE_URL=file:test.db" > .env diesel migration run ``` +### server + +```bash +# if ubuntu : sudo apt-get install libsqlite3-dev +# if fedora : sudo dnf install libsqlite3x-devel +cd actix-web/examples/diesel +cargo run (or ``cargo watch -x run``) +# Started http server: 127.0.0.1:8080 +``` + +### web client + +[http://127.0.0.1:8080/NAME](http://127.0.0.1:8080/NAME) + +### sqlite client + +```bash +# if ubuntu : sudo apt-get install sqlite3 +# if fedora : sudo dnf install sqlite3x +sqlite3 test.db +sqlite> .tables +sqlite> select * from users; +``` + + ## Postgresql You will also find another complete example of diesel+postgresql on [https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Rust/actix](https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Rust/actix) \ No newline at end of file diff --git a/examples/diesel/src/main.rs b/examples/diesel/src/main.rs index 350a9ee5a..7303690e6 100644 --- a/examples/diesel/src/main.rs +++ b/examples/diesel/src/main.rs @@ -16,8 +16,11 @@ extern crate actix; extern crate actix_web; extern crate env_logger; +use actix::*; use actix_web::*; use actix::prelude::*; +#[cfg(target_os = "linux")] use actix::actors::signal::{ProcessSignals, Subscribe}; + use diesel::prelude::*; use futures::future::Future; @@ -59,7 +62,7 @@ fn main() { }); // Start http server - HttpServer::new(move || { + let _addr = HttpServer::new(move || { Application::with_state(State{db: addr.clone()}) // enable logger .middleware(middleware::Logger::default()) @@ -67,6 +70,11 @@ fn main() { .bind("127.0.0.1:8080").unwrap() .start(); + if cfg!(target_os = "linux") { // Subscribe to unix signals + let signals = Arbiter::system_registry().get::(); + signals.send(Subscribe(_addr.subscriber())); + } + println!("Started http server: 127.0.0.1:8080"); let _ = sys.run(); }