1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

Merge pull request #27 from ami44/master

move examples/websocket.rs to examples/websocket
This commit is contained in:
Ami44 2018-01-01 12:26:07 +01:00 committed by GitHub
commit e798af26a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 11 deletions

View File

@ -46,6 +46,7 @@ script:
cd examples/diesel && cargo check && cd ../.. cd examples/diesel && cargo check && cd ../..
cd examples/tls && cargo check && cd ../.. cd examples/tls && cargo check && cd ../..
cd examples/websocket-chat && cargo check && cd ../.. cd examples/websocket-chat && cargo check && cd ../..
cd examples/websocket && cargo check && cd ../..
fi fi
- | - |
if [[ "$TRAVIS_RUST_VERSION" == "nightly" && $CLIPPY ]]; then if [[ "$TRAVIS_RUST_VERSION" == "nightly" && $CLIPPY ]]; then

View File

@ -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/) * [Basic](https://github.com/actix/actix-web/tree/master/examples/basic/)
* [Stateful](https://github.com/actix/actix-web/tree/master/examples/state/) * [Stateful](https://github.com/actix/actix-web/tree/master/examples/state/)
* [Mulitpart streams](https://github.com/actix/actix-web/tree/master/examples/multipart/) * [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/) * [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/) * [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/) * [SSL / HTTP/2.0](https://github.com/actix/actix-web/tree/master/examples/tls/)

View File

@ -6,8 +6,8 @@ workspace = "../.."
[dependencies] [dependencies]
env_logger = "0.4" env_logger = "0.4"
actix = "^0.3.1" actix = "^0.3.5"
actix-web = { git = "https://github.com/actix/actix-web.git" } actix-web = { git = "https://github.com/actix/actix-web", features=["signal"] }
futures = "0.1" futures = "0.1"
uuid = { version = "0.5", features = ["serde", "v4"] } uuid = { version = "0.5", features = ["serde", "v4"] }

View File

@ -4,17 +4,40 @@ Diesel's `Getting Started` guide using SQLite for Actix web
## Usage ## Usage
install `diesel_cli` ### init database sqlite
```bash ```bash
cargo install diesel_cli --no-default-features --features sqlite cargo install diesel_cli --no-default-features --features sqlite
``` cd actix-web/examples/diesel
```bash
echo "DATABASE_URL=file:test.db" > .env echo "DATABASE_URL=file:test.db" > .env
diesel migration run 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 ## 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) 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)

View File

@ -16,8 +16,11 @@ extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate env_logger; extern crate env_logger;
use actix::*;
use actix_web::*; use actix_web::*;
use actix::prelude::*; use actix::prelude::*;
#[cfg(target_os = "linux")] use actix::actors::signal::{ProcessSignals, Subscribe};
use diesel::prelude::*; use diesel::prelude::*;
use futures::future::Future; use futures::future::Future;
@ -59,7 +62,7 @@ fn main() {
}); });
// Start http server // Start http server
HttpServer::new(move || { let _addr = HttpServer::new(move || {
Application::with_state(State{db: addr.clone()}) Application::with_state(State{db: addr.clone()})
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
@ -67,6 +70,11 @@ fn main() {
.bind("127.0.0.1:8080").unwrap() .bind("127.0.0.1:8080").unwrap()
.start(); .start();
if cfg!(target_os = "linux") { // Subscribe to unix signals
let signals = Arbiter::system_registry().get::<ProcessSignals>();
signals.send(Subscribe(_addr.subscriber()));
}
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");
let _ = sys.run(); let _ = sys.run();
} }

View File

@ -0,0 +1,14 @@
[package]
name = "websocket"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
[[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"] }

View File

@ -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``

View File

@ -10,7 +10,7 @@ extern crate env_logger;
use actix::*; use actix::*;
use actix_web::*; use actix_web::*;
#[cfg(target_os = "linux")] use actix::actors::signal::{ProcessSignals, Subscribe};
/// do websocket handshake and start `MyWebSocket` actor /// do websocket handshake and start `MyWebSocket` actor
fn ws_index(r: HttpRequest) -> Result<HttpResponse, Error> { fn ws_index(r: HttpRequest) -> Result<HttpResponse, Error> {
@ -60,7 +60,7 @@ fn main() {
let _ = env_logger::init(); let _ = env_logger::init();
let sys = actix::System::new("ws-example"); let sys = actix::System::new("ws-example");
HttpServer::new( let _addr = HttpServer::new(
|| Application::new() || Application::new()
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
@ -68,11 +68,16 @@ fn main() {
.resource("/ws/", |r| r.method(Method::GET).f(ws_index)) .resource("/ws/", |r| r.method(Method::GET).f(ws_index))
// static files // static files
.resource("/{tail:.*}", .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 // start http server on 127.0.0.1:8080
.bind("127.0.0.1:8080").unwrap() .bind("127.0.0.1:8080").unwrap()
.start(); .start();
if cfg!(target_os = "linux") { // Subscribe to unix signals
let signals = Arbiter::system_registry().get::<ProcessSignals>();
signals.send(Subscribe(_addr.subscriber()));
}
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");
let _ = sys.run(); let _ = sys.run();
} }