1
0
Fork 0
mirror of https://git.42l.fr/neil/sncf.git synced 2024-05-04 23:13:11 +02:00
sncf/src/main.rs

126 lines
3.6 KiB
Rust
Raw Permalink Normal View History

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;
2021-04-13 00:01:33 +02:00
use actix_session::CookieSession;
use actix_web::cookie::SameSite;
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::{web, App, FromRequest, HttpServer};
2020-08-19 01:21:42 +02:00
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;
2020-11-01 18:00:55 +01:00
// default to postgres
#[cfg(feature = "default")]
type DbConn = PgConnection;
#[cfg(feature = "default")]
embed_migrations!("migrations/postgres");
2020-08-19 01:21:42 +02:00
2020-11-01 18:00:55 +01:00
#[cfg(feature = "postgres")]
type DbConn = PgConnection;
#[cfg(feature = "postgres")]
embed_migrations!("migrations/postgres");
#[cfg(feature = "sqlite")]
type DbConn = SqliteConnection;
#[cfg(feature = "sqlite")]
embed_migrations!("migrations/sqlite");
#[cfg(feature = "mysql")]
type DbConn = MysqlConnection;
#[cfg(feature = "mysql")]
embed_migrations!("migrations/mysql");
type DbPool = r2d2::Pool<ConnectionManager<DbConn>>;
2020-08-19 01:21:42 +02:00
2021-03-21 23:52:17 +01:00
#[actix_web::main]
2020-08-19 01:21:42 +02:00
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();
2021-03-24 20:29:24 +01:00
if CONFIG.database_path.is_empty() {
2020-11-01 18:00:55 +01:00
println!("No database specified. Please enter a MySQL, PostgreSQL or SQLite connection string in config.toml.");
}
debug(&format!("Opening database {}", CONFIG.database_path));
2020-11-01 18:00:55 +01:00
let manager = ConnectionManager::<DbConn>::new(&CONFIG.database_path);
2020-08-19 01:21:42 +02:00
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())
2021-04-13 00:01:33 +02:00
.wrap(
CookieSession::signed(&[0; 32])
.secure(true)
.same_site(SameSite::Strict)
.http_only(true)
.name("sncf_cookies")
)
2020-08-19 01:21:42 +02:00
/*.route("/mimolette", web::get().to(login))*/
/*.route("/login", web::post().to(forward))*/
/*.wrap(middleware::Compress::default())*/
2020-08-19 01:21:42 +02:00
.service(Files::new("/assets/", "./templates/assets/").index_file("index.html"))
.route("/", web::get().to(index))
.route("/link", web::post().to(forward_register))
2020-08-19 01:21:42 +02:00
.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);
}
}