2018-08-27 11:56:26 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate tera;
|
|
|
|
|
2019-03-11 02:23:44 +01:00
|
|
|
use std::{env, io};
|
|
|
|
|
|
|
|
use actix_files as fs;
|
|
|
|
use actix_session::CookieSession;
|
2018-08-27 11:56:26 +02:00
|
|
|
use actix_web::middleware::{ErrorHandlers, Logger};
|
2019-03-11 02:23:44 +01:00
|
|
|
use actix_web::{http, web, App, HttpServer};
|
2018-08-27 11:56:26 +02:00
|
|
|
use dotenv::dotenv;
|
|
|
|
use tera::Tera;
|
|
|
|
|
|
|
|
mod api;
|
|
|
|
mod db;
|
|
|
|
mod model;
|
|
|
|
mod schema;
|
|
|
|
mod session;
|
|
|
|
|
|
|
|
static SESSION_SIGNING_KEY: &[u8] = &[0; 32];
|
|
|
|
|
2019-03-11 02:23:44 +01:00
|
|
|
fn main() -> io::Result<()> {
|
2018-08-27 11:56:26 +02:00
|
|
|
dotenv().ok();
|
|
|
|
|
2019-03-11 02:23:44 +01:00
|
|
|
env::set_var("RUST_LOG", "actix_todo=debug,actix_web=info");
|
2018-08-27 11:56:26 +02:00
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
|
|
|
let pool = db::init_pool(&database_url).expect("Failed to create pool");
|
|
|
|
|
|
|
|
let app = move || {
|
|
|
|
debug!("Constructing the App");
|
|
|
|
|
|
|
|
let templates: Tera = compile_templates!("templates/**/*");
|
|
|
|
|
2019-03-11 02:23:44 +01:00
|
|
|
let session_store = CookieSession::signed(SESSION_SIGNING_KEY).secure(false);
|
2018-08-27 11:56:26 +02:00
|
|
|
|
|
|
|
let error_handlers = ErrorHandlers::new()
|
|
|
|
.handler(
|
|
|
|
http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
api::internal_server_error,
|
|
|
|
)
|
|
|
|
.handler(http::StatusCode::BAD_REQUEST, api::bad_request)
|
|
|
|
.handler(http::StatusCode::NOT_FOUND, api::not_found);
|
|
|
|
|
2019-03-11 02:23:44 +01:00
|
|
|
App::new()
|
|
|
|
.state(templates)
|
|
|
|
.state(pool.clone())
|
2018-08-27 11:56:26 +02:00
|
|
|
.middleware(Logger::default())
|
|
|
|
.middleware(session_store)
|
|
|
|
.middleware(error_handlers)
|
2019-03-11 02:23:44 +01:00
|
|
|
.service(web::resource("/").route(web::get().to_async(api::index)))
|
|
|
|
.service(web::resource("/todo").route(web::post().to_async(api::create)))
|
|
|
|
.service(
|
|
|
|
web::resource("/todo/{id}").route(web::post().to_async(api::update)),
|
|
|
|
)
|
|
|
|
.service(fs::Files::new("/static", "static/"))
|
2018-08-27 11:56:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
debug!("Starting server");
|
2019-03-11 02:23:44 +01:00
|
|
|
HttpServer::new(app).bind("localhost:8088")?.run()
|
2018-08-27 11:56:26 +02:00
|
|
|
}
|