2020-08-19 01:21:42 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
|
|
|
|
2020-08-22 16:34:28 +02:00
|
|
|
use actix_files::Files;
|
2020-08-19 01:21:42 +02:00
|
|
|
use actix_web::client::Client;
|
|
|
|
use actix_web::{middleware, web, App, FromRequest, HttpServer};
|
|
|
|
use diesel::prelude::*;
|
|
|
|
use diesel::r2d2::{self, ConnectionManager};
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
use crate::config::CONFIG;
|
|
|
|
use crate::config::PAYLOAD_LIMIT;
|
|
|
|
use crate::forward::*;
|
|
|
|
|
2020-08-22 16:34:28 +02:00
|
|
|
mod account;
|
2020-08-19 01:21:42 +02:00
|
|
|
mod config;
|
|
|
|
mod database;
|
2020-08-22 16:34:28 +02:00
|
|
|
mod errors;
|
2020-08-19 01:21:42 +02:00
|
|
|
mod forward;
|
|
|
|
mod sniff;
|
|
|
|
mod templates;
|
|
|
|
|
|
|
|
type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
|
|
|
|
|
|
|
embed_migrations!();
|
|
|
|
|
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2020-08-22 16:34:28 +02:00
|
|
|
/* std::env::set_var("RUST_LOG", "actix_web=debug");
|
2020-08-19 01:21:42 +02:00
|
|
|
env_logger::init();*/
|
|
|
|
|
|
|
|
println!("ta ta tala ~ SNCF init");
|
|
|
|
|
|
|
|
println!("Checking configuration file...");
|
|
|
|
CONFIG.check_version();
|
|
|
|
|
|
|
|
println!("Opening database {}", CONFIG.database_path);
|
|
|
|
let manager = ConnectionManager::<SqliteConnection>::new(&CONFIG.database_path);
|
|
|
|
let pool = r2d2::Pool::builder()
|
|
|
|
.build(manager)
|
|
|
|
.expect("ERROR: main: Failed to create the database pool.");
|
|
|
|
|
|
|
|
let conn = pool.get().expect("ERROR: main: DB connection failed");
|
|
|
|
|
|
|
|
println!("Running migrations...");
|
|
|
|
embedded_migrations::run(&*conn).expect("ERROR: main: Failed to run database migrations");
|
|
|
|
|
2020-08-22 16:34:28 +02:00
|
|
|
let forward_url =
|
|
|
|
Url::parse(&CONFIG.nextcloud_url).expect("Couldn't parse the forward url from config");
|
2020-08-19 01:21:42 +02:00
|
|
|
|
|
|
|
println!(
|
|
|
|
"Now listening at {}:{}",
|
|
|
|
CONFIG.listening_address, CONFIG.listening_port
|
|
|
|
);
|
|
|
|
|
|
|
|
// starting the http server
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.data(pool.clone())
|
|
|
|
.data(Client::new())
|
|
|
|
.data(forward_url.clone())
|
|
|
|
/*.route("/mimolette", web::get().to(login))*/
|
|
|
|
/*.route("/login", web::post().to(forward))*/
|
|
|
|
.wrap(middleware::Compress::default())
|
|
|
|
.service(Files::new("/assets/", "./templates/assets/").index_file("index.html"))
|
|
|
|
.route("/", web::get().to(index))
|
|
|
|
.route("/link", web::get().to(forward_register))
|
|
|
|
.route("/admin/{token}", web::get().to(forward_login))
|
2020-08-22 16:34:28 +02:00
|
|
|
.default_service(web::route().to(forward))
|
|
|
|
.data(String::configure(|cfg| cfg.limit(PAYLOAD_LIMIT)))
|
|
|
|
.app_data(actix_web::web::Bytes::configure(|cfg| {
|
2020-08-19 01:21:42 +02:00
|
|
|
cfg.limit(PAYLOAD_LIMIT)
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
.bind((CONFIG.listening_address.as_str(), CONFIG.listening_port))?
|
2020-08-22 16:34:28 +02:00
|
|
|
.system_exit()
|
|
|
|
.run()
|
|
|
|
.await
|
2020-08-19 01:21:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn debug(text: &str) {
|
|
|
|
if CONFIG.debug_mode {
|
|
|
|
println!("{}", text);
|
|
|
|
}
|
|
|
|
}
|