Monotone Arbeit nervt!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.5 KiB

2 years ago
  1. #[macro_use]
  2. extern crate lazy_static;
  3. #[macro_use]
  4. extern crate serde_derive;
  5. #[macro_use]
  6. extern crate diesel;
  7. #[macro_use]
  8. extern crate diesel_migrations;
  9. use actix_session::CookieSession;
  10. use actix_web::cookie::SameSite;
  11. use actix_files::Files;
  12. use actix_web::client::Client;
  13. use actix_web::{web, App, FromRequest, HttpServer};
  14. use diesel::prelude::*;
  15. use diesel::r2d2::{self, ConnectionManager};
  16. use url::Url;
  17. use crate::config::CONFIG;
  18. use crate::config::PAYLOAD_LIMIT;
  19. use crate::forward::*;
  20. mod account;
  21. mod config;
  22. mod database;
  23. mod errors;
  24. mod forward;
  25. mod sniff;
  26. mod templates;
  27. // default to postgres
  28. #[cfg(feature = "default")]
  29. type DbConn = PgConnection;
  30. #[cfg(feature = "default")]
  31. embed_migrations!("migrations/postgres");
  32. #[cfg(feature = "postgres")]
  33. type DbConn = PgConnection;
  34. #[cfg(feature = "postgres")]
  35. embed_migrations!("migrations/postgres");
  36. #[cfg(feature = "sqlite")]
  37. type DbConn = SqliteConnection;
  38. #[cfg(feature = "sqlite")]
  39. embed_migrations!("migrations/sqlite");
  40. #[cfg(feature = "mysql")]
  41. type DbConn = MysqlConnection;
  42. #[cfg(feature = "mysql")]
  43. embed_migrations!("migrations/mysql");
  44. type DbPool = r2d2::Pool<ConnectionManager<DbConn>>;
  45. #[actix_web::main]
  46. async fn main() -> std::io::Result<()> {
  47. /* std::env::set_var("RUST_LOG", "actix_web=debug");
  48. env_logger::init();*/
  49. println!("ta ta tala ~ SNCF init");
  50. println!("Checking configuration file...");
  51. CONFIG.check_version();
  52. if CONFIG.database_path.is_empty() {
  53. println!("No database specified. Please enter a MySQL, PostgreSQL or SQLite connection string in config.toml.");
  54. }
  55. debug(&format!("Opening database {}", CONFIG.database_path));
  56. let manager = ConnectionManager::<DbConn>::new(&CONFIG.database_path);
  57. let pool = r2d2::Pool::builder()
  58. .build(manager)
  59. .expect("ERROR: main: Failed to create the database pool.");
  60. let conn = pool.get().expect("ERROR: main: DB connection failed");
  61. println!("Running migrations...");
  62. embedded_migrations::run(&*conn).expect("ERROR: main: Failed to run database migrations");
  63. let forward_url =
  64. Url::parse(&CONFIG.nextcloud_url).expect("Couldn't parse the forward url from config");
  65. println!(
  66. "Now listening at {}:{}",
  67. CONFIG.listening_address, CONFIG.listening_port
  68. );
  69. // starting the http server
  70. HttpServer::new(move || {
  71. App::new()
  72. .data(pool.clone())
  73. .data(Client::new())
  74. .data(forward_url.clone())
  75. .wrap(
  76. CookieSession::signed(&[0; 32])
  77. .secure(true)
  78. .same_site(SameSite::Strict)
  79. .http_only(true)
  80. .name("sncf_cookies")
  81. )
  82. /*.route("/mimolette", web::get().to(login))*/
  83. /*.route("/login", web::post().to(forward))*/
  84. /*.wrap(middleware::Compress::default())*/
  85. .service(Files::new("/assets/", "./templates/assets/").index_file("index.html"))
  86. .route("/", web::get().to(index))
  87. .route("/link", web::post().to(forward_register))
  88. .route("/admin/{token}", web::get().to(forward_login))
  89. .default_service(web::route().to(forward))
  90. .data(String::configure(|cfg| cfg.limit(PAYLOAD_LIMIT)))
  91. .app_data(actix_web::web::Bytes::configure(|cfg| {
  92. cfg.limit(PAYLOAD_LIMIT)
  93. }))
  94. })
  95. .bind((CONFIG.listening_address.as_str(), CONFIG.listening_port))?
  96. .system_exit()
  97. .run()
  98. .await
  99. }
  100. pub fn debug(text: &str) {
  101. if CONFIG.debug_mode {
  102. println!("{}", text);
  103. }
  104. }