diff --git a/.env.dev b/.env.dev new file mode 100644 index 0000000..6b342b6 --- /dev/null +++ b/.env.dev @@ -0,0 +1,3 @@ +export GATEWAY_ORIGIN_STORE_GIT_DIR_PATH=/tmp/gateway_dev_env/origin/git +export GATEWAY_DOMAIN_CHECKER_TARGET_CNAME=gateway.example.com +export GATEWAY_DOMAIN_CONFIG_STORE_DIR_PATH=/tmp/gateway_dev_env/domain/config diff --git a/.envrc b/.envrc deleted file mode 100644 index 3550a30..0000000 --- a/.envrc +++ /dev/null @@ -1 +0,0 @@ -use flake diff --git a/Cargo.lock b/Cargo.lock index 33560bf..dd9f620 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,55 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is-terminal", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" + +[[package]] +name = "anstyle-parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +dependencies = [ + "anstyle", + "windows-sys 0.48.0", +] + [[package]] name = "arc-swap" version = "1.6.0" @@ -58,6 +107,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + [[package]] name = "base64" version = "0.21.0" @@ -112,6 +167,12 @@ version = "3.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "bytes" version = "1.4.0" @@ -136,12 +197,60 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34d21f9bf1b425d2968943631ec91202fe5e837264063503708b83013f8fc938" +dependencies = [ + "clap_builder", + "clap_derive", + "once_cell", +] + +[[package]] +name = "clap_builder" +version = "4.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "914c8c79fb560f238ef6429439a30023c862f7a28e688c58f7203f12b29970bd" +dependencies = [ + "anstream", + "anstyle", + "bitflags 1.3.2", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9644cd56d6b87dbe899ef8b053e331c0637664e9e21a33dfcdc36093f5c5c4" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.15", +] + +[[package]] +name = "clap_lex" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" + [[package]] name = "clru" version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8191fa7302e03607ff0e237d4246cc043ff5b3cb9409d995172ba3bea16b807" +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "cpufeatures" version = "0.2.7" @@ -392,6 +501,21 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +[[package]] +name = "futures" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.28" @@ -399,6 +523,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -407,12 +532,34 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" +[[package]] +name = "futures-executor" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" +[[package]] +name = "futures-macro" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "futures-sink" version = "0.3.28" @@ -431,8 +578,11 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ + "futures-channel", "futures-core", "futures-io", + "futures-macro", + "futures-sink", "futures-task", "memchr", "pin-project-lite", @@ -444,15 +594,21 @@ dependencies = [ name = "gateway" version = "0.1.0" dependencies = [ + "clap", + "futures", "gix", "hex", "mockall", "serde", "serde_json", "sha2", + "signal-hook", + "signal-hook-tokio", "tempdir", "thiserror", + "tokio", "trust-dns-client", + "warp", ] [[package]] @@ -997,7 +1153,7 @@ version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f01c2bf7b989c679695ef635fc7d9e80072e08101be4b53193c8e8b649900102" dependencies = [ - "base64", + "base64 0.21.0", "bstr", "gix-command", "gix-credentials", @@ -1107,6 +1263,31 @@ version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +[[package]] +name = "headers" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584" +dependencies = [ + "base64 0.13.1", + "bitflags 1.3.2", + "bytes", + "headers-core", + "http", + "httpdate", + "mime", + "sha1", +] + +[[package]] +name = "headers-core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +dependencies = [ + "http", +] + [[package]] name = "heck" version = "0.4.1" @@ -1320,6 +1501,18 @@ version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12b6ee2129af8d4fb011108c73d99a1b83a85977f23b82460c0ae2e25bb4b57f" +[[package]] +name = "is-terminal" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" +dependencies = [ + "hermit-abi 0.3.1", + "io-lifetimes", + "rustix", + "windows-sys 0.48.0", +] + [[package]] name = "itertools" version = "0.10.5" @@ -1468,6 +1661,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1522,6 +1725,24 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "multer" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01acbdc23469fd8fe07ab135923371d5f5a422fbf9c522158677c8eb15bc51c2" +dependencies = [ + "bytes", + "encoding_rs", + "futures-util", + "http", + "httparse", + "log", + "memchr", + "mime", + "spin 0.9.8", + "version_check", +] + [[package]] name = "nibble_vec" version = "0.1.0" @@ -1610,6 +1831,26 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" +[[package]] +name = "pin-project" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad29a609b6bcd67fee905812e544992d216af9d755757c05ed2d0e15a74c6ecc" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "pin-project-lite" version = "0.2.9" @@ -1847,7 +2088,7 @@ version = "0.11.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13293b639a097af28fc8a90f22add145a9c954e49d77da06263d58cf44d5fb91" dependencies = [ - "base64", + "base64 0.21.0", "bytes", "encoding_rs", "futures-core", @@ -1900,7 +2141,7 @@ dependencies = [ "cc", "libc", "once_cell", - "spin", + "spin 0.5.2", "untrusted", "web-sys", "winapi", @@ -1938,7 +2179,7 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d194b56d58803a43635bdc398cd17e383d6f71f9182b9a192c127ca42494a59b" dependencies = [ - "base64", + "base64 0.21.0", ] [[package]] @@ -1956,6 +2197,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + [[package]] name = "scopeguard" version = "1.1.0" @@ -2015,6 +2262,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha1" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sha1_smol" version = "1.0.0" @@ -2051,6 +2309,18 @@ dependencies = [ "libc", ] +[[package]] +name = "signal-hook-tokio" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "213241f76fb1e37e27de3b6aa1b068a2c333233b59cca6634f634b80a27ecf1e" +dependencies = [ + "futures-core", + "libc", + "signal-hook", + "tokio", +] + [[package]] name = "slab" version = "0.4.8" @@ -2082,12 +2352,24 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + [[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "1.0.109" @@ -2205,20 +2487,34 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.28.0" +version = "1.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c786bf8134e5a3a166db9b29ab8f48134739014a3eca7bc6bfa95d673b136f" +checksum = "0aa32867d44e6f2ce3385e89dceb990188b8bb0fb25b0cf576647a6f98ac5105" dependencies = [ "autocfg", "bytes", "libc", "mio", "num_cpus", + "parking_lot", "pin-project-lite", + "signal-hook-registry", "socket2", + "tokio-macros", "windows-sys 0.48.0", ] +[[package]] +name = "tokio-macros" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "tokio-rustls" version = "0.23.4" @@ -2230,6 +2526,29 @@ dependencies = [ "webpki", ] +[[package]] +name = "tokio-stream" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54319c93411147bced34cb5609a80e0a8e44c5999c93903a81cd866630ec0bfd" +dependencies = [ + "futures-util", + "log", + "tokio", + "tungstenite", +] + [[package]] name = "tokio-util" version = "0.7.8" @@ -2257,6 +2576,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ "cfg-if", + "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -2353,6 +2673,25 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" +[[package]] +name = "tungstenite" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30ee6ab729cd4cf0fd55218530c4522ed30b7b6081752839b68fcec8d0960788" +dependencies = [ + "base64 0.13.1", + "byteorder", + "bytes", + "http", + "httparse", + "log", + "rand 0.8.5", + "sha1", + "thiserror", + "url", + "utf-8", +] + [[package]] name = "typenum" version = "1.16.0" @@ -2368,6 +2707,15 @@ dependencies = [ "arrayvec", ] +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.13" @@ -2412,6 +2760,18 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version_check" version = "0.9.4" @@ -2438,6 +2798,37 @@ dependencies = [ "try-lock", ] +[[package]] +name = "warp" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba431ef570df1287f7f8b07e376491ad54f84d26ac473489427231e1718e1f69" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "headers", + "http", + "hyper", + "log", + "mime", + "mime_guess", + "multer", + "percent-encoding", + "pin-project", + "rustls-pemfile", + "scoped-tls", + "serde", + "serde_json", + "serde_urlencoded", + "tokio", + "tokio-stream", + "tokio-tungstenite", + "tokio-util", + "tower-service", + "tracing", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 9393b48..c30a821 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,9 @@ serde_json = "1.0.96" trust-dns-client = "0.22.0" mockall = "0.11.4" thiserror = "1.0.40" +tokio = { version = "1.28.1", features = [ "full" ]} +warp = "0.3.5" +signal-hook = "0.3.15" +futures = "0.3.28" +signal-hook-tokio = { version = "0.3.1", features = [ "futures-v0_3" ]} +clap = { version = "4.2.7", features = ["derive", "env"] } diff --git a/src/domain/checker.rs b/src/domain/checker.rs index b2592aa..05518d2 100644 --- a/src/domain/checker.rs +++ b/src/domain/checker.rs @@ -43,25 +43,23 @@ pub struct DNSChecker { client: SyncClient, } -impl DNSChecker { - pub fn new(target_cname: &str, resolver_addr: &str) -> Result { - let target_cname = - Name::from_str(target_cname).map_err(|_| NewDNSCheckerError::InvalidTargetCNAME)?; +pub fn new(target_cname: &str, resolver_addr: &str) -> Result { + let target_cname = + Name::from_str(target_cname).map_err(|_| NewDNSCheckerError::InvalidTargetCNAME)?; - let resolver_addr = resolver_addr - .parse() - .map_err(|_| NewDNSCheckerError::InvalidResolverAddress)?; + let resolver_addr = resolver_addr + .parse() + .map_err(|_| NewDNSCheckerError::InvalidResolverAddress)?; - let conn = UdpClientConnection::new(resolver_addr) - .map_err(|e| NewDNSCheckerError::Unexpected(Box::from(e)))?; + let conn = UdpClientConnection::new(resolver_addr) + .map_err(|e| NewDNSCheckerError::Unexpected(Box::from(e)))?; - let client = SyncClient::new(conn); + let client = SyncClient::new(conn); - Ok(DNSChecker { - target_cname, - client, - }) - } + Ok(DNSChecker { + target_cname, + client, + }) } impl Checker for DNSChecker { diff --git a/src/domain/config.rs b/src/domain/config.rs index a02f223..149af66 100644 --- a/src/domain/config.rs +++ b/src/domain/config.rs @@ -47,7 +47,7 @@ struct FSStore { dir_path: PathBuf, } -pub fn new_store(dir_path: &Path) -> io::Result { +pub fn new(dir_path: &Path) -> io::Result { fs::create_dir_all(dir_path)?; Ok(FSStore { dir_path: dir_path.into(), diff --git a/src/domain/manager.rs b/src/domain/manager.rs index 63d3a9b..2e297f6 100644 --- a/src/domain/manager.rs +++ b/src/domain/manager.rs @@ -131,7 +131,7 @@ pub trait Manager { ) -> Result<(), SyncWithConfigError>; } -pub fn new_manager( +pub fn new( origin_store: OriginStore, domain_config_store: DomainConfigStore, domain_checker: DomainChecker, diff --git a/src/lib.rs b/src/lib.rs index 4e6989c..174e28e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod domain; pub mod origin; +pub mod service; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5091d21 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,81 @@ +use clap::Parser; +use futures::stream::StreamExt; +use signal_hook::consts::signal; +use signal_hook_tokio::Signals; +use tokio::sync::oneshot; + +use std::path; + +#[derive(Parser, Debug)] +#[command(version)] +#[command(about = "A gateway to another dimension")] +struct Cli { + #[arg(long, required = true, env = "GATEWAY_ORIGIN_STORE_GIT_DIR_PATH")] + origin_store_git_dir_path: Option, + + #[arg(long, required = true, env = "GATEWAY_DOMAIN_CHECKER_TARGET_CNAME")] + domain_checker_target_cname: Option, + + #[arg(long, default_value_t = String::from("1.1.1.1:53"), env = "GATEWAY_DOMAIN_CHECKER_RESOLVER_ADDR")] + domain_checker_resolver_addr: String, + + #[arg(long, required = true, env = "GATEWAY_DOMAIN_CONFIG_STORE_DIR_PATH")] + domain_config_store_dir_path: Option, +} + +#[tokio::main] +async fn main() { + let config = Cli::parse(); + + // set up signal handling, stop_ch_rx will be used to signal that the stop signal has been + // received + let stop_ch_rx = { + let mut signals = Signals::new(&[signal::SIGTERM, signal::SIGINT, signal::SIGQUIT]) + .expect("initialized signals"); + + let (stop_ch_tx, stop_ch_rx) = oneshot::channel(); + + tokio::spawn(async move { + if let Some(_) = signals.next().await { + println!("Gracefully shutting down..."); + let _ = stop_ch_tx.send(()); + } + + if let Some(_) = signals.next().await { + println!("Forcefully shutting down"); + std::process::exit(1); + } + }); + + stop_ch_rx + }; + + let origin_store = gateway::origin::store::git::new(config.origin_store_git_dir_path.unwrap()) + .expect("git origin store initialized"); + + let domain_checker = gateway::domain::checker::new( + &config.domain_checker_target_cname.unwrap(), + &config.domain_checker_resolver_addr, + ) + .expect("domain checker initialized"); + + let domain_config_store = + gateway::domain::config::new(&config.domain_config_store_dir_path.unwrap()) + .expect("domain config store initialized"); + + let manager = gateway::domain::manager::new(origin_store, domain_config_store, domain_checker); + + let service = gateway::service::new(manager); + + let (addr, server) = + warp::serve(service).bind_with_graceful_shutdown(([127, 0, 0, 1], 3030), async { + stop_ch_rx.await.ok(); + }); + + println!("Listening on {addr}"); + tokio::task::spawn(server) + .await + .expect("server shutdown gracefully"); + + println!("Graceful shutdown complete"); +} diff --git a/src/origin/store/git.rs b/src/origin/store/git.rs index ee05e06..14f95c3 100644 --- a/src/origin/store/git.rs +++ b/src/origin/store/git.rs @@ -65,16 +65,16 @@ pub struct Store { origins: sync::RwLock>>, } -impl Store { - pub fn new(dir_path: PathBuf) -> io::Result { - fs::create_dir_all(&dir_path)?; - Ok(Store { - dir_path, - sync_guard: sync::Mutex::new(collections::HashMap::new()), - origins: sync::RwLock::new(collections::HashMap::new()), - }) - } +pub fn new(dir_path: PathBuf) -> io::Result { + fs::create_dir_all(&dir_path)?; + Ok(Store { + dir_path, + sync_guard: sync::Mutex::new(collections::HashMap::new()), + origins: sync::RwLock::new(collections::HashMap::new()), + }) +} +impl Store { fn repo_path(&self, descr: &origin::Descr) -> PathBuf { self.dir_path.join(descr.id()) } diff --git a/src/service.rs b/src/service.rs new file mode 100644 index 0000000..78cda89 --- /dev/null +++ b/src/service.rs @@ -0,0 +1,17 @@ +use warp::Filter; + +/* + * POST /domain/init (domain, config) -> token + * POST /domain/config (domain, config) + * GET /domain/config (domain) -> config + * GET /domains + */ + +pub fn new( + _manager: impl crate::domain::manager::Manager, +) -> impl warp::Filter + Clone { + warp::path("hello") + .and(warp::path("world")) + .and(warp::path::param()) + .map(|name: String| format!("Hello, {}!", name)) +}