mirror of https://github.com/raftario/filite.git
Added basic GET routes
This commit is contained in:
parent
c66677c43f
commit
5492744299
|
@ -31,6 +31,25 @@ dependencies = [
|
|||
"trust-dns-resolver 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-files"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"actix-http 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"actix-service 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"actix-web 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bitflags 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"derive_more 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"mime_guess 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"v_htmlescape 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-http"
|
||||
version = "0.2.10"
|
||||
|
@ -567,6 +586,7 @@ dependencies = [
|
|||
name = "filite"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"actix-files 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"actix-web 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -822,6 +842,15 @@ name = "mime"
|
|||
version = "0.3.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "mime_guess"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicase 2.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniz-sys"
|
||||
version = "0.1.12"
|
||||
|
@ -892,6 +921,15 @@ name = "nodrop"
|
|||
version = "0.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "4.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.41"
|
||||
|
@ -1594,6 +1632,14 @@ dependencies = [
|
|||
"trust-dns-proto 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicase"
|
||||
version = "2.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-bidi"
|
||||
version = "0.3.4"
|
||||
|
@ -1640,11 +1686,44 @@ dependencies = [
|
|||
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "v_escape"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"v_escape_derive 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "v_escape_derive"
|
||||
version = "0.5.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "v_htmlescape"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"v_escape 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.7.0"
|
||||
|
@ -1729,6 +1808,7 @@ dependencies = [
|
|||
[metadata]
|
||||
"checksum actix-codec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9f2c11af4b06dc935d8e1b1491dad56bfb32febc49096a91e773f8535c176453"
|
||||
"checksum actix-connect 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "9fade9bd4bb46bacde89f1e726c7a3dd230536092712f5d94d77ca57c087fca0"
|
||||
"checksum actix-files 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2803bce03271ad1684bb56489dcb7538c037a909191acdc07d6185bb829e76ac"
|
||||
"checksum actix-http 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf758ebbc4abfecbdc1ce7408601b2d7e0cd7e4766ef61183cd8ce16c194d64"
|
||||
"checksum actix-router 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "23224bb527e204261d0291102cb9b52713084def67d94f7874923baefe04ccf7"
|
||||
"checksum actix-rt 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "168620aaf00fcd2a16e621790abaf180ef7377c2f8355b4ca5775d6afc778ed8"
|
||||
|
@ -1811,6 +1891,7 @@ dependencies = [
|
|||
"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
|
||||
"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
|
||||
"checksum mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "dd1d63acd1b78403cc0c325605908475dd9b9a3acbf65ed8bcab97e27014afcf"
|
||||
"checksum mime_guess 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1a0ed03949aef72dbdf3116a383d7b38b4768e6f960528cd6a6044aa9ed68599"
|
||||
"checksum miniz-sys 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9e3ae51cea1576ceba0dde3d484d30e6e5b86dee0b2d412fe3a16a15c98202"
|
||||
"checksum miniz_oxide 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "304f66c19be2afa56530fa7c39796192eef38618da8d19df725ad7c6d6b2aaae"
|
||||
"checksum mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)" = "83f51996a3ed004ef184e16818edc51fadffe8e7ca68be67f9dee67d84d0ff23"
|
||||
|
@ -1818,6 +1899,7 @@ dependencies = [
|
|||
"checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919"
|
||||
"checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88"
|
||||
"checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945"
|
||||
"checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
|
||||
"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09"
|
||||
"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32"
|
||||
"checksum num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcef43580c035376c0705c42792c294b66974abbfd2789b511784023f71f3273"
|
||||
|
@ -1894,13 +1976,18 @@ dependencies = [
|
|||
"checksum toml 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c7aabe75941d914b72bf3e5d3932ed92ce0664d49d8432305a8b547c37227724"
|
||||
"checksum trust-dns-proto 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5559ebdf6c2368ddd11e20b11d6bbaf9e46deb803acd7815e93f5a7b4a6d2901"
|
||||
"checksum trust-dns-resolver 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6c9992e58dba365798803c0b91018ff6c8d3fc77e06977c4539af2a6bfe0a039"
|
||||
"checksum unicase 2.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2e2e6bd1e59e56598518beb94fd6db628ded570326f0a98c679a304bd9f00150"
|
||||
"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
|
||||
"checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426"
|
||||
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
|
||||
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
||||
"checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a"
|
||||
"checksum url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75b414f6c464c879d7f9babf951f23bc3743fb7313c081b2e6ca719067ea9d61"
|
||||
"checksum v_escape 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "660b101c07b5d0863deb9e7fb3138777e858d6d2a79f9e6049a27d1cc77c6da6"
|
||||
"checksum v_escape_derive 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c2ca2a14bc3fc5b64d188b087a7d3a927df87b152e941ccfbc66672e20c467ae"
|
||||
"checksum v_htmlescape 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e33e939c0d8cf047514fb6ba7d5aac78bc56677a6938b2ee67000b91f2e97e41"
|
||||
"checksum vcpkg 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "33dd455d0f96e90a75803cfeb7f948768c08d70a6de9a8d2362461935698bf95"
|
||||
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
|
||||
"checksum wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d"
|
||||
"checksum widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "effc0e4ff8085673ea7b9b2e3c73f6bd4d118810c9009ed8f1e16bd96c331db6"
|
||||
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
|
||||
|
|
|
@ -5,6 +5,7 @@ authors = ["Raphaël Thériault <raphael_theriault@outlook.com>"]
|
|||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-files = "0.1.5"
|
||||
actix-web = "1.0.8"
|
||||
cfg-if = "0.1.10"
|
||||
chrono = "0.4.9"
|
||||
|
|
77
src/main.rs
77
src/main.rs
|
@ -5,7 +5,9 @@ use filite::queries::{self, SelectFilters, SelectQuery};
|
|||
use filite::setup::{self, Config};
|
||||
use filite::Pool;
|
||||
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer, Responder};
|
||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use futures::Future;
|
||||
use std::process;
|
||||
|
||||
|
@ -47,7 +49,7 @@ macro_rules! select {
|
|||
let filters = SelectFilters::from(query.into_inner());
|
||||
web::block(move || queries::$m::select(filters, pool)).then(|result| match result {
|
||||
Ok(x) => Ok(HttpResponse::Ok().json(x)),
|
||||
Err(_) => Ok(HttpResponse::InternalServerError().into()),
|
||||
Err(_) => Err(HttpResponse::InternalServerError().finish().into()),
|
||||
})
|
||||
}
|
||||
};
|
||||
|
@ -57,6 +59,76 @@ select!(get_files, files);
|
|||
select!(get_links, links);
|
||||
select!(get_texts, texts);
|
||||
|
||||
/// Returns a generic Not Found error response
|
||||
#[inline(always)]
|
||||
fn not_found() -> Error {
|
||||
HttpResponse::NotFound().finish().into()
|
||||
}
|
||||
|
||||
/// Parses a base 36 ID
|
||||
#[inline(always)]
|
||||
fn parse_id(s: &str) -> i32 {
|
||||
i32::from_str_radix(s, 36).unwrap_or(-1)
|
||||
}
|
||||
|
||||
/// Formats a timestamp to the "Last-Modified" header format
|
||||
fn timestamp_to_last_modified(timestamp: i32) -> String {
|
||||
let datetime =
|
||||
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(timestamp as i64, 0), Utc);
|
||||
datetime.format("%a, %d %b %Y %H:%M:%S GMT").to_string()
|
||||
}
|
||||
|
||||
/// GET a file entry and statically serve it
|
||||
fn get_file(
|
||||
path: web::Path<String>,
|
||||
pool: web::Data<Pool>,
|
||||
config: web::Data<Config>,
|
||||
) -> impl Future<Item = NamedFile, Error = Error> {
|
||||
let id = parse_id(&path);
|
||||
let files_dir = config.files_dir.clone();
|
||||
web::block(move || queries::files::find(id, pool)).then(|result| match result {
|
||||
Ok(file) => {
|
||||
let mut path = files_dir;
|
||||
path.push(file.filepath);
|
||||
match NamedFile::open(&path) {
|
||||
Ok(nf) => Ok(nf),
|
||||
Err(_) => Err(HttpResponse::NotFound().finish().into()),
|
||||
}
|
||||
}
|
||||
Err(_) => Err(not_found()),
|
||||
})
|
||||
}
|
||||
|
||||
/// GET a link entry and redirect to it
|
||||
fn get_link(
|
||||
path: web::Path<String>,
|
||||
pool: web::Data<Pool>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
let id = parse_id(&path);
|
||||
web::block(move || queries::links::find(id, pool)).then(|result| match result {
|
||||
Ok(link) => Ok(HttpResponse::Found()
|
||||
.header("Location", link.forward)
|
||||
.header("Last-Modified", timestamp_to_last_modified(link.updated))
|
||||
.finish()),
|
||||
Err(_) => Err(not_found()),
|
||||
})
|
||||
}
|
||||
|
||||
/// GET a text entry and display it
|
||||
fn get_text(
|
||||
path: web::Path<String>,
|
||||
pool: web::Data<Pool>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
let id = parse_id(&path);
|
||||
web::block(move || queries::texts::find(id, pool)).then(|result| match result {
|
||||
Ok(text) => Ok(HttpResponse::Ok()
|
||||
.header("Last-Modified", timestamp_to_last_modified(text.updated))
|
||||
.body(text.contents)),
|
||||
Err(_) => Err(not_found()),
|
||||
})
|
||||
}
|
||||
|
||||
/// GET the config info
|
||||
fn get_config(config: web::Data<Config>) -> impl Responder {
|
||||
HttpResponse::Ok().json(config.get_ref())
|
||||
}
|
||||
|
@ -91,6 +163,9 @@ fn main() {
|
|||
.service(web::resource("/f").route(web::get().to_async(get_files)))
|
||||
.service(web::resource("/l").route(web::get().to_async(get_links)))
|
||||
.service(web::resource("/t").route(web::get().to_async(get_texts)))
|
||||
.route("/f/{id}", web::get().to_async(get_file))
|
||||
.route("/l/{id}", web::get().to_async(get_link))
|
||||
.route("/t/{id}", web::get().to_async(get_text))
|
||||
})
|
||||
.bind(&format!("localhost:{}", port))
|
||||
.unwrap_or_else(|e| {
|
||||
|
|
21
src/setup.rs
21
src/setup.rs
|
@ -54,7 +54,7 @@ impl Default for Config {
|
|||
let pool_size = num_cpus::get() as u32 / 2;
|
||||
let files_dir = {
|
||||
let mut path = get_data_dir();
|
||||
path.push("data");
|
||||
path.push("files");
|
||||
path
|
||||
};
|
||||
|
||||
|
@ -106,12 +106,19 @@ impl Config {
|
|||
let database_url = get_env("DATABASE_URL");
|
||||
let pool_size = get_env("POOL_SIZE").parse().expect("Invalid POOL_SIZE.");
|
||||
let files_dir = {
|
||||
let cargo_manifest_dir = env!("CARGO_MANIFEST_DIR");
|
||||
let mut path = PathBuf::from_str(cargo_manifest_dir)
|
||||
.expect("Can't convert cargo manifest dir to path.");
|
||||
let files_dir = get_env("FILES_DIR");
|
||||
path.push(&files_dir);
|
||||
path
|
||||
let path = PathBuf::from_str(&files_dir).expect("Can't convert files dir to path");
|
||||
if path.is_absolute() {
|
||||
path.canonicalize().expect("Invalid FILES_DIR")
|
||||
} else {
|
||||
let cargo_manifest_dir = env!("CARGO_MANIFEST_DIR");
|
||||
let mut cargo_manifest_dir = PathBuf::from_str(cargo_manifest_dir)
|
||||
.expect("Can't convert cargo manifest dir to path.");
|
||||
cargo_manifest_dir.push(&path);
|
||||
cargo_manifest_dir
|
||||
.canonicalize()
|
||||
.expect("Invalid FILES_DIR")
|
||||
}
|
||||
};
|
||||
|
||||
Config {
|
||||
|
@ -137,7 +144,7 @@ pub fn init_logger() {
|
|||
if cfg!(debug_assertions) && env::var_os("RUST_LOG").is_none() {
|
||||
env::set_var("RUST_LOG", "actix_web=debug");
|
||||
} else if !cfg!(debug_assertions) {
|
||||
env::set_var("RUST_LOG", "actix_web=warn");
|
||||
env::set_var("RUST_LOG", "actix_web=info");
|
||||
}
|
||||
env_logger::init();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue