1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00
This commit is contained in:
Rob Ede 2022-02-18 02:44:02 +00:00
parent aca1dab890
commit fbd3b228e9
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
48 changed files with 103 additions and 261 deletions

View File

@ -5,10 +5,7 @@ use tokio::sync::RwLock;
use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer};
/// simple handle
async fn success(
enforcer: web::Data<RwLock<Enforcer>>,
req: HttpRequest,
) -> HttpResponse {
async fn success(enforcer: web::Data<RwLock<Enforcer>>, req: HttpRequest) -> HttpResponse {
let mut e = enforcer.write().await;
println!("{:?}", req);
assert_eq!(vec!["data2_admin"], e.get_roles_for_user("alice", None));

View File

@ -11,8 +11,8 @@ static ref API_KEY: String = std::env::var("SPARKPOST_API_KEY").expect("SPARKPOS
pub fn send_invitation(invitation: &Invitation) -> Result<(), ServiceError> {
let tm = Transmission::new_eu(API_KEY.as_str());
let sending_email = std::env::var("SENDING_EMAIL_ADDRESS")
.expect("SENDING_EMAIL_ADDRESS must be set");
let sending_email =
std::env::var("SENDING_EMAIL_ADDRESS").expect("SENDING_EMAIL_ADDRESS must be set");
// new email message with sender name and email
let mut email = Message::new(EmailAddress::new(sending_email, "Let's Organise"));

View File

@ -20,14 +20,11 @@ pub enum ServiceError {
impl ResponseError for ServiceError {
fn error_response(&self) -> HttpResponse {
match self {
ServiceError::InternalServerError => HttpResponse::InternalServerError()
.json("Internal Server Error, Please try later"),
ServiceError::BadRequest(ref message) => {
HttpResponse::BadRequest().json(message)
}
ServiceError::Unauthorized => {
HttpResponse::Unauthorized().json("Unauthorized")
ServiceError::InternalServerError => {
HttpResponse::InternalServerError().json("Internal Server Error, Please try later")
}
ServiceError::BadRequest(ref message) => HttpResponse::BadRequest().json(message),
ServiceError::Unauthorized => HttpResponse::Unauthorized().json("Unauthorized"),
}
}
}
@ -47,8 +44,7 @@ impl From<DBError> for ServiceError {
match error {
DBError::DatabaseError(kind, info) => {
if let DatabaseErrorKind::UniqueViolation = kind {
let message =
info.details().unwrap_or_else(|| info.message()).to_string();
let message = info.details().unwrap_or_else(|| info.message()).to_string();
return ServiceError::BadRequest(message);
}
ServiceError::InternalServerError

View File

@ -15,8 +15,7 @@ pub async fn post_invitation(
pool: web::Data<Pool>,
) -> Result<HttpResponse, actix_web::Error> {
// run diesel blocking code
web::block(move || create_invitation(invitation_data.into_inner().email, pool))
.await??;
web::block(move || create_invitation(invitation_data.into_inner().email, pool)).await??;
Ok(HttpResponse::Ok().finish())
}
@ -30,10 +29,7 @@ fn create_invitation(
}
/// Diesel query
fn query(
eml: String,
pool: web::Data<Pool>,
) -> Result<Invitation, crate::errors::ServiceError> {
fn query(eml: String, pool: web::Data<Pool>) -> Result<Invitation, crate::errors::ServiceError> {
use crate::schema::invitations::dsl::invitations;
let new_invitation: Invitation = eml.into();

View File

@ -31,8 +31,7 @@ async fn main() -> std::io::Result<()> {
let pool: models::Pool = r2d2::Pool::builder()
.build(manager)
.expect("Failed to create pool.");
let domain: String =
std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_string());
let domain: String = std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_string());
// Start http server
HttpServer::new(move || {

View File

@ -20,9 +20,10 @@ pub fn hash_password(password: &str) -> Result<String, ServiceError> {
}
pub fn verify(hash: &str, password: &str) -> Result<bool, ServiceError> {
argon2::verify_encoded_ext(hash, password.as_bytes(), SECRET_KEY.as_bytes(), &[])
.map_err(|err| {
argon2::verify_encoded_ext(hash, password.as_bytes(), SECRET_KEY.as_bytes(), &[]).map_err(
|err| {
dbg!(err);
ServiceError::Unauthorized
})
},
)
}

View File

@ -9,8 +9,7 @@ use actix_web::{
header::{self, ContentType},
Method, StatusCode,
},
middleware, web, App, Either, HttpRequest, HttpResponse, HttpServer, Responder,
Result,
middleware, web, App, Either, HttpRequest, HttpResponse, HttpServer, Responder, Result,
};
use async_stream::stream;
@ -44,8 +43,7 @@ async fn welcome(req: HttpRequest, session: Session) -> Result<HttpResponse> {
async fn default_handler(req_method: Method) -> Result<impl Responder> {
match req_method {
Method::GET => {
let file = NamedFile::open("static/404.html")?
.set_status_code(StatusCode::NOT_FOUND);
let file = NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND);
Ok(Either::Left(file))
}
_ => Ok(Either::Right(HttpResponse::MethodNotAllowed().finish())),
@ -93,9 +91,7 @@ async fn main() -> io::Result<()> {
// with path parameters
.service(web::resource("/user/{name}").route(web::get().to(with_param)))
// async response body
.service(
web::resource("/async-body/{name}").route(web::get().to(response_body)),
)
.service(web::resource("/async-body/{name}").route(web::get().to(response_body)))
.service(
web::resource("/test").to(|req: HttpRequest| match *req.method() {
Method::GET => HttpResponse::Ok(),
@ -112,14 +108,14 @@ async fn main() -> io::Result<()> {
// static files
.service(Files::new("/static", "static").show_files_listing())
// redirect
.service(web::resource("/").route(web::get().to(
|req: HttpRequest| async move {
.service(
web::resource("/").route(web::get().to(|req: HttpRequest| async move {
println!("{:?}", req);
HttpResponse::Found()
.insert_header((header::LOCATION, "static/welcome.html"))
.finish()
},
)))
})),
)
// default
.default_service(web::to(default_handler))
})

View File

@ -93,8 +93,7 @@ async fn main() -> std::io::Result<()> {
env_logger::init();
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")?
.run()

View File

@ -2,15 +2,11 @@ use actix_web::{web, Error, HttpResponse};
use crate::common::{Part, Product};
pub async fn get_products(
_query: web::Query<Option<Part>>,
) -> Result<HttpResponse, Error> {
pub async fn get_products(_query: web::Query<Option<Part>>) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().finish())
}
pub async fn add_product(
_new_product: web::Json<Product>,
) -> Result<HttpResponse, Error> {
pub async fn add_product(_new_product: web::Json<Product>) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().finish())
}

View File

@ -49,10 +49,7 @@ pub async fn create(
session: Session,
) -> Result<HttpResponse, Error> {
if params.description.is_empty() {
session::set_flash(
&session,
FlashMessage::error("Description cannot be empty"),
)?;
session::set_flash(&session, FlashMessage::error("Description cannot be empty"))?;
Ok(redirect_to("/"))
} else {
db::create_task(params.into_inner().description, &pool)
@ -133,9 +130,7 @@ pub fn not_found<B>(res: dev::ServiceResponse<B>) -> Result<ErrorHandlerResponse
Ok(ErrorHandlerResponse::Response(res.into_response(new_resp)))
}
pub fn internal_server_error<B>(
res: dev::ServiceResponse<B>,
) -> Result<ErrorHandlerResponse<B>> {
pub fn internal_server_error<B>(res: dev::ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
let new_resp = NamedFile::open("static/errors/500.html")?
.set_status_code(res.status())
.into_response(res.request())

View File

@ -32,8 +32,7 @@ async fn main() -> io::Result<()> {
HttpServer::new(move || {
log::debug!("Constructing the App");
let mut templates =
Tera::new("templates/**/*").expect("errors in tera templates");
let mut templates = Tera::new("templates/**/*").expect("errors in tera templates");
templates.autoescape_on(vec!["tera"]);
let session_store = CookieSession::signed(SESSION_SIGNING_KEY).secure(false);

View File

@ -28,10 +28,7 @@ impl Task {
Ok(tasks)
}
pub async fn insert(
todo: NewTask,
connection: &SqlitePool,
) -> Result<(), sqlx::Error> {
pub async fn insert(todo: NewTask, connection: &SqlitePool) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
INSERT INTO tasks (description)
@ -45,10 +42,7 @@ impl Task {
Ok(())
}
pub async fn toggle_with_id(
id: i32,
connection: &SqlitePool,
) -> Result<(), sqlx::Error> {
pub async fn toggle_with_id(id: i32, connection: &SqlitePool) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
UPDATE tasks
@ -63,10 +57,7 @@ impl Task {
Ok(())
}
pub async fn delete_with_id(
id: i32,
connection: &SqlitePool,
) -> Result<(), sqlx::Error> {
pub async fn delete_with_id(id: i32, connection: &SqlitePool) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
DELETE FROM tasks

View File

@ -36,8 +36,7 @@ async fn get_user(
if let Some(user) = user {
Ok(HttpResponse::Ok().json(user))
} else {
let res = HttpResponse::NotFound()
.body(format!("No user found with uid: {}", user_uid));
let res = HttpResponse::NotFound().body(format!("No user found with uid: {}", user_uid));
Ok(res)
}
}

View File

@ -25,10 +25,7 @@ async fn add_user(client: web::Data<Client>, form: web::Form<User>) -> HttpRespo
/// Gets the user with the supplied username.
#[get("/get_user/{username}")]
async fn get_user(
client: web::Data<Client>,
username: web::Path<String>,
) -> HttpResponse {
async fn get_user(client: web::Data<Client>, username: web::Path<String>) -> HttpResponse {
let username = username.into_inner();
let collection: Collection<User> = client.database(DB_NAME).collection(COLL_NAME);
match collection
@ -36,8 +33,9 @@ async fn get_user(
.await
{
Ok(Some(user)) => HttpResponse::Ok().json(user),
Ok(None) => HttpResponse::NotFound()
.body(format!("No user found with username {}", username)),
Ok(None) => {
HttpResponse::NotFound().body(format!("No user found with username {}", username))
}
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
}
}
@ -59,8 +57,7 @@ async fn create_username_index(client: &Client) {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let uri = std::env::var("MONGODB_URI")
.unwrap_or_else(|_| "mongodb://localhost:27017".into());
let uri = std::env::var("MONGODB_URI").unwrap_or_else(|_| "mongodb://localhost:27017".into());
let client = Client::with_uri_str(uri).await.expect("failed to connect");
create_username_index(&client).await;

View File

@ -9,8 +9,7 @@ use super::*;
#[actix_web::test]
#[ignore = "requires MongoDB instance running"]
async fn test() {
let uri = std::env::var("MONGODB_URI")
.unwrap_or_else(|_| "mongodb://localhost:27017".into());
let uri = std::env::var("MONGODB_URI").unwrap_or_else(|_| "mongodb://localhost:27017".into());
let client = Client::with_uri_str(uri).await.expect("failed to connect");

View File

@ -46,9 +46,7 @@ async fn cache_stuff(
}
}
async fn del_stuff(
redis: web::Data<Addr<RedisActor>>,
) -> actix_web::Result<HttpResponse> {
async fn del_stuff(redis: web::Data<Addr<RedisActor>>) -> actix_web::Result<HttpResponse> {
let res = redis
.send(Command(resp_array![
"DEL",

View File

@ -63,13 +63,8 @@ async fn main() -> io::Result<()> {
// store db pool as Data object
.app_data(web::Data::new(pool.clone()))
.wrap(middleware::Logger::default())
.service(
web::resource("/asyncio_weather").route(web::get().to(asyncio_weather)),
)
.service(
web::resource("/parallel_weather")
.route(web::get().to(parallel_weather)),
)
.service(web::resource("/asyncio_weather").route(web::get().to(asyncio_weather)))
.service(web::resource("/parallel_weather").route(web::get().to(parallel_weather)))
})
.bind(("127.0.0.1", 8080))?
.workers(2)

View File

@ -1,8 +1,6 @@
use serde::{Deserialize, Serialize};
use actix_web::{
middleware, web, App, HttpRequest, HttpResponse, HttpServer, Responder, Result,
};
use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer, Responder, Result};
struct AppState {
foo: String,

View File

@ -23,8 +23,7 @@ async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
//make key
let s3_upload_key = format!("projects/{}/", "posts_id");
//create tmp file and upload s3 and remove tmp file
let upload_files: Vec<UploadFile> =
upload_save_file(pl.1, s3_upload_key).await.unwrap();
let upload_files: Vec<UploadFile> = upload_save_file(pl.1, s3_upload_key).await.unwrap();
println!("upload_files={:#?}", upload_files);
Ok(HttpResponse::Ok().into())
}
@ -79,8 +78,7 @@ async fn main() -> std::io::Result<()> {
dotenv().ok();
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let aws_access_key_id =
env::var("AWS_ACCESS_KEY_ID").expect("AWS_ACCESS_KEY_ID must be set");
let aws_access_key_id = env::var("AWS_ACCESS_KEY_ID").expect("AWS_ACCESS_KEY_ID must be set");
let aws_secret_access_key =
env::var("AWS_SECRET_ACCESS_KEY").expect("AWS_SECRET_ACCESS_KEY must be set");
let aws_s3_bucket_name =

View File

@ -12,10 +12,7 @@ use self::star_wars::{QueryRoot, StarWars, StarWarsSchema};
/// GraphQL endpoint
#[route("/graphql", method = "GET", method = "POST")]
async fn graphql(
schema: web::Data<StarWarsSchema>,
req: GraphQLRequest,
) -> GraphQLResponse {
async fn graphql(schema: web::Data<StarWarsSchema>, req: GraphQLRequest) -> GraphQLResponse {
schema.execute(req.into_inner()).await.into()
}

View File

@ -25,10 +25,7 @@ async fn graphql_playground() -> impl Responder {
/// GraphQL endpoint
#[route("/graphql", method = "GET", method = "POST")]
async fn graphql(
st: web::Data<Schema>,
data: web::Json<GraphQLRequest>,
) -> impl Responder {
async fn graphql(st: web::Data<Schema>, data: web::Json<GraphQLRequest>) -> impl Responder {
let user = data.execute(&st, &()).await;
HttpResponse::Ok().json(user)
}

View File

@ -1,8 +1,6 @@
use std::net::ToSocketAddrs;
use actix_web::{
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
};
use actix_web::{error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use awc::Client;
use clap::StructOpt;
use url::Url;
@ -23,9 +21,7 @@ async fn forward(
.request_from(new_url.as_str(), req.head())
.no_decompress();
let forwarded_req = match req.head().peer_addr {
Some(addr) => {
forwarded_req.insert_header(("x-forwarded-for", format!("{}", addr.ip())))
}
Some(addr) => forwarded_req.insert_header(("x-forwarded-for", format!("{}", addr.ip()))),
None => forwarded_req,
};
@ -37,9 +33,7 @@ async fn forward(
let mut client_resp = HttpResponse::build(res.status());
// Remove `Connection` as per
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection#Directives
for (header_name, header_value) in
res.headers().iter().filter(|(h, _)| *h != "connection")
{
for (header_name, header_value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
client_resp.insert_header((header_name.clone(), header_value.clone()));
}

View File

@ -66,15 +66,13 @@ async fn main() -> std::io::Result<()> {
/// Create simple rustls client config from root certificates.
fn rustls_config() -> ClientConfig {
let mut root_store = RootCertStore::empty();
root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(
|ta| {
root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
},
));
}));
rustls::ClientConfig::builder()
.with_safe_defaults()

View File

@ -10,10 +10,7 @@ use openssl::{
x509::X509,
};
pub async fn gen_tls_cert(
user_email: &str,
user_domain: &str,
) -> anyhow::Result<Certificate> {
pub async fn gen_tls_cert(user_email: &str, user_domain: &str) -> anyhow::Result<Certificate> {
// Create acme-challenge dir.
fs::create_dir("./acme-challenge").unwrap();

View File

@ -5,13 +5,11 @@ use std::{any::Any, env, fs::File, io::BufReader, net::SocketAddr};
use actix_tls::accept::rustls::{reexports::ServerConfig, TlsStream};
use actix_web::{
dev::Extensions, rt::net::TcpStream, web, App, HttpRequest, HttpResponse,
HttpServer, Responder,
dev::Extensions, rt::net::TcpStream, web, App, HttpRequest, HttpResponse, HttpServer, Responder,
};
use log::info;
use rustls::{
server::AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey,
RootCertStore,
server::AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore,
};
use rustls_pemfile::{certs, pkcs8_private_keys};

View File

@ -1,6 +1,4 @@
use actix_web::{
error, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder,
};
use actix_web::{error, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use serde::Deserialize;
#[derive(Deserialize)]
@ -18,9 +16,7 @@ fn json_error_handler(err: error::JsonPayloadError, _req: &HttpRequest) -> error
let detail = err.to_string();
let resp = match &err {
JsonPayloadError::ContentType => {
HttpResponse::UnsupportedMediaType().body(detail)
}
JsonPayloadError::ContentType => HttpResponse::UnsupportedMediaType().body(detail),
JsonPayloadError::Deserialize(json_err) if json_err.is_data() => {
HttpResponse::UnprocessableEntity().body(detail)
}

View File

@ -23,8 +23,7 @@ impl ResponseError for Error {
// builds the actual response to send back when an error occurs
fn error_response(&self) -> web::HttpResponse {
let err_json = json!({ "error": self.msg });
web::HttpResponse::build(StatusCode::from_u16(self.status).unwrap())
.json(err_json)
web::HttpResponse::build(StatusCode::from_u16(self.status).unwrap()).json(err_json)
}
}
@ -40,9 +39,7 @@ async fn main() -> io::Result<()> {
let ip_address = "127.0.0.1:8000";
println!("Running server on {}", ip_address);
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)
.expect("Can not bind to port 8000")
.run()

View File

@ -1,6 +1,4 @@
use actix_web::{
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
};
use actix_web::{error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use futures::StreamExt;
use json::JsonValue;
use serde::{Deserialize, Serialize};
@ -92,9 +90,8 @@ mod tests {
#[actix_web::test]
async fn test_index() {
let app = test::init_service(
App::new().service(web::resource("/").route(web::post().to(index))),
)
let app =
test::init_service(App::new().service(web::resource("/").route(web::post().to(index))))
.await;
let req = test::TestRequest::post()

View File

@ -18,10 +18,7 @@ use serde_json::Value;
mod convention;
/// The main handler for JSONRPC server.
async fn rpc_handler(
body: Bytes,
app_state: web::Data<AppState>,
) -> Result<HttpResponse, Error> {
async fn rpc_handler(body: Bytes, app_state: web::Data<AppState>) -> Result<HttpResponse, Error> {
let reqjson: convention::Request = match serde_json::from_slice(body.as_ref()) {
Ok(ok) => ok,
Err(_) => {
@ -90,10 +87,7 @@ async fn rpc_select(
pub trait ImplNetwork {
fn ping(&self) -> String;
fn wait(
&self,
d: u64,
) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn error::Error>>>>>;
fn wait(&self, d: u64) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn error::Error>>>>>;
fn get(&self) -> u32;
fn inc(&mut self);
@ -114,10 +108,7 @@ impl ImplNetwork for ObjNetwork {
String::from("pong")
}
fn wait(
&self,
d: u64,
) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn error::Error>>>>> {
fn wait(&self, d: u64) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn error::Error>>>>> {
async move {
actix_web::rt::time::sleep(Duration::from_secs(d)).await;
Ok(String::from("pong"))

View File

@ -7,9 +7,7 @@ use rustls_pemfile::{certs, pkcs8_private_keys};
#[get("/")]
async fn index() -> String {
String::from(
"<html><head><title>FOO BAR</title></head><body><h1>FOO BAR</h1></body></html>",
)
String::from("<html><head><title>FOO BAR</title></head><body><h1>FOO BAR</h1></body></html>")
}
#[actix_web::main]

View File

@ -32,9 +32,11 @@ async fn main() -> std::io::Result<()> {
.service(web::resource("/login").to(|| async {
"You are on /login. Go to src/redirect.rs to change this behavior."
}))
.service(web::resource("/").to(|| async {
.service(
web::resource("/").to(|| async {
"Hello, middleware! Check the console where the server is run."
}))
}),
)
})
.bind(("127.0.0.1", 8080))?
.run()

View File

@ -1,2 +1 @@
max_width = 89
reorder_imports = true

View File

@ -37,10 +37,8 @@ impl Broadcaster {
fn spawn_ping(me: Data<Mutex<Self>>) {
actix_web::rt::spawn(async move {
let mut task = IntervalStream::new(interval_at(
Instant::now(),
Duration::from_secs(10),
));
let mut task =
IntervalStream::new(interval_at(Instant::now(), Duration::from_secs(10)));
while task.next().await.is_some() {
me.lock().remove_stale_clients();
@ -85,10 +83,7 @@ pub struct Client(ReceiverStream<Bytes>);
impl Stream for Client {
type Item = Result<Bytes, Error>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match Pin::new(&mut self.0).poll_next(cx) {
Poll::Ready(Some(v)) => Poll::Ready(Some(Ok(v))),
Poll::Ready(None) => Poll::Ready(None),

View File

@ -46,10 +46,7 @@ async fn new_client(broadcaster: Data<Mutex<Broadcaster>>) -> impl Responder {
.streaming(rx)
}
async fn broadcast(
msg: Path<String>,
broadcaster: Data<Mutex<Broadcaster>>,
) -> impl Responder {
async fn broadcast(msg: Path<String>, broadcaster: Data<Mutex<Broadcaster>>) -> impl Responder {
broadcaster.lock().send(&msg.into_inner());
HttpResponse::Ok().body("msg sent")
}

View File

@ -20,10 +20,7 @@ async fn index(hb: web::Data<Handlebars<'_>>) -> HttpResponse {
}
#[get("/{user}/{data}")]
async fn user(
hb: web::Data<Handlebars<'_>>,
path: web::Path<(String, String)>,
) -> HttpResponse {
async fn user(hb: web::Data<Handlebars<'_>>, path: web::Path<(String, String)>) -> HttpResponse {
let info = path.into_inner();
let data = json!({
"user": info.0,
@ -72,10 +69,7 @@ fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<BoxBody>
}
// Generic error handler.
fn get_error_response<B>(
res: &ServiceResponse<B>,
error: &str,
) -> HttpResponse<BoxBody> {
fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse<BoxBody> {
let request = res.request();
// Provide a fallback to a simple plain text response in case an error occurs during the

View File

@ -33,8 +33,7 @@ async fn main() -> std::io::Result<()> {
println!("Listening on: 127.0.0.1:8080, open browser and visit have a try!");
HttpServer::new(|| {
let tera =
Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
let tera = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
App::new()
.app_data(web::Data::new(tera))

View File

@ -13,9 +13,7 @@ impl ResponseError for MyErr {}
#[allow(unused_must_use)] // ywrite_min causes warning: unused borrow that must be used
#[get("/")]
async fn index(
query: web::Query<HashMap<String, String>>,
) -> Result<HttpResponse, Error> {
async fn index(query: web::Query<HashMap<String, String>>) -> Result<HttpResponse, Error> {
// `ywrite_min` is work in progress check your templates before put in production
// or use `ywrite_html`
Ok(HttpResponse::Ok()

View File

@ -14,10 +14,7 @@ async fn main() -> std::io::Result<()> {
App::new()
// enable logger - always register Actix Web Logger middleware last
.wrap(middleware::Logger::default())
.service(
web::resource("/index.html")
.route(web::get().to(|| async { "Hello world!" })),
)
.service(web::resource("/index.html").route(web::get().to(|| async { "Hello world!" })))
.service(web::resource("/").to(index))
})
.bind_uds("/tmp/actix-uds.socket")?

View File

@ -14,11 +14,7 @@ impl Actor for AutobahnWebSocket {
}
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for AutobahnWebSocket {
fn handle(
&mut self,
msg: Result<ws::Message, ws::ProtocolError>,
ctx: &mut Self::Context,
) {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
if let Ok(msg) = msg {
match msg {
ws::Message::Text(text) => ctx.text(text),

View File

@ -1,7 +1,5 @@
use actix_files::{Files, NamedFile};
use actix_web::{
middleware::Logger, web, App, Error, HttpRequest, HttpServer, Responder,
};
use actix_web::{middleware::Logger, web, App, Error, HttpRequest, HttpServer, Responder};
use actix_web_actors::ws;
mod message;
@ -14,10 +12,7 @@ async fn index() -> impl Responder {
NamedFile::open_async("./static/index.html").await.unwrap()
}
async fn chat_ws(
req: HttpRequest,
stream: web::Payload,
) -> Result<impl Responder, Error> {
async fn chat_ws(req: HttpRequest, stream: web::Payload) -> Result<impl Responder, Error> {
ws::start(WsChatSession::default(), &req, stream)
}

View File

@ -20,12 +20,7 @@ impl WsChatServer {
Some(room)
}
fn add_client_to_room(
&mut self,
room_name: &str,
id: Option<usize>,
client: Client,
) -> usize {
fn add_client_to_room(&mut self, room_name: &str, id: Option<usize>, client: Client) -> usize {
let mut id = id.unwrap_or_else(rand::random::<usize>);
if let Some(room) = self.rooms.get_mut(room_name) {
@ -50,12 +45,7 @@ impl WsChatServer {
id
}
fn send_chat_message(
&mut self,
room_name: &str,
msg: &str,
_src: usize,
) -> Option<()> {
fn send_chat_message(&mut self, room_name: &str, msg: &str, _src: usize) -> Option<()> {
let mut room = self.take_room(room_name)?;
for (id, client) in room.drain() {

View File

@ -101,11 +101,7 @@ impl Handler<ChatMessage> for WsChatSession {
}
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
fn handle(
&mut self,
msg: Result<ws::Message, ws::ProtocolError>,
ctx: &mut Self::Context,
) {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
let msg = match msg {
Err(_) => {
ctx.stop();

View File

@ -68,11 +68,7 @@ impl Decoder for ChatCodec {
impl Encoder<ChatResponse> for ChatCodec {
type Error = io::Error;
fn encode(
&mut self,
msg: ChatResponse,
dst: &mut BytesMut,
) -> Result<(), Self::Error> {
fn encode(&mut self, msg: ChatResponse, dst: &mut BytesMut) -> Result<(), Self::Error> {
let msg = json::to_string(&msg).unwrap();
let msg_ref: &[u8] = msg.as_ref();
@ -112,11 +108,7 @@ impl Decoder for ClientChatCodec {
impl Encoder<ChatRequest> for ClientChatCodec {
type Error = io::Error;
fn encode(
&mut self,
msg: ChatRequest,
dst: &mut BytesMut,
) -> Result<(), Self::Error> {
fn encode(&mut self, msg: ChatRequest, dst: &mut BytesMut) -> Result<(), Self::Error> {
let msg = json::to_string(&msg).unwrap();
let msg_ref: &[u8] = msg.as_ref();

View File

@ -2,9 +2,7 @@ use std::time::{Duration, Instant};
use actix::prelude::*;
use actix_files::NamedFile;
use actix_web::{
middleware::Logger, web, App, Error, HttpRequest, HttpServer, Responder,
};
use actix_web::{middleware::Logger, web, App, Error, HttpRequest, HttpServer, Responder};
use actix_web_actors::ws;
mod codec;
@ -103,11 +101,7 @@ impl Handler<session::Message> for WsChatSession {
/// WebSocket message handler
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
fn handle(
&mut self,
msg: Result<ws::Message, ws::ProtocolError>,
ctx: &mut Self::Context,
) {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
let msg = match msg {
Err(_) => {
ctx.stop();

View File

@ -9,8 +9,7 @@ use std::{
use actix::*;
use actix_files::{Files, NamedFile};
use actix_web::{
middleware::Logger, web, App, Error, HttpRequest, HttpResponse, HttpServer,
Responder,
middleware::Logger, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
};
use actix_web_actors::ws;

View File

@ -105,11 +105,7 @@ impl Handler<server::Message> for WsChatSession {
/// WebSocket message handler
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
fn handle(
&mut self,
msg: Result<ws::Message, ws::ProtocolError>,
ctx: &mut Self::Context,
) {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
let msg = match msg {
Err(_) => {
ctx.stop();

View File

@ -3,9 +3,7 @@
//! Open `http://localhost:8080/` in browser to test.
use actix_files::NamedFile;
use actix_web::{
middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
};
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web_actors::ws;
mod server;

View File

@ -55,11 +55,7 @@ impl Actor for MyWebSocket {
/// Handler for `ws::Message`
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWebSocket {
fn handle(
&mut self,
msg: Result<ws::Message, ws::ProtocolError>,
ctx: &mut Self::Context,
) {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
// process websocket messages
println!("WS: {:?}", msg);
match msg {