2022-04-10 22:21:45 +00:00
|
|
|
extern crate core;
|
|
|
|
|
2022-06-03 16:24:34 +00:00
|
|
|
use crate::args::ARGS;
|
|
|
|
use crate::endpoints::{
|
|
|
|
create, edit, errors, help, pasta as pasta_endpoint, pastalist, remove, static_resources,
|
|
|
|
};
|
|
|
|
use crate::pasta::Pasta;
|
|
|
|
use crate::util::dbio;
|
2022-05-07 21:30:57 +00:00
|
|
|
use actix_web::middleware::Condition;
|
2022-06-03 16:24:34 +00:00
|
|
|
use actix_web::{middleware, web, App, HttpServer};
|
2022-05-07 21:30:57 +00:00
|
|
|
use actix_web_httpauth::middleware::HttpAuthentication;
|
2022-05-02 15:53:10 +00:00
|
|
|
use chrono::Local;
|
2022-06-03 16:24:34 +00:00
|
|
|
use env_logger::Builder;
|
2022-05-02 15:53:10 +00:00
|
|
|
use log::LevelFilter;
|
2022-05-07 21:30:57 +00:00
|
|
|
use std::fs;
|
2022-06-03 16:24:34 +00:00
|
|
|
use std::io::Write;
|
|
|
|
use std::sync::Mutex;
|
2022-04-10 22:21:45 +00:00
|
|
|
|
2022-06-03 16:24:34 +00:00
|
|
|
pub mod args;
|
|
|
|
pub mod pasta;
|
2022-04-10 22:21:45 +00:00
|
|
|
|
2022-06-03 16:24:34 +00:00
|
|
|
pub mod util {
|
|
|
|
pub mod animalnumbers;
|
|
|
|
pub mod auth;
|
|
|
|
pub mod dbio;
|
|
|
|
pub mod misc;
|
|
|
|
pub mod syntaxhighlighter;
|
2022-04-10 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 16:24:34 +00:00
|
|
|
pub mod endpoints {
|
|
|
|
pub mod create;
|
|
|
|
pub mod edit;
|
|
|
|
pub mod errors;
|
|
|
|
pub mod help;
|
|
|
|
pub mod pasta;
|
|
|
|
pub mod pastalist;
|
|
|
|
pub mod remove;
|
|
|
|
pub mod static_resources;
|
2022-04-10 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 16:24:34 +00:00
|
|
|
pub struct AppState {
|
|
|
|
pub pastas: Mutex<Vec<Pasta>>,
|
2022-04-10 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2022-05-02 15:53:10 +00:00
|
|
|
Builder::new()
|
|
|
|
.format(|buf, record| {
|
|
|
|
writeln!(
|
|
|
|
buf,
|
|
|
|
"{} [{}] - {}",
|
|
|
|
Local::now().format("%Y-%m-%dT%H:%M:%S"),
|
|
|
|
record.level(),
|
|
|
|
record.args()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.filter(None, LevelFilter::Info)
|
|
|
|
.init();
|
|
|
|
|
|
|
|
log::info!(
|
2022-05-07 21:30:57 +00:00
|
|
|
"MicroBin starting on http://127.0.0.1:{}",
|
2022-06-03 16:24:34 +00:00
|
|
|
ARGS.port.to_string()
|
2022-04-23 15:47:36 +00:00
|
|
|
);
|
|
|
|
|
2022-06-03 16:24:34 +00:00
|
|
|
match fs::create_dir_all("./pasta_data") {
|
2022-05-07 21:30:57 +00:00
|
|
|
Ok(dir) => dir,
|
|
|
|
Err(error) => {
|
|
|
|
log::error!("Couldn't create data directory ./pasta_data: {:?}", error);
|
|
|
|
panic!("Couldn't create data directory ./pasta_data: {:?}", error);
|
|
|
|
}
|
|
|
|
};
|
2022-05-02 15:53:10 +00:00
|
|
|
|
2022-04-23 15:47:36 +00:00
|
|
|
let data = web::Data::new(AppState {
|
2022-05-02 15:53:10 +00:00
|
|
|
pastas: Mutex::new(dbio::load_from_file().unwrap()),
|
2022-04-23 15:47:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.app_data(data.clone())
|
2022-05-07 21:30:57 +00:00
|
|
|
.wrap(middleware::NormalizePath::trim())
|
2022-06-03 16:24:34 +00:00
|
|
|
.service(create::index)
|
|
|
|
.service(help::help)
|
|
|
|
.service(pasta_endpoint::getpasta)
|
|
|
|
.service(pasta_endpoint::getrawpasta)
|
|
|
|
.service(pasta_endpoint::redirecturl)
|
|
|
|
.service(edit::get_edit)
|
|
|
|
.service(edit::post_edit)
|
|
|
|
.service(static_resources::static_resources)
|
2022-05-07 21:30:57 +00:00
|
|
|
.service(actix_files::Files::new("/file", "./pasta_data"))
|
2022-06-03 16:24:34 +00:00
|
|
|
.service(web::resource("/upload").route(web::post().to(create::create)))
|
|
|
|
.default_service(web::route().to(errors::not_found))
|
2022-05-07 21:30:57 +00:00
|
|
|
.wrap(middleware::Logger::default())
|
2022-06-03 16:24:34 +00:00
|
|
|
.service(remove::remove)
|
|
|
|
.service(pastalist::list)
|
2022-05-07 21:30:57 +00:00
|
|
|
.wrap(Condition::new(
|
2022-06-03 16:24:34 +00:00
|
|
|
ARGS.auth_username.is_some(),
|
|
|
|
HttpAuthentication::basic(util::auth::auth_validator),
|
2022-05-07 21:30:57 +00:00
|
|
|
))
|
2022-04-23 15:47:36 +00:00
|
|
|
})
|
2022-06-03 16:24:34 +00:00
|
|
|
.bind(format!("0.0.0.0:{}", ARGS.port.to_string()))?
|
|
|
|
.workers(ARGS.threads as usize)
|
2022-04-23 15:47:36 +00:00
|
|
|
.run()
|
|
|
|
.await
|
2022-04-10 22:21:45 +00:00
|
|
|
}
|