1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

normalize ports to 8080

This commit is contained in:
Rob Ede 2022-03-06 00:41:32 +00:00
parent e8cd22d2f2
commit f27cc4b6b4
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
20 changed files with 122 additions and 50 deletions

56
Cargo.lock generated
View File

@ -534,6 +534,39 @@ dependencies = [
"tokio 1.17.0", "tokio 1.17.0",
] ]
[[package]]
name = "actix-web-lab"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "034cda5de08616dcc7d261977d337eb1f7dead34a788905b0b04af7fc0f4260f"
dependencies = [
"actix-files",
"actix-http",
"actix-router",
"actix-service",
"actix-utils",
"actix-web",
"ahash",
"bytes 1.1.0",
"csv",
"derive_more",
"digest 0.10.3",
"futures-core",
"futures-util",
"hmac 0.12.1",
"local-channel",
"log",
"matchit",
"mime",
"once_cell",
"pin-project-lite 0.2.8",
"serde 1.0.136",
"serde_json",
"serde_urlencoded",
"subtle",
"tokio 1.17.0",
]
[[package]] [[package]]
name = "actix_derive" name = "actix_derive"
version = "0.6.0" version = "0.6.0"
@ -814,7 +847,7 @@ dependencies = [
"actix 0.12.0", "actix 0.12.0",
"actix-cors", "actix-cors",
"actix-web", "actix-web",
"actix-web-lab", "actix-web-lab 0.10.0",
"async-graphql", "async-graphql",
"async-graphql-actix-web", "async-graphql-actix-web",
"env_logger", "env_logger",
@ -1940,6 +1973,7 @@ dependencies = [
"actix-web", "actix-web",
"derive_more", "derive_more",
"env_logger", "env_logger",
"log",
"rand 0.8.5", "rand 0.8.5",
] ]
@ -2795,6 +2829,7 @@ dependencies = [
"env_logger", "env_logger",
"futures-util", "futures-util",
"json", "json",
"log",
"serde 1.0.136", "serde 1.0.136",
"serde_json", "serde_json",
] ]
@ -2819,6 +2854,8 @@ name = "json_decode_error"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"env_logger",
"log",
"serde 1.0.136", "serde 1.0.136",
] ]
@ -2827,6 +2864,8 @@ name = "json_error"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"env_logger",
"log",
"serde 1.0.136", "serde 1.0.136",
"serde_json", "serde_json",
] ]
@ -2872,7 +2911,7 @@ version = "1.0.0"
dependencies = [ dependencies = [
"actix-cors", "actix-cors",
"actix-web", "actix-web",
"actix-web-lab", "actix-web-lab 0.10.0",
"dotenv", "dotenv",
"env_logger", "env_logger",
"juniper", "juniper",
@ -2891,7 +2930,7 @@ version = "1.0.0"
dependencies = [ dependencies = [
"actix-cors", "actix-cors",
"actix-web", "actix-web",
"actix-web-lab", "actix-web-lab 0.10.0",
"env_logger", "env_logger",
"juniper", "juniper",
"log", "log",
@ -3101,6 +3140,12 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
[[package]]
name = "matchit"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9376a4f0340565ad675d11fc1419227faf5f60cd7ac9cb2e7185a471f30af833"
[[package]] [[package]]
name = "md-5" name = "md-5"
version = "0.9.1" version = "0.9.1"
@ -3152,6 +3197,7 @@ name = "middleware-http-to-https"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"actix-web-lab 0.13.0",
"env_logger", "env_logger",
"futures-util", "futures-util",
"log", "log",
@ -4260,6 +4306,7 @@ dependencies = [
"actix-test", "actix-test",
"actix-web", "actix-web",
"env_logger", "env_logger",
"log",
"serde 1.0.136", "serde 1.0.136",
"serde_json", "serde_json",
"time 0.3.7", "time 0.3.7",
@ -5039,6 +5086,7 @@ version = "1.0.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"env_logger", "env_logger",
"log",
"tokio 1.17.0", "tokio 1.17.0",
] ]
@ -5483,7 +5531,7 @@ name = "template_sailfish"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"actix-web-lab", "actix-web-lab 0.10.0",
"env_logger", "env_logger",
"log", "log",
"sailfish", "sailfish",

View File

@ -7,7 +7,9 @@ edition = "2021"
actix-web = "4" actix-web = "4"
actix-session = "0.5" actix-session = "0.5"
actix-redis = "0.10" actix-redis = "0.10"
env_logger = "0.9" env_logger = "0.9"
log = "0.4"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"

View File

@ -73,8 +73,10 @@ async fn logout(session: Session) -> Result<String> {
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info,actix_redis=info"); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
env_logger::init();
log::info!("starting HTTP server at http://localhost:8080");
// Generate a random 32 byte key. Note that it is important to use a unique // Generate a random 32 byte key. Note that it is important to use a unique
// private key for every project. Anyone with access to the key can generate // private key for every project. Anyone with access to the key can generate

View File

@ -67,7 +67,7 @@ async fn main() -> std::io::Result<()> {
), ),
) )
}) })
.bind("127.0.0.1:3000")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -5,6 +5,8 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
derive_more = "0.99.2"
rand = "0.8" derive_more = "0.99.5"
env_logger = "0.9" env_logger = "0.9"
log = "0.4"
rand = "0.8"

View File

@ -89,13 +89,15 @@ async fn do_something() -> Result<HttpResponse, Error> {
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
env_logger::init();
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(move || { HttpServer::new(move || {
App::new().service(web::resource("/something").route(web::get().to(do_something))) App::new().service(web::resource("/something").route(web::get().to(do_something)))
}) })
.bind("127.0.0.1:8088")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -1,25 +1,30 @@
# multipart+s3 # Multipart + AWS S3
Upload a file in multipart form to aws s3(https://github.com/rusoto/rusoto)
Receive multiple data in multipart form in JSON format and receive it as a struct. Upload a file in multipart form to aws s3 (https://github.com/rusoto/rusoto).
Receive multiple data in multipart form in JSON format and receive it as a struct.
# Usage
# usage
``` ```
cd forms/multipart-s3 cd forms/multipart-s3
``` ```
1. copy .env.example .env
2. edit .env AWS_ACCESS_KEY_ID=you_key
3. edit .env AWS_SECRET_ACCESS_KEY=you_key
4. edit .env AWS_S3_BUCKET_NAME=you_key
1. copy .env.example .env
1. edit .env AWS_ACCESS_KEY_ID=you_key
1. edit .env AWS_SECRET_ACCESS_KEY=you_key
1. edit .env AWS_S3_BUCKET_NAME=you_key
# Running Server # Running Server
``` ```
cd forms/multipart-s3 cd forms/multipart-s3
cargo run (or ``cargo watch -x run``) cargo run (or ``cargo watch -x run``)
``` ```
http://localhost:3000
http://localhost:8080
# Using Other Regions # Using Other Regions
<!-- - https://www.rusoto.org/regions.html --> <!-- - https://www.rusoto.org/regions.html -->
- https://docs.rs/rusoto_core/0.42.0/rusoto_core/enum.Region.html - https://docs.rs/rusoto_core/0.42.0/rusoto_core/enum.Region.html

View File

@ -1,7 +1,7 @@
//! This example shows how to use `actix_web::HttpServer::on_connect` to access client certificates //! This example shows how to use `actix_web::HttpServer::on_connect` to access client certificates
//! pass them to a handler through connection-local data. //! pass them to a handler through connection-local data.
use std::{any::Any, env, fs::File, io::BufReader, net::SocketAddr}; use std::{any::Any, fs::File, io::BufReader, net::SocketAddr};
use actix_tls::accept::rustls::{reexports::ServerConfig, TlsStream}; use actix_tls::accept::rustls::{reexports::ServerConfig, TlsStream};
use actix_web::{ use actix_web::{
@ -69,11 +69,7 @@ fn get_client_cert(connection: &dyn Any, data: &mut Extensions) {
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
if env::var("RUST_LOG").is_err() { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
env::set_var("RUST_LOG", "info");
}
env_logger::init();
let mut cert_store = RootCertStore::empty(); let mut cert_store = RootCertStore::empty();
@ -107,7 +103,8 @@ async fn main() -> std::io::Result<()> {
.collect(); .collect();
let config = config.with_single_cert(cert_chain, keys.remove(0)).unwrap(); let config = config.with_single_cert(cert_chain, keys.remove(0)).unwrap();
// start server log::info!("starting HTTP server at http://localhost:8080 and http://localhost:8443");
HttpServer::new(|| App::new().default_service(web::to(route_whoami))) HttpServer::new(|| App::new().default_service(web::to(route_whoami)))
.on_connect(get_client_cert) .on_connect(get_client_cert)
.bind(("localhost", 8080))? .bind(("localhost", 8080))?

View File

@ -5,4 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
env_logger = "0.9"
log = "0.4"
serde = "1" serde = "1"

View File

@ -11,7 +11,7 @@ representation of the error.
```shell ```shell
cd json/json_decode_error cd json/json_decode_error
cargo run cargo run
# Started HTTP server: 127.0.0.1:8088 # Started HTTP server: 127.0.0.1:8080
``` ```
## Examples ## Examples
@ -23,7 +23,7 @@ ellipsis `...`.
- A well-formed request - A well-formed request
```shell ```shell
$ curl -i 127.0.0.1:8088 -H 'Content-Type: application/json' -d '{"name": "Alice"}' $ curl -i 127.0.0.1:8080 -H 'Content-Type: application/json' -d '{"name": "Alice"}'
HTTP/1.1 200 OK HTTP/1.1 200 OK
... ...
@ -33,7 +33,7 @@ ellipsis `...`.
- Missing `Content-Type` header - Missing `Content-Type` header
```shell ```shell
$ curl -i 127.0.0.1:8088 -d '{"name": "Bob"}' $ curl -i 127.0.0.1:8080 -d '{"name": "Bob"}'
HTTP/1.1 415 Unsupported Media Type HTTP/1.1 415 Unsupported Media Type
... ...
@ -43,7 +43,7 @@ ellipsis `...`.
- Malformed JSON - Malformed JSON
```shell ```shell
$ curl -i 127.0.0.1:8088 -H 'Content-Type: application/json' -d '{"name": "Eve}' $ curl -i 127.0.0.1:8080 -H 'Content-Type: application/json' -d '{"name": "Eve}'
HTTP/1.1 400 Bad Request HTTP/1.1 400 Bad Request
... ...
@ -53,7 +53,7 @@ ellipsis `...`.
- JSON value of wrong type - JSON value of wrong type
```shell ```shell
$ curl -i 127.0.0.1:8088 -H 'Content-Type: application/json' -d '{"name": 350}' $ curl -i 127.0.0.1:8080 -H 'Content-Type: application/json' -d '{"name": 350}'
HTTP/1.1 422 Unprocessable Entity HTTP/1.1 422 Unprocessable Entity
... ...
@ -63,7 +63,7 @@ ellipsis `...`.
- Wrong JSON key - Wrong JSON key
```shell ```shell
$ curl -i 127.0.0.1:8088 -H 'Content-Type: application/json' -d '{"namn": "John"}' $ curl -i 127.0.0.1:8080 -H 'Content-Type: application/json' -d '{"namn": "John"}'
HTTP/1.1 422 Unprocessable Entity HTTP/1.1 422 Unprocessable Entity
... ...

View File

@ -27,6 +27,10 @@ fn json_error_handler(err: error::JsonPayloadError, _req: &HttpRequest) -> error
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(|| { HttpServer::new(|| {
App::new().service(greet).app_data( App::new().service(greet).app_data(
web::JsonConfig::default() web::JsonConfig::default()
@ -34,7 +38,7 @@ async fn main() -> std::io::Result<()> {
.error_handler(json_error_handler), .error_handler(json_error_handler),
) )
}) })
.bind("127.0.0.1:8088")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -5,5 +5,8 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
env_logger = "0.9"
log = "0.4"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"

View File

@ -38,12 +38,12 @@ async fn index() -> Result<HttpResponse, Error> {
#[actix_web::main] #[actix_web::main]
async fn main() -> io::Result<()> { async fn main() -> io::Result<()> {
let ip_address = "127.0.0.1:8000"; env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
println!("Running server on {}", ip_address);
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(|| App::new().service(web::resource("/").route(web::get().to(index)))) HttpServer::new(|| App::new().service(web::resource("/").route(web::get().to(index))))
.bind(ip_address) .bind(("127.0.0.1", 8080))?
.expect("Can not bind to port 8000")
.run() .run()
.await .await
} }

View File

@ -6,8 +6,9 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
env_logger = "0.9.0" env_logger = "0.9"
futures-util = { version = "0.3.7", default-features = false, features = ["std"] } futures-util = { version = "0.3.7", default-features = false, features = ["std"] }
json = "0.12" json = "0.12"
log = "0.4"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"

View File

@ -58,8 +58,9 @@ async fn index_mjsonrust(body: web::Bytes) -> Result<HttpResponse, Error> {
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
env_logger::init();
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()

View File

@ -5,6 +5,8 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = { version = "4", features = ["rustls"] } actix-web = { version = "4", features = ["rustls"] }
actix-web-lab = "0.13"
env_logger = "0.9" env_logger = "0.9"
futures-util = { version = "0.3.7", default-features = false, features = ["std"] } futures-util = { version = "0.3.7", default-features = false, features = ["std"] }
log = "0.4" log = "0.4"

View File

@ -61,8 +61,8 @@ async fn main() -> std::io::Result<()> {
}) })
.service(index) .service(index)
}) })
.bind("127.0.0.1:80")? // Port 80 to listen for HTTP request .bind(("127.0.0.1", 80))? // HTTP port
.bind_rustls("127.0.0.1:443", config)? // Port 443 to listen for HTTPS request .bind_rustls(("127.0.0.1", 443), config)? // HTTPS port
.run() .run()
.await .await
} }

View File

@ -8,4 +8,5 @@ description = "Send a request to the server to shut it down"
actix-web = "4" actix-web = "4"
env_logger = "0.9" env_logger = "0.9"
log = "0.4"
tokio = { version = "1.16", features = ["signal"] } tokio = { version = "1.16", features = ["signal"] }

View File

@ -16,13 +16,12 @@ async fn stop(stopper: web::Data<mpsc::Sender<()>>) -> HttpResponse {
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_server=debug,actix_web=debug"); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
env_logger::init();
// create a channel // create a channel
let (tx, rx) = mpsc::channel::<()>(); let (tx, rx) = mpsc::channel::<()>();
let bind = ("127.0.0.1", 8080); log::info!("starting HTTP server at http://localhost:8080");
// start server as normal but don't .await after .run() yet // start server as normal but don't .await after .run() yet
let server = HttpServer::new(move || { let server = HttpServer::new(move || {
@ -33,7 +32,7 @@ async fn main() -> std::io::Result<()> {
.service(hello) .service(hello)
.service(stop) .service(stop)
}) })
.bind(&bind)? .bind(("127.0.0.1", 8080))?
.run(); .run();
// clone the server handle // clone the server handle

View File

@ -224,10 +224,10 @@ impl WsChatSession {
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
// Start chat server actor // start chat server actor
let server = server::ChatServer::default().start(); let server = server::ChatServer::default().start();
// Start tcp server in separate thread // start TCP server in separate thread
let srv = server.clone(); let srv = server.clone();
session::tcp_server("127.0.0.1:12345", srv); session::tcp_server("127.0.0.1:12345", srv);