Move filters into main.rs for SPEED

Using a macro from the warp github to balance the OR filter tree which sped up compile times by 10x.
This commit is contained in:
Tyler Hallada 2020-10-20 21:50:01 -04:00
parent 0dc4247224
commit 3f124ce439
6 changed files with 336 additions and 419 deletions

15
.cargo/config.toml Normal file
View File

@ -0,0 +1,15 @@
# NOTE: For maximum performance, build using a nightly compiler
# If you are using rust stable, remove the "-Zshare-generics=y" below.
[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Zshare-generics=y"]
[target.x86_64-apple-darwin]
rustflags = ["-Zshare-generics=y"]
# NOTE: you must manually install lld on windows. you can easily do this with the "scoop" package manager:
# `scoop install llvm`
[target.x86_64-pc-windows-msvc]
linker = "lld-link.exe"
rustflags = ["-Clinker=lld", "-Zshare-generics=y"]

199
Cargo.lock generated
View File

@ -147,12 +147,6 @@ version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d67c978b1322c8031145b1f6c236fc371292f52c565bc96018b2971afcbffe1" checksum = "9d67c978b1322c8031145b1f6c236fc371292f52c565bc96018b2971afcbffe1"
[[package]]
name = "base64"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.12.3" version = "0.12.3"
@ -648,7 +642,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60fb4bb6bba52f78a471264d9a3b7d026cc0af47b22cd2cffbc0b787ca003e63" checksum = "60fb4bb6bba52f78a471264d9a3b7d026cc0af47b22cd2cffbc0b787ca003e63"
dependencies = [ dependencies = [
"typenum", "typenum",
"version_check 0.9.2", "version_check",
] ]
[[package]] [[package]]
@ -706,13 +700,13 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed18eb2459bf1a09ad2d6b1547840c3e5e62882fa09b9a6a20b1de8e3228848f" checksum = "ed18eb2459bf1a09ad2d6b1547840c3e5e62882fa09b9a6a20b1de8e3228848f"
dependencies = [ dependencies = [
"base64 0.12.3", "base64",
"bitflags", "bitflags",
"bytes", "bytes",
"headers-core", "headers-core",
"http", "http",
"mime 0.3.16", "mime",
"sha-1", "sha-1 0.8.2",
"time", "time",
] ]
@ -934,15 +928,6 @@ dependencies = [
"scopeguard", "scopeguard",
] ]
[[package]]
name = "log"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
dependencies = [
"log 0.4.11",
]
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.11" version = "0.4.11"
@ -1011,41 +996,20 @@ version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "mime"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0"
dependencies = [
"log 0.3.9",
]
[[package]] [[package]]
name = "mime" name = "mime"
version = "0.3.16" version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "mime_guess"
version = "1.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "216929a5ee4dd316b1702eedf5e74548c123d370f47841ceaac38ca154690ca3"
dependencies = [
"mime 0.2.6",
"phf 0.7.24",
"phf_codegen",
"unicase 1.4.2",
]
[[package]] [[package]]
name = "mime_guess" name = "mime_guess"
version = "2.0.3" version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212" checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212"
dependencies = [ dependencies = [
"mime 0.3.16", "mime",
"unicase 2.6.0", "unicase",
] ]
[[package]] [[package]]
@ -1069,7 +1033,7 @@ dependencies = [
"iovec", "iovec",
"kernel32-sys", "kernel32-sys",
"libc", "libc",
"log 0.4.11", "log",
"miow 0.2.1", "miow 0.2.1",
"net2", "net2",
"slab", "slab",
@ -1082,7 +1046,7 @@ version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656" checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656"
dependencies = [ dependencies = [
"log 0.4.11", "log",
"mio", "mio",
"miow 0.3.5", "miow 0.3.5",
"winapi 0.3.9", "winapi 0.3.9",
@ -1123,15 +1087,15 @@ dependencies = [
[[package]] [[package]]
name = "multipart" name = "multipart"
version = "0.16.1" version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "136eed74cadb9edd2651ffba732b19a450316b680e4f48d6c79e905799e19d01" checksum = "8209c33c951f07387a8497841122fc6f712165e3f9bda3e6be4645b58188f676"
dependencies = [ dependencies = [
"buf_redux", "buf_redux",
"httparse", "httparse",
"log 0.4.11", "log",
"mime 0.2.6", "mime",
"mime_guess 1.8.8", "mime_guess",
"quick-error", "quick-error",
"rand 0.6.5", "rand 0.6.5",
"safemem", "safemem",
@ -1147,7 +1111,7 @@ checksum = "2b0d88c06fe90d5ee94048ba40409ef1d9315d86f6f38c2efdaad4fb50c58b2d"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"libc", "libc",
"log 0.4.11", "log",
"openssl", "openssl",
"openssl-probe", "openssl-probe",
"openssl-sys", "openssl-sys",
@ -1286,52 +1250,13 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "phf"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3da44b85f8e8dfaec21adae67f95d93244b2ecf6ad2a692320598dcc8e6dd18"
dependencies = [
"phf_shared 0.7.24",
]
[[package]] [[package]]
name = "phf" name = "phf"
version = "0.8.0" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12" checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"
dependencies = [ dependencies = [
"phf_shared 0.8.0", "phf_shared",
]
[[package]]
name = "phf_codegen"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b03e85129e324ad4166b06b2c7491ae27fe3ec353af72e72cd1654c7225d517e"
dependencies = [
"phf_generator",
"phf_shared 0.7.24",
]
[[package]]
name = "phf_generator"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662"
dependencies = [
"phf_shared 0.7.24",
"rand 0.6.5",
]
[[package]]
name = "phf_shared"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0"
dependencies = [
"siphasher 0.2.3",
"unicase 1.4.2",
] ]
[[package]] [[package]]
@ -1340,7 +1265,7 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7" checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7"
dependencies = [ dependencies = [
"siphasher 0.3.3", "siphasher",
] ]
[[package]] [[package]]
@ -1387,7 +1312,7 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81c5b25980f9a9b5ad36e9cdc855530575396d8a57f67e14691a2440ed0d9a90" checksum = "81c5b25980f9a9b5ad36e9cdc855530575396d8a57f67e14691a2440ed0d9a90"
dependencies = [ dependencies = [
"base64 0.12.3", "base64",
"byteorder", "byteorder",
"bytes", "bytes",
"fallible-iterator", "fallible-iterator",
@ -1426,7 +1351,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",
"version_check 0.9.2", "version_check",
] ]
[[package]] [[package]]
@ -1439,7 +1364,7 @@ dependencies = [
"quote", "quote",
"syn", "syn",
"syn-mid", "syn-mid",
"version_check 0.9.2", "version_check",
] ]
[[package]] [[package]]
@ -1660,10 +1585,10 @@ dependencies = [
"cfg-if", "cfg-if",
"chrono", "chrono",
"lazy_static", "lazy_static",
"log 0.4.11", "log",
"regex", "regex",
"serde", "serde",
"siphasher 0.3.3", "siphasher",
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-postgres", "tokio-postgres",
@ -1842,6 +1767,19 @@ dependencies = [
"opaque-debug 0.2.3", "opaque-debug 0.2.3",
] ]
[[package]]
name = "sha-1"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "170a36ea86c864a3f16dd2687712dd6646f7019f301e57537c7f4dc9f5916770"
dependencies = [
"block-buffer 0.9.0",
"cfg-if",
"cpuid-bool",
"digest 0.9.0",
"opaque-debug 0.3.0",
]
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.8.2" version = "0.8.2"
@ -1886,12 +1824,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "siphasher"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac"
[[package]] [[package]]
name = "siphasher" name = "siphasher"
version = "0.3.3" version = "0.3.3"
@ -1951,7 +1883,7 @@ checksum = "88ac5a436f941c42eac509471a730df5c3c58e1450e68cd39afedbd948206273"
dependencies = [ dependencies = [
"async-native-tls", "async-native-tls",
"async-stream", "async-stream",
"base64 0.12.3", "base64",
"bitflags", "bitflags",
"byteorder", "byteorder",
"chrono", "chrono",
@ -1964,14 +1896,14 @@ dependencies = [
"hmac 0.7.1", "hmac 0.7.1",
"ipnetwork", "ipnetwork",
"libc", "libc",
"log 0.4.11", "log",
"md-5", "md-5",
"memchr", "memchr",
"percent-encoding", "percent-encoding",
"rand 0.7.3", "rand 0.7.3",
"serde", "serde",
"serde_json", "serde_json",
"sha-1", "sha-1 0.8.2",
"sha2 0.8.2", "sha2 0.8.2",
"sqlformat", "sqlformat",
"tokio", "tokio",
@ -2171,10 +2103,10 @@ dependencies = [
"bytes", "bytes",
"fallible-iterator", "fallible-iterator",
"futures", "futures",
"log 0.4.11", "log",
"parking_lot", "parking_lot",
"percent-encoding", "percent-encoding",
"phf 0.8.0", "phf",
"pin-project-lite", "pin-project-lite",
"postgres-protocol", "postgres-protocol",
"postgres-types", "postgres-types",
@ -2184,12 +2116,12 @@ dependencies = [
[[package]] [[package]]
name = "tokio-tungstenite" name = "tokio-tungstenite"
version = "0.10.1" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8b8fe88007ebc363512449868d7da4389c9400072a3f666f212c7280082882a" checksum = "6d9e878ad426ca286e4dcae09cbd4e1973a7f8987d97570e2469703dd7f5720c"
dependencies = [ dependencies = [
"futures", "futures-util",
"log 0.4.11", "log",
"pin-project", "pin-project",
"tokio", "tokio",
"tungstenite", "tungstenite",
@ -2204,7 +2136,7 @@ dependencies = [
"bytes", "bytes",
"futures-core", "futures-core",
"futures-sink", "futures-sink",
"log 0.4.11", "log",
"pin-project-lite", "pin-project-lite",
"tokio", "tokio",
] ]
@ -2231,7 +2163,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0aae59226cf195d8e74d4b34beae1859257efb4e5fed3f147d2dc2c7d372178" checksum = "f0aae59226cf195d8e74d4b34beae1859257efb4e5fed3f147d2dc2c7d372178"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"log 0.4.11", "log",
"tracing-attributes", "tracing-attributes",
"tracing-core", "tracing-core",
] ]
@ -2273,7 +2205,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e0f8c7178e13481ff6765bd169b33e8d554c5d2bbede5e32c356194be02b9b9" checksum = "5e0f8c7178e13481ff6765bd169b33e8d554c5d2bbede5e32c356194be02b9b9"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"log 0.4.11", "log",
"tracing-core", "tracing-core",
] ]
@ -2315,19 +2247,19 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]] [[package]]
name = "tungstenite" name = "tungstenite"
version = "0.10.1" version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfea31758bf674f990918962e8e5f07071a3161bd7c4138ed23e416e1ac4264e" checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23"
dependencies = [ dependencies = [
"base64 0.11.0", "base64",
"byteorder", "byteorder",
"bytes", "bytes",
"http", "http",
"httparse", "httparse",
"input_buffer", "input_buffer",
"log 0.4.11", "log",
"rand 0.7.3", "rand 0.7.3",
"sha-1", "sha-1 0.9.1",
"url", "url",
"utf-8", "utf-8",
] ]
@ -2347,22 +2279,13 @@ version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
[[package]]
name = "unicase"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33"
dependencies = [
"version_check 0.1.5",
]
[[package]] [[package]]
name = "unicase" name = "unicase"
version = "2.6.0" version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [ dependencies = [
"version_check 0.9.2", "version_check",
] ]
[[package]] [[package]]
@ -2455,12 +2378,6 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.2" version = "0.9.2"
@ -2484,15 +2401,15 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0"
dependencies = [ dependencies = [
"log 0.4.11", "log",
"try-lock", "try-lock",
] ]
[[package]] [[package]]
name = "warp" name = "warp"
version = "0.2.4" version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df341dee97c9ae29dfa5e0b0fbbbf620e0d6a36686389bedf83b3daeb8b0d0ac" checksum = "f41be6df54c97904af01aa23e613d4521eed7ab23537cede692d4058f6449407"
dependencies = [ dependencies = [
"async-compression", "async-compression",
"bytes", "bytes",
@ -2500,9 +2417,9 @@ dependencies = [
"headers", "headers",
"http", "http",
"hyper", "hyper",
"log 0.4.11", "log",
"mime 0.3.16", "mime",
"mime_guess 2.0.3", "mime_guess",
"multipart", "multipart",
"pin-project", "pin-project",
"scoped-tls", "scoped-tls",

View File

@ -1,268 +0,0 @@
use http::StatusCode;
use serde::de::DeserializeOwned;
use std::convert::Infallible;
use warp::{Filter, Rejection, Reply};
use super::handlers;
use super::models::{InteriorRefList, ListParams, MerchandiseList, Owner, Shop};
use super::Environment;
pub fn status() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::path("status")
.and(warp::get())
.map(|| StatusCode::OK) // TODO: return what api versions this server supports instead
}
pub fn shops(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("shops").and(
get_shop(env.clone())
.or(delete_shop(env.clone()))
.or(update_shop(env.clone()))
.or(create_shop(env.clone()))
.or(list_shops(env.clone()))
.or(get_latest_interior_ref_list_by_shop_id(env)),
)
}
pub fn owners(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("owners").and(
get_owner(env.clone())
.or(delete_owner(env.clone()))
.or(update_owner(env.clone()))
.or(create_owner(env.clone()))
.or(list_owners(env)),
)
}
pub fn interior_ref_lists(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("interior_ref_lists").and(
get_interior_ref_list(env.clone())
.or(delete_interior_ref_list(env.clone()))
.or(create_interior_ref_list(env.clone()))
.or(list_interior_ref_lists(env)),
)
}
pub fn merchandise_lists(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("merchandise_lists").and(
get_merchandise_list(env.clone())
.or(delete_merchandise_list(env.clone()))
.or(create_merchandise_list(env.clone()))
.or(list_merchandise_lists(env)),
)
}
pub fn get_shop(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::path::end())
.and(warp::get())
.and(with_env(env))
.and_then(handlers::get_shop)
}
pub fn create_shop(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::end()
.and(warp::post())
.and(json_body::<Shop>())
.and(warp::header::optional("api-key"))
.and(with_env(env))
.and_then(handlers::create_shop)
}
pub fn delete_shop(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::path::end())
.and(warp::delete())
.and(warp::header::optional("api-key"))
.and(with_env(env))
.and_then(handlers::delete_shop)
}
pub fn update_shop(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::patch())
.and(json_body::<Shop>())
.and(warp::header::optional("api-key"))
.and(with_env(env))
.and_then(handlers::update_shop)
}
pub fn list_shops(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_shops)
}
pub fn get_owner(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::path::end())
.and(warp::get())
.and(with_env(env))
.and_then(handlers::get_owner)
}
pub fn create_owner(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::end()
.and(warp::post())
.and(json_body::<Owner>())
.and(warp::addr::remote())
.and(warp::header::optional("api-key"))
.and(warp::header::optional("x-real-ip"))
.and(with_env(env))
.and_then(handlers::create_owner)
}
pub fn delete_owner(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::path::end())
.and(warp::delete())
.and(warp::header::optional("api-key"))
.and(with_env(env))
.and_then(handlers::delete_owner)
}
pub fn update_owner(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::path::end())
.and(warp::patch())
.and(json_body::<Owner>())
.and(warp::header::optional("api-key"))
.and(with_env(env))
.and_then(handlers::update_owner)
}
pub fn list_owners(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_owners)
}
pub fn get_interior_ref_list(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::path::end())
.and(warp::get())
.and(with_env(env))
.and_then(handlers::get_interior_ref_list)
}
pub fn create_interior_ref_list(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::end()
.and(warp::post())
.and(json_body::<InteriorRefList>())
.and(warp::header::optional("api-key"))
.and(with_env(env))
.and_then(handlers::create_interior_ref_list)
}
pub fn delete_interior_ref_list(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::path::end())
.and(warp::delete())
.and(warp::header::optional("api-key"))
.and(with_env(env))
.and_then(handlers::delete_interior_ref_list)
}
pub fn list_interior_ref_lists(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_interior_ref_lists)
}
pub fn get_latest_interior_ref_list_by_shop_id(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::path("latest_interior_ref_list"))
.and(warp::path::end())
.and(warp::get())
.and(with_env(env))
.and_then(handlers::get_latest_interior_ref_list_by_shop_id)
}
pub fn get_merchandise_list(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::path::end())
.and(warp::get())
.and(with_env(env))
.and_then(handlers::get_merchandise_list)
}
pub fn create_merchandise_list(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::end()
.and(warp::post())
.and(json_body::<MerchandiseList>())
.and(warp::header::optional("api-key"))
.and(with_env(env))
.and_then(handlers::create_merchandise_list)
}
pub fn delete_merchandise_list(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::param()
.and(warp::path::end())
.and(warp::delete())
.and(warp::header::optional("api-key"))
.and(with_env(env))
.and_then(handlers::delete_merchandise_list)
}
pub fn list_merchandise_lists(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_merchandise_lists)
}
fn with_env(env: Environment) -> impl Filter<Extract = (Environment,), Error = Infallible> + Clone {
warp::any().map(move || env.clone())
}
fn json_body<T>() -> impl Filter<Extract = (T,), Error = warp::Rejection> + Clone
where
T: Send + DeserializeOwned,
{
warp::body::content_length_limit(1024 * 64).and(warp::body::json())
}

70
src/macros.rs Normal file
View File

@ -0,0 +1,70 @@
// From SafariMonkey and jhpratt in https://github.com/seanmonstar/warp/issues/619
// I'm including this to reduce huge compile times until rustc is fixed.
/// Takes a list of handler expressions and `or`s them together
/// in a balanced tree. That is, instead of `a.or(b).or(c).or(d)`,
/// it produces `(a.or(b)).or(c.or(d))`, thus nesting the types
/// less deeply, which provides improvements in compile time.
///
/// It also applies `::warp::Filter::boxed` to each handler expression
/// when in `debug_assertions` mode, improving compile time further.
//
// The basic list splitting algorithm here is based on this gist:
// https://gist.github.com/durka/9fc479de2555225a787f
// It uses a counter from which two items are removed each time,
// stopping when the counter reaches 0. At each step, one item
// is moved from the left to the right, and thus at the end,
// there will be the same number of items in each list.
//
// The flow is as follows:
// - If there is one handler expression, debug_box it and return.
// - If there is more than one handler expression:
// - First, copy the list into two: the one that will go into the
// right side of the `or`, and one that will serve as a counter.
// Recurse with these separated by semicolons, plus an empty `left`
// list before the first semicolon.
// - Then, as long as there are at least two items in the counter
// list, remove them and move the first item on the right side of
// the first semicolon (`head`) to the left side of the first semicolon.
// - Finally, when there are one or zero items left in the counter,
// move one last item to the left, make the call this macro on both the
// left and right sides, and `or` the two sides together.
//
// For example, balanced_or_tree!(a, b, c, d, e) would take the following steps:
//
// - balanced_or_tree!(a, b, c, d, e)
// - balanced_or_tree!(@internal ; a, b, c, d, e ; a, b, c, d, e) // initialise lists
// - balanced_or_tree!(@internal a ; b, c, d, e ; c, d, e) // move one elem; remove two
// - balanced_or_tree!(@internal a, b ; c, d, e ; e) // now only one elem in counter
// - balanced_or_tree!(a, b, c).or(balanced_or_tree(d, e)) // recurse on each sublist
macro_rules! balanced_or_tree {
// Base case: just a single expression, return it wrapped in `debug_boxed`
($x:expr $(,)?) => { debug_boxed!($x) };
// Multiple expressions: recurse with three lists: left, right and counter.
($($x:expr),+ $(,)?) => {
balanced_or_tree!(@internal ; $($x),+; $($x),+)
// ^ left ^ right ^ counter
};
// Counter 1 or 2; move one more item and recurse on each sublist, and or them together
(@internal $($left:expr),*; $head:expr, $($tail:expr),+; $a:expr $(,$b:expr)?) => {
(balanced_or_tree!($($left,)* $head)).or(balanced_or_tree!($($tail),+))
};
// Counter > 2; move one item from the right to the left and subtract two from the counter
(@internal $($left:expr),*; $head:expr, $($tail:expr),+; $a:expr, $b:expr, $($more:expr),+) => {
balanced_or_tree!(@internal $($left,)* $head; $($tail),+; $($more),+)
};
}
#[cfg(debug_assertions)]
macro_rules! debug_boxed {
($x:expr) => {
::warp::Filter::boxed($x)
};
}
#[cfg(not(debug_assertions))]
macro_rules! debug_boxed {
($x:expr) => {
$x
};
}

View File

@ -1,9 +1,10 @@
use anyhow::Result; use anyhow::Result;
use clap::Clap; use clap::Clap;
use dotenv::dotenv; use dotenv::dotenv;
use http::StatusCode;
use hyper::server::Server; use hyper::server::Server;
use listenfd::ListenFd; use listenfd::ListenFd;
use serde::Serialize; use serde::{de::DeserializeOwned, Serialize};
use sqlx::postgres::PgPool; use sqlx::postgres::PgPool;
use std::convert::Infallible; use std::convert::Infallible;
use std::env; use std::env;
@ -14,12 +15,18 @@ use warp::Filter;
mod caches; mod caches;
mod db; mod db;
mod filters;
mod handlers; mod handlers;
#[macro_use]
mod macros;
mod models; mod models;
mod problem; mod problem;
use caches::Caches; use caches::Caches;
use models::interior_ref_list::InteriorRefList;
use models::merchandise_list::MerchandiseList;
use models::owner::Owner;
use models::shop::Shop;
use models::ListParams;
#[derive(Clap)] #[derive(Clap)]
#[clap(version = "0.1.0", author = "Tyler Hallada <tyler@hallada.net>")] #[clap(version = "0.1.0", author = "Tyler Hallada <tyler@hallada.net>")]
@ -54,6 +61,16 @@ struct ErrorMessage {
message: String, message: String,
} }
fn with_env(env: Environment) -> impl Filter<Extract = (Environment,), Error = Infallible> + Clone {
warp::any().map(move || env.clone())
}
fn json_body<T>() -> impl Filter<Extract = (T,), Error = warp::Rejection> + Clone
where
T: Send + DeserializeOwned,
{
warp::body::content_length_limit(1024 * 64).and(warp::body::json())
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
dotenv().ok(); dotenv().ok();
@ -76,13 +93,180 @@ async fn main() -> Result<()> {
let api_url = host_url.join("/v1/")?; let api_url = host_url.join("/v1/")?;
let env = Environment::new(api_url).await?; let env = Environment::new(api_url).await?;
let base = warp::path("v1"); let status_handler = warp::path::path("status")
let routes = filters::status() .and(warp::path::end())
.or(base.and( .and(warp::get())
filters::shops(env.clone()) .map(|| StatusCode::OK); // TODO: return what api versions this server supports instead
.or(filters::owners(env.clone())) let get_owner_handler = warp::path("owners").and(
.or(filters::interior_ref_lists(env.clone())) warp::path::param()
.or(filters::merchandise_lists(env.clone())), .and(warp::path::end())
.and(warp::get())
.and(with_env(env.clone()))
.and_then(handlers::get_owner),
);
let create_owner_handler = warp::path("owners").and(
warp::path::end()
.and(warp::post())
.and(json_body::<Owner>())
.and(warp::addr::remote())
.and(warp::header::optional("api-key"))
.and(warp::header::optional("x-real-ip"))
.and(with_env(env.clone()))
.and_then(handlers::create_owner),
);
let delete_owner_handler = warp::path("owners").and(
warp::path::param()
.and(warp::path::end())
.and(warp::delete())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::delete_owner),
);
let update_owner_handler = warp::path("owners").and(
warp::path::param()
.and(warp::path::end())
.and(warp::patch())
.and(json_body::<Owner>())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::update_owner),
);
let list_owners_handler = warp::path("owners").and(
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env.clone()))
.and_then(handlers::list_owners),
);
let get_shop_handler = warp::path("shops").and(
warp::path::param()
.and(warp::path::end())
.and(warp::get())
.and(with_env(env.clone()))
.and_then(handlers::get_shop),
);
let create_shop_handler = warp::path("shops").and(
warp::path::end()
.and(warp::post())
.and(json_body::<Shop>())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::create_shop),
);
let delete_shop_handler = warp::path("shops").and(
warp::path::param()
.and(warp::path::end())
.and(warp::delete())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::delete_shop),
);
let update_shop_handler = warp::path("shops").and(
warp::path::param()
.and(warp::patch())
.and(json_body::<Shop>())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::update_shop),
);
let list_shops_handler = warp::path("shops").and(
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env.clone()))
.and_then(handlers::list_shops),
);
let get_interior_ref_list_handler = warp::path("interior_ref_lists").and(
warp::path::param()
.and(warp::path::end())
.and(warp::get())
.and(with_env(env.clone()))
.and_then(handlers::get_interior_ref_list),
);
let create_interior_ref_list_handler = warp::path("interior_ref_lists").and(
warp::path::end()
.and(warp::post())
.and(json_body::<InteriorRefList>())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::create_interior_ref_list),
);
let delete_interior_ref_list_handler = warp::path("interior_ref_lists").and(
warp::path::param()
.and(warp::path::end())
.and(warp::delete())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::delete_interior_ref_list),
);
let list_interior_ref_lists_handler = warp::path("interior_ref_lists").and(
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env.clone()))
.and_then(handlers::list_interior_ref_lists),
);
let get_latest_interior_ref_list_by_shop_id_handler = warp::path("shops").and(
warp::path::param()
.and(warp::path("latest_interior_ref_list"))
.and(warp::path::end())
.and(warp::get())
.and(with_env(env.clone()))
.and_then(handlers::get_latest_interior_ref_list_by_shop_id),
);
let get_merchandise_list_handler = warp::path("merchandise_lists").and(
warp::path::param()
.and(warp::path::end())
.and(warp::get())
.and(with_env(env.clone()))
.and_then(handlers::get_merchandise_list),
);
let create_merchandise_list_handler = warp::path("merchandise_lists").and(
warp::path::end()
.and(warp::post())
.and(json_body::<MerchandiseList>())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::create_merchandise_list),
);
let delete_merchandise_list_handler = warp::path("merchandise_lists").and(
warp::path::param()
.and(warp::path::end())
.and(warp::delete())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::delete_merchandise_list),
);
let list_merchandise_lists_handler = warp::path("merchandise_lists").and(
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env.clone()))
.and_then(handlers::list_merchandise_lists),
);
let routes = warp::path("v1")
.and(balanced_or_tree!(
status_handler,
get_owner_handler,
delete_owner_handler,
update_owner_handler,
create_owner_handler,
list_owners_handler,
get_shop_handler,
delete_shop_handler,
update_shop_handler,
create_shop_handler,
list_shops_handler,
get_latest_interior_ref_list_by_shop_id_handler,
get_interior_ref_list_handler,
delete_interior_ref_list_handler,
create_interior_ref_list_handler,
list_interior_ref_lists_handler,
get_merchandise_list_handler,
delete_merchandise_list_handler,
create_merchandise_list_handler,
list_merchandise_lists_handler,
)) ))
.recover(problem::unpack_problem) .recover(problem::unpack_problem)
.with(warp::compression::gzip()) .with(warp::compression::gzip())

View File

@ -7,7 +7,6 @@ use tracing::instrument;
use super::ListParams; use super::ListParams;
use super::{Model, UpdateableModel}; use super::{Model, UpdateableModel};
use crate::models::InteriorRefList;
use crate::problem::forbidden_permission; use crate::problem::forbidden_permission;
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]