1
0
Fork 0
rust-actix/src/main.rs

41 lines
1.3 KiB
Rust
Raw Normal View History

2022-03-21 22:37:03 +01:00
#[macro_use]
extern crate diesel; // Required for schema.rs
2022-03-21 19:35:53 +01:00
pub(self) mod controllers;
2022-03-21 22:37:03 +01:00
pub(self) mod models;
pub(self) mod schema;
2022-03-21 19:41:05 +01:00
pub(self) mod views;
2022-03-21 19:35:53 +01:00
2022-03-21 22:37:03 +01:00
pub(self) type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
use actix_web::{middleware, web, App, HttpServer};
use diesel::{
r2d2::{self, ConnectionManager},
SqliteConnection,
};
2022-03-21 19:35:53 +01:00
#[actix_web::main]
2022-03-21 22:37:03 +01:00
async fn main() -> std::io::Result<()> {
dotenv::dotenv().ok();
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
// set up database connection pool
let conn_spec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
let manager = ConnectionManager::<SqliteConnection>::new(conn_spec);
let pool = r2d2::Pool::builder()
.build(manager)
.expect("Failed to create pool.");
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(pool.clone()))
.wrap(middleware::Logger::default())
.service(web::scope("/hello").service(controllers::hello_controller::index))
.service(web::scope("/users").service(controllers::users_controller::index))
.service(web::scope("/user").service(controllers::users_controller::create))
2022-03-21 19:35:53 +01:00
})
.bind(("127.0.0.1", 8000))?
.run()
.await
}