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.

118 lines
3.3 KiB

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