2019-10-08 02:17:11 +00:00
|
|
|
#[macro_use]
|
2019-10-15 12:04:34 +00:00
|
|
|
extern crate diesel;
|
2019-10-09 15:59:52 +00:00
|
|
|
#[macro_use]
|
2019-10-22 18:34:26 +00:00
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
2019-10-09 15:59:52 +00:00
|
|
|
extern crate serde;
|
2019-10-08 02:17:11 +00:00
|
|
|
|
2019-10-21 14:57:14 +00:00
|
|
|
#[cfg_attr(not(feature = "dev"), macro_use)]
|
2019-10-17 18:33:52 +00:00
|
|
|
extern crate diesel_migrations;
|
|
|
|
|
2019-10-21 21:16:28 +00:00
|
|
|
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
2020-01-16 03:34:55 +00:00
|
|
|
use actix_web::{web, App, HttpServer};
|
2019-10-21 18:29:39 +00:00
|
|
|
use diesel::{
|
|
|
|
r2d2::{self, ConnectionManager},
|
|
|
|
sqlite::SqliteConnection,
|
|
|
|
};
|
2019-10-15 12:04:34 +00:00
|
|
|
use std::process;
|
2019-10-15 11:52:20 +00:00
|
|
|
|
2019-10-21 18:29:39 +00:00
|
|
|
#[cfg(feature = "dev")]
|
|
|
|
use crate::setup::Config;
|
2019-10-21 15:19:54 +00:00
|
|
|
#[cfg(feature = "dev")]
|
|
|
|
use dotenv;
|
|
|
|
#[cfg(not(feature = "dev"))]
|
|
|
|
use std::fs;
|
|
|
|
|
2019-10-15 12:04:34 +00:00
|
|
|
pub mod models;
|
|
|
|
pub mod queries;
|
2019-10-17 18:35:44 +00:00
|
|
|
pub mod routes;
|
2019-10-15 12:04:34 +00:00
|
|
|
pub mod schema;
|
|
|
|
pub mod setup;
|
|
|
|
|
|
|
|
/// SQLite database connection pool
|
|
|
|
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
2019-10-08 02:37:25 +00:00
|
|
|
|
2019-10-21 14:57:14 +00:00
|
|
|
#[cfg(not(feature = "dev"))]
|
2020-01-15 05:10:48 +00:00
|
|
|
embed_migrations!();
|
2019-10-17 18:33:52 +00:00
|
|
|
|
2020-01-15 05:10:48 +00:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() {
|
2019-10-08 02:17:11 +00:00
|
|
|
let config = {
|
2019-10-21 14:57:14 +00:00
|
|
|
#[cfg(feature = "dev")]
|
2019-10-15 12:04:34 +00:00
|
|
|
{
|
|
|
|
Config::debug()
|
|
|
|
}
|
2019-10-21 14:57:14 +00:00
|
|
|
#[cfg(not(feature = "dev"))]
|
2019-10-15 12:04:34 +00:00
|
|
|
{
|
2019-10-17 18:55:59 +00:00
|
|
|
setup::init()
|
2019-10-08 02:17:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
setup::init_logger();
|
2019-10-08 02:37:25 +00:00
|
|
|
|
|
|
|
let pool = setup::create_pool(&config.database_url, config.pool_size);
|
2019-10-21 14:57:14 +00:00
|
|
|
#[cfg(not(feature = "dev"))]
|
2019-10-25 18:13:44 +00:00
|
|
|
{
|
|
|
|
embedded_migrations::run(&pool.get().unwrap()).unwrap_or_else(|e| {
|
2020-01-16 03:34:55 +00:00
|
|
|
eprintln!("Can't prepare database: {}", e);
|
2019-10-25 18:13:44 +00:00
|
|
|
process::exit(1);
|
|
|
|
});
|
|
|
|
}
|
2019-10-08 02:37:25 +00:00
|
|
|
|
2019-10-25 03:39:34 +00:00
|
|
|
let password_hash = {
|
2019-10-21 15:19:54 +00:00
|
|
|
#[cfg(feature = "dev")]
|
|
|
|
{
|
|
|
|
dotenv::dotenv().ok();
|
2019-10-25 03:39:34 +00:00
|
|
|
let password = get_env!("PASSWD");
|
|
|
|
setup::hash(&password)
|
2019-10-21 15:19:54 +00:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "dev"))]
|
|
|
|
{
|
2019-10-25 03:39:34 +00:00
|
|
|
let password_path = setup::get_password_path();
|
|
|
|
fs::read(&password_path).unwrap_or_else(|e| {
|
|
|
|
eprintln!("Can't read password hash from disk: {}.", e);
|
2019-10-21 15:19:54 +00:00
|
|
|
process::exit(1);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-08 16:06:30 +00:00
|
|
|
let port = config.port;
|
2019-10-26 13:04:23 +00:00
|
|
|
println!("Listening on port {}", port);
|
|
|
|
|
2019-10-08 02:37:25 +00:00
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.data(pool.clone())
|
2019-10-21 19:19:16 +00:00
|
|
|
.data(config.clone())
|
2019-10-25 03:39:34 +00:00
|
|
|
.data(password_hash.clone())
|
2019-10-21 21:16:28 +00:00
|
|
|
.wrap(IdentityService::new(
|
|
|
|
CookieIdentityPolicy::new(&[0; 32])
|
|
|
|
.name("filite-auth-cookie")
|
|
|
|
.secure(false),
|
|
|
|
))
|
2019-10-09 13:14:47 +00:00
|
|
|
.wrap(setup::logger_middleware())
|
2019-10-22 18:34:26 +00:00
|
|
|
.route("/", web::get().to(routes::index))
|
2019-10-21 20:59:13 +00:00
|
|
|
.route("/logout", web::get().to(routes::logout))
|
|
|
|
.route("/config", web::get().to(routes::get_config))
|
2020-01-15 20:13:08 +00:00
|
|
|
.service(
|
|
|
|
web::resource("/f")
|
|
|
|
.route(web::get().to(routes::files::select))
|
|
|
|
.route(web::post().to(routes::files::post)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/l")
|
|
|
|
.route(web::get().to(routes::links::select))
|
|
|
|
.route(web::post().to(routes::links::post)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/t")
|
|
|
|
.route(web::get().to(routes::texts::select))
|
|
|
|
.route(web::post().to(routes::texts::post)),
|
|
|
|
)
|
2019-10-09 15:59:52 +00:00
|
|
|
.service(
|
|
|
|
web::resource("/f/{id}")
|
2020-01-15 20:13:08 +00:00
|
|
|
.route(web::get().to(routes::files::get))
|
2020-01-15 05:10:48 +00:00
|
|
|
.route(web::put().to(routes::files::put))
|
|
|
|
.route(web::delete().to(routes::files::delete)),
|
2019-10-15 13:08:07 +00:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/l/{id}")
|
2020-01-15 20:13:08 +00:00
|
|
|
.route(web::get().to(routes::links::get))
|
2020-01-15 05:10:48 +00:00
|
|
|
.route(web::put().to(routes::links::put))
|
|
|
|
.route(web::delete().to(routes::links::delete)),
|
2019-10-15 13:08:07 +00:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/t/{id}")
|
2020-01-15 20:13:08 +00:00
|
|
|
.route(web::get().to(routes::texts::get))
|
2020-01-15 05:10:48 +00:00
|
|
|
.route(web::put().to(routes::texts::put))
|
|
|
|
.route(web::delete().to(routes::texts::delete)),
|
2019-10-09 15:59:52 +00:00
|
|
|
)
|
2019-10-08 02:37:25 +00:00
|
|
|
})
|
2019-10-08 16:06:30 +00:00
|
|
|
.bind(&format!("localhost:{}", port))
|
2019-10-08 02:37:25 +00:00
|
|
|
.unwrap_or_else(|e| {
|
2020-01-16 03:34:55 +00:00
|
|
|
eprintln!("Can't bind webserver to specified port: {}", e);
|
2019-10-08 02:37:25 +00:00
|
|
|
process::exit(1);
|
|
|
|
})
|
|
|
|
.run()
|
2020-01-15 05:10:48 +00:00
|
|
|
.await
|
2019-10-08 02:37:25 +00:00
|
|
|
.unwrap_or_else(|e| {
|
2020-01-16 03:34:55 +00:00
|
|
|
eprintln!("Can't start webserver: {}", e);
|
2019-10-08 02:37:25 +00:00
|
|
|
process::exit(1);
|
|
|
|
});
|
2019-10-08 02:17:11 +00:00
|
|
|
}
|