1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

group imports

This commit is contained in:
Rob Ede 2022-07-09 21:08:11 +01:00
parent 4b801ba222
commit a64e21ee6e
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
23 changed files with 66 additions and 50 deletions

View File

@ -1,8 +1,8 @@
use casbin::{CoreApi, DefaultModel, Enforcer, FileAdapter, RbacApi};
use std::io; use std::io;
use tokio::sync::RwLock;
use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer}; use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer};
use casbin::{CoreApi, DefaultModel, Enforcer, FileAdapter, RbacApi};
use tokio::sync::RwLock;
/// simple handle /// simple handle
async fn success(enforcer: web::Data<RwLock<Enforcer>>, req: HttpRequest) -> HttpResponse { async fn success(enforcer: web::Data<RwLock<Enforcer>>, req: HttpRequest) -> HttpResponse {

View File

@ -100,7 +100,6 @@ async fn main() -> std::io::Result<()> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*;
use actix_web::{ use actix_web::{
middleware, middleware,
web::{get, post, resource}, web::{get, post, resource},
@ -108,6 +107,8 @@ mod test {
}; };
use serde_json::json; use serde_json::json;
use super::*;
#[actix_web::test] #[actix_web::test]
async fn test_workflow() { async fn test_workflow() {
let private_key = actix_web::cookie::Key::generate(); let private_key = actix_web::cookie::Key::generate();

View File

@ -1,10 +1,9 @@
// email_service.rs
use crate::errors::ServiceError;
use crate::models::Invitation;
use sparkpost::transmission::{ use sparkpost::transmission::{
EmailAddress, Message, Options, Recipient, Transmission, TransmissionResponse, EmailAddress, Message, Options, Recipient, Transmission, TransmissionResponse,
}; };
use crate::{errors::ServiceError, models::Invitation};
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref API_KEY: String = std::env::var("SPARKPOST_API_KEY").expect("SPARKPOST_API_KEY must be set"); static ref API_KEY: String = std::env::var("SPARKPOST_API_KEY").expect("SPARKPOST_API_KEY must be set");
} }

View File

@ -1,7 +1,8 @@
use std::convert::From;
use actix_web::{error::ResponseError, HttpResponse}; use actix_web::{error::ResponseError, HttpResponse};
use derive_more::Display; use derive_more::Display;
use diesel::result::{DatabaseErrorKind, Error as DBError}; use diesel::result::{DatabaseErrorKind, Error as DBError};
use std::convert::From;
use uuid::Error as ParseError; use uuid::Error as ParseError;
#[derive(Debug, Display)] #[derive(Debug, Display)]

View File

@ -1,7 +1,8 @@
use super::schema::*;
use diesel::{r2d2::ConnectionManager, PgConnection}; use diesel::{r2d2::ConnectionManager, PgConnection};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::schema::*;
// type alias to use in multiple places // type alias to use in multiple places
pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>; pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;

View File

@ -1,6 +1,7 @@
use crate::errors::ServiceError;
use argon2::{self, Config}; use argon2::{self, Config};
use crate::errors::ServiceError;
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub static ref SECRET_KEY: String = std::env::var("SECRET_KEY").unwrap_or_else(|_| "0123".repeat(8)); pub static ref SECRET_KEY: String = std::env::var("SECRET_KEY").unwrap_or_else(|_| "0123".repeat(8));
} }

View File

@ -24,11 +24,12 @@ async fn main() -> std::io::Result<()> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use actix_web::body::to_bytes; use actix_web::body::to_bytes;
use actix_web::dev::Service; use actix_web::dev::Service;
use actix_web::{http, test, web, App, Error}; use actix_web::{http, test, web, App, Error};
use super::*;
#[actix_web::test] #[actix_web::test]
async fn test_index() -> Result<(), Error> { async fn test_index() -> Result<(), Error> {
let app = App::new().route("/", web::get().to(index)); let app = App::new().route("/", web::get().to(index));

View File

@ -1,5 +1,4 @@
use actix_web::{middleware, App, HttpServer}; use actix_web::{middleware, App, HttpServer};
use nested_routing::app_config::config_app; use nested_routing::app_config::config_app;
#[actix_web::main] #[actix_web::main]

View File

@ -20,13 +20,14 @@ pub async fn remove_product(_id: web::Path<String>) -> Result<HttpResponse, Erro
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::app_config::config_app;
use actix_web::dev::Service;
use actix_web::{ use actix_web::{
dev::Service,
http::{header, StatusCode}, http::{header, StatusCode},
test, App, test, App,
}; };
use crate::app_config::config_app;
#[actix_web::test] #[actix_web::test]
async fn test_add_product() { async fn test_add_product() {
let app = test::init_service(App::new().configure(config_app)).await; let app = test::init_service(App::new().configure(config_app)).await;

View File

@ -1,6 +1,5 @@
use actix_web::web::Data; use actix_web::web::Data;
use actix_web::{get, App, HttpServer}; use actix_web::{get, App, HttpServer};
use redis_tang::{Builder, Pool, RedisManager}; use redis_tang::{Builder, Pool, RedisManager};
#[actix_web::main] #[actix_web::main]

View File

@ -5,9 +5,8 @@ mod model;
mod test; mod test;
use actix_web::{get, post, web, App, HttpResponse, HttpServer}; use actix_web::{get, post, web, App, HttpResponse, HttpServer};
use mongodb::{bson::doc, options::IndexOptions, Client, Collection, IndexModel};
use model::User; use model::User;
use mongodb::{bson::doc, options::IndexOptions, Client, Collection, IndexModel};
const DB_NAME: &str = "myApp"; const DB_NAME: &str = "myApp";
const COLL_NAME: &str = "users"; const COLL_NAME: &str = "users";

View File

@ -51,10 +51,11 @@ mod errors {
} }
mod db { mod db {
use crate::{errors::MyError, models::User};
use deadpool_postgres::Client; use deadpool_postgres::Client;
use tokio_pg_mapper::FromTokioPostgresRow; use tokio_pg_mapper::FromTokioPostgresRow;
use crate::{errors::MyError, models::User};
pub async fn add_user(client: &Client, user_info: User) -> Result<User, MyError> { pub async fn add_user(client: &Client, user_info: User) -> Result<User, MyError> {
let _stmt = include_str!("../sql/add_user.sql"); let _stmt = include_str!("../sql/add_user.sql");
let _stmt = _stmt.replace("$table_fields", &User::sql_table_fields()); let _stmt = _stmt.replace("$table_fields", &User::sql_table_fields());
@ -80,10 +81,11 @@ mod db {
} }
mod handlers { mod handlers {
use crate::{db, errors::MyError, models::User};
use actix_web::{web, Error, HttpResponse}; use actix_web::{web, Error, HttpResponse};
use deadpool_postgres::{Client, Pool}; use deadpool_postgres::{Client, Pool};
use crate::{db, errors::MyError, models::User};
pub async fn add_user( pub async fn add_user(
user: web::Json<User>, user: web::Json<User>,
db_pool: web::Data<Pool>, db_pool: web::Data<Pool>,
@ -98,13 +100,14 @@ mod handlers {
} }
} }
use crate::config::ExampleConfig;
use ::config::Config; use ::config::Config;
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
use dotenv::dotenv; use dotenv::dotenv;
use handlers::add_user; use handlers::add_user;
use tokio_postgres::NoTls; use tokio_postgres::NoTls;
use crate::config::ExampleConfig;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
dotenv().ok(); dotenv().ok();

View File

@ -1,7 +1,8 @@
use std::{thread::sleep, time::Duration};
use actix_web::{error, web, Error}; use actix_web::{error, web, Error};
use rusqlite::Statement; use rusqlite::Statement;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{thread::sleep, time::Duration};
pub type Pool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>; pub type Pool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>;
pub type Connection = r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>; pub type Connection = r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>;

View File

@ -1,6 +1,5 @@
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};
use serde::{Deserialize, Serialize};
struct AppState { struct AppState {
foo: String, foo: String,
@ -71,13 +70,14 @@ async fn handle_post_3(req: HttpRequest, params: web::Form<MyParams>) -> impl Re
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use actix_web::body::to_bytes; use actix_web::body::to_bytes;
use actix_web::dev::{Service, ServiceResponse}; use actix_web::dev::{Service, ServiceResponse};
use actix_web::http::{header::HeaderValue, header::CONTENT_TYPE, StatusCode}; use actix_web::http::{header::HeaderValue, header::CONTENT_TYPE, StatusCode};
use actix_web::test::{self, TestRequest}; use actix_web::test::{self, TestRequest};
use actix_web::web::{Bytes, Form}; use actix_web::web::{Bytes, Form};
use super::*;
trait BodyTest { trait BodyTest {
fn as_str(&self) -> &str; fn as_str(&self) -> &str;
} }

View File

@ -1,7 +1,7 @@
use std::io::Read as _;
use rusoto_core::Region; use rusoto_core::Region;
use rusoto_s3::S3; use rusoto_s3::{DeleteObjectRequest, PutObjectRequest, S3Client, S3};
use rusoto_s3::{DeleteObjectRequest, PutObjectRequest, S3Client};
use std::io::Read;
pub struct Client { pub struct Client {
#[allow(dead_code)] #[allow(dead_code)]

View File

@ -1,10 +1,11 @@
use crate::utils::s3::Client; use std::{convert::From, io::Write};
use actix_multipart::{Field, Multipart}; use actix_multipart::{Field, Multipart};
use actix_web::{web, web::Bytes, Error}; use actix_web::{web, web::Bytes, Error};
use futures_util::StreamExt as _; use futures_util::StreamExt as _;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::convert::From;
use std::io::Write; use crate::utils::s3::Client;
#[derive(Deserialize, Serialize, Debug, Clone)] #[derive(Deserialize, Serialize, Debug, Clone)]
pub struct UploadFile { pub struct UploadFile {

View File

@ -3,11 +3,12 @@ use juniper::{
}; };
use mysql::{from_row, params, Error as DBError, Row}; use mysql::{from_row, params, Error as DBError, Row};
use super::{
product::{Product, ProductInput},
user::{User, UserInput},
};
use crate::db::Pool; use crate::db::Pool;
use super::product::{Product, ProductInput};
use super::user::{User, UserInput};
pub struct Context { pub struct Context {
pub dbpool: Pool, pub dbpool: Pool,
} }

View File

@ -84,10 +84,9 @@ async fn main() -> std::io::Result<()> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use actix_web::{body::to_bytes, dev::Service, http, test, web, App};
use super::*; use super::*;
use actix_web::body::to_bytes;
use actix_web::dev::Service;
use actix_web::{http, test, web, App};
#[actix_web::test] #[actix_web::test]
async fn test_index() { async fn test_index() {

View File

@ -1 +1,2 @@
reorder_imports = true reorder_imports = true
group_imports = "StdExternalCrate"

View File

@ -1,6 +1,7 @@
use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer};
use std::{sync::mpsc, thread}; use std::{sync::mpsc, thread};
use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer};
#[get("/hello")] #[get("/hello")]
async fn hello() -> &'static str { async fn hello() -> &'static str {
"Hello world!" "Hello world!"

View File

@ -1,12 +1,15 @@
use actix_web::body::BoxBody; use std::io;
use actix_web::dev::ServiceResponse;
use actix_web::http::header::ContentType; use actix_web::{
use actix_web::http::StatusCode; body::BoxBody,
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers}; dev::ServiceResponse,
use actix_web::{get, web, App, HttpResponse, HttpServer, Result}; get,
http::{header::ContentType, StatusCode},
middleware::{ErrorHandlerResponse, ErrorHandlers},
web, App, HttpResponse, HttpServer, Result,
};
use handlebars::Handlebars; use handlebars::Handlebars;
use serde_json::json; use serde_json::json;
use std::io;
// Macro documentation can be found in the actix_web_codegen crate // Macro documentation can be found in the actix_web_codegen crate
#[get("/")] #[get("/")]

View File

@ -1,10 +1,13 @@
use actix_web::body::BoxBody;
use actix_web::dev::ServiceResponse;
use actix_web::http::header::ContentType;
use actix_web::http::StatusCode;
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer, Result};
use std::collections::HashMap; use std::collections::HashMap;
use actix_web::{
body::BoxBody,
dev::ServiceResponse,
error,
http::{header::ContentType, StatusCode},
middleware::{self, ErrorHandlerResponse, ErrorHandlers},
web, App, Error, HttpResponse, HttpServer, Result,
};
use tera::Tera; use tera::Tera;
// store tera template in application state // store tera template in application state

View File

@ -2,9 +2,10 @@
//! And manages available rooms. Peers send messages to other peers in same //! And manages available rooms. Peers send messages to other peers in same
//! room through `ChatServer`. //! room through `ChatServer`.
use std::collections::{HashMap, HashSet};
use actix::prelude::*; use actix::prelude::*;
use rand::{self, rngs::ThreadRng, Rng}; use rand::{self, rngs::ThreadRng, Rng};
use std::collections::{HashMap, HashSet};
use crate::session; use crate::session;