actix / examples

Community showcase and examples of Actix Web ecosystem usage.
Apache License 2.0
3.7k stars 807 forks source link

Help on the return type of the prototype of the `move ||`? #130

Closed micfan closed 5 years ago

micfan commented 5 years ago

I want put the Factory parameter of App::new() into my single file: router.rs, how to declare the return type? Thanks..

HttpServer::new(move || {
        App::new()
        ...
)

error: aborting due to previous error


``` rust
///// my router.rs
use actix_web::{App, web, HttpServer};
use actix_web::middleware::{
    identity::{CookieIdentityPolicy, IdentityService},
    Logger,
};
use chrono::Duration;
use actix_files as fs;

use crate::api::{};
use crate::api;
use diesel::r2d2::ConnectionManager;
use diesel::PgConnection;
use actix::{Addr, SyncArbiter};
use crate::models::DbExecutor;
use actix_web::dev::{MessageBody, Body};
//use actix_web::app_service::{AppEntry};

pub fn app() -> App {
    // secret is a random minimum 32 bytes long base 64 string
    let secret: String = std::env::var("SECRET_KEY").unwrap_or_else(|_| "0123".repeat(8));
    let domain: String = std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_string());

    let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
    // create db connection pool
    let manager = ConnectionManager::<PgConnection>::new(database_url);
    let pool = r2d2::Pool::builder()
        .build(manager)
        .expect("Failed to create pool.");

    let address: Addr<DbExecutor> =
        SyncArbiter::start(4, move || DbExecutor(pool.clone()));

    App::new()
        .data(address.clone())
        .wrap(Logger::default())
        .wrap(IdentityService::new(
            CookieIdentityPolicy::new(secret.as_bytes())
                .name("auth")
                .path("/")
                .domain(domain.as_str())
                .max_age_time(Duration::days(1))
                .secure(false), // this can only be true if you have https
        ))
        // everything under '/api/' route
        .service(
            web::scope("/api")
                // routes for authentication
                .service(
                    web::resource("/auth")
                        .route(web::post().to_async(api::auth_routes::login))
                        .route(web::delete().to(api::auth_routes::logout))
                        .route(web::get().to_async(api::auth_routes::get_me)),
                )
                // routes to invitation
                .service(
                    web::resource("/invitation").route(
                        web::post().to_async(api::invitation_routes::register_email),
                    ),
                )
                // routes to register as a user after the
                .service(
                    web::resource("/register/{invitation_id}")
                        .route(web::post().to_async(api::register_routes::register_user)),
                ),
        )
        // serve static files
        .service(fs::Files::new("/", "./static/").index_file("index.html"))
}
fafhrd91 commented 5 years ago

You should use App::configure instead. App type is designed to be use in-place

micfan commented 5 years ago

thanks, found an example here: https://github.com/actix/examples/blob/09b0188ff99e872898793bd815382a4105219b77/async_ex2/src/bin/main.rs#L11