Verified Commit 0685199f authored by Mathias B.'s avatar Mathias B.
Browse files

Utilisation de Actix pour faire le serveur RPC

parent 7d3bd93b
......@@ -2,6 +2,205 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "actix-codec"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d5dbeb2d9e51344cb83ca7cc170f1217f9fe25bfc50160e6e200b5c31c1019a"
dependencies = [
"bitflags",
"bytes 1.0.1",
"futures-core",
"futures-sink",
"log",
"pin-project-lite",
"tokio",
"tokio-util",
]
[[package]]
name = "actix-http"
version = "3.0.0-beta.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd38a862fa7fead2b47ee55e550982aba583ebc7365ccf0155b49934ad6f16f9"
dependencies = [
"actix-codec",
"actix-rt",
"actix-service",
"actix-tls",
"actix-utils",
"ahash",
"base64 0.13.0",
"bitflags",
"bytes 1.0.1",
"bytestring",
"derive_more",
"encoding_rs",
"futures-core",
"futures-util",
"h2",
"http",
"httparse",
"itoa",
"language-tags",
"local-channel",
"log",
"mime",
"once_cell",
"percent-encoding",
"pin-project",
"pin-project-lite",
"rand 0.8.4",
"regex",
"serde",
"sha-1",
"smallvec",
"time 0.2.27",
"tokio",
]
[[package]]
name = "actix-macros"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2f86cd6857c135e6e9fe57b1619a88d1f94a7df34c00e11fe13e64fd3438837"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "actix-router"
version = "0.5.0-beta.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36b95ce0d76d1aa2f98b681702807475ade0f99bd4552546a6843a966d42ea3d"
dependencies = [
"bytestring",
"firestorm",
"http",
"log",
"regex",
"serde",
]
[[package]]
name = "actix-rt"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc7d7cd957c9ed92288a7c3c96af81fa5291f65247a76a34dac7b6af74e52ba0"
dependencies = [
"actix-macros",
"futures-core",
"tokio",
]
[[package]]
name = "actix-server"
version = "2.0.0-beta.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26369215fcc3b0176018b3b68756a8bcc275bb000e6212e454944913a1f9bf87"
dependencies = [
"actix-rt",
"actix-service",
"actix-utils",
"futures-core",
"log",
"mio",
"num_cpus",
"slab",
"tokio",
]
[[package]]
name = "actix-service"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77f5f9d66a8730d0fae62c26f3424f5751e5518086628a40b7ab6fca4a705034"
dependencies = [
"futures-core",
"paste",
"pin-project-lite",
]
[[package]]
name = "actix-tls"
version = "3.0.0-beta.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65b7bb60840962ef0332f7ea01a57d73a24d2cb663708511ff800250bbfef569"
dependencies = [
"actix-codec",
"actix-rt",
"actix-service",
"actix-utils",
"derive_more",
"futures-core",
"http",
"log",
"tokio-util",
]
[[package]]
name = "actix-utils"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e491cbaac2e7fc788dfff99ff48ef317e23b3cf63dbaf7aaab6418f40f92aa94"
dependencies = [
"local-waker",
"pin-project-lite",
]
[[package]]
name = "actix-web"
version = "4.0.0-beta.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34aa2b23ec9c7c9a799b3cf9258f67c91b18ac3f0f5f484e175c7ac46739bb5"
dependencies = [
"actix-codec",
"actix-http",
"actix-macros",
"actix-router",
"actix-rt",
"actix-server",
"actix-service",
"actix-utils",
"actix-web-codegen",
"ahash",
"bytes 1.0.1",
"cfg-if",
"derive_more",
"either",
"encoding_rs",
"futures-core",
"futures-util",
"itoa",
"language-tags",
"log",
"mime",
"once_cell",
"paste",
"pin-project",
"regex",
"serde",
"serde_json",
"serde_urlencoded",
"smallvec",
"socket2",
"time 0.2.27",
"url",
]
[[package]]
name = "actix-web-codegen"
version = "0.5.0-beta.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a11fd6f322120a74b23327e778ef0a4950b1f44a2b76468a69316a150f5c6dd"
dependencies = [
"actix-router",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "addr2line"
version = "0.16.0"
......@@ -27,6 +226,17 @@ dependencies = [
"rand_core 0.6.3",
]
[[package]]
name = "ahash"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43bb833f0bf979d8475d38fbf09ed3b8a55e1885fe93ad3f93239fc6a4f17b98"
dependencies = [
"getrandom 0.2.3",
"once_cell",
"version_check",
]
[[package]]
name = "aho-corasick"
version = "0.7.18"
......@@ -45,12 +255,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "anyhow"
version = "1.0.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1"
[[package]]
name = "async-trait"
version = "0.1.51"
......@@ -119,15 +323,6 @@ version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "bincode"
version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
dependencies = [
"serde",
]
[[package]]
name = "bitflags"
version = "1.2.1"
......@@ -143,12 +338,6 @@ dependencies = [
"generic-array",
]
[[package]]
name = "borrow-bag"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22a81f981b8ef527251cf84b62c73331637431d5d34820d7f166ed28ea97afd6"
[[package]]
name = "bot_attr"
version = "0.1.0"
......@@ -183,6 +372,15 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040"
[[package]]
name = "bytestring"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90706ba19e97b90786e19dc0d5e2abd80008d99d4c0c5d1ad0b5e72cec7c494d"
dependencies = [
"bytes 1.0.1",
]
[[package]]
name = "cc"
version = "1.0.69"
......@@ -261,14 +459,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f92cfa0fd5690b3cf8c1ef2cabbd9b7ef22fa53cf5e1f92b05103f6d5d1cf6e7"
[[package]]
name = "cookie"
version = "0.15.1"
name = "convert_case"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5f1c7727e460397e56abc4bddc1d49e07a1ad78fc98eb2e1c8f032a58a2f80d"
dependencies = [
"time 0.2.27",
"version_check",
]
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
[[package]]
name = "core-foundation"
......@@ -338,6 +532,19 @@ dependencies = [
"translation",
]
[[package]]
name = "derive_more"
version = "0.99.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40eebddd2156ce1bb37b20bbe5151340a31828b1f2d22ba4141f3531710e38df"
dependencies = [
"convert_case",
"proc-macro2",
"quote",
"rustc_version 0.3.3",
"syn",
]
[[package]]
name = "digest"
version = "0.9.0"
......@@ -357,6 +564,7 @@ checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0"
name = "discord-bot-rs"
version = "0.1.0"
dependencies = [
"actix-web",
"bot_attr",
"chacha20poly1305",
"chrono",
......@@ -365,12 +573,9 @@ dependencies = [
"dotenv",
"eyre",
"futures",
"gotham",
"gotham_derive",
"itertools 0.10.1",
"listing-apis",
"markov",
"mime",
"once_cell",
"procinfo",
"rand 0.8.4",
......@@ -429,6 +634,12 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "firestorm"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31586bda1b136406162e381a3185a506cdfc1631708dd40cba2f6628d8634499"
[[package]]
name = "fixedbitset"
version = "0.2.0"
......@@ -619,50 +830,6 @@ version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7"
[[package]]
name = "gotham"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52aeb8c70641bedbf18466e0537af9aff917a23468004b5ecca210116a2bfbbc"
dependencies = [
"anyhow",
"base64 0.13.0",
"bincode",
"borrow-bag",
"bytes 1.0.1",
"chrono",
"cookie",
"futures",
"http",
"httpdate 0.3.2",
"hyper",
"itertools 0.10.1",
"linked-hash-map",
"log",
"mime",
"mime_guess",
"num_cpus",
"percent-encoding",
"pin-project",
"rand 0.8.4",
"rand_chacha 0.3.1",
"regex",
"serde",
"serde_derive",
"tokio",
"uuid",
]
[[package]]
name = "gotham_derive"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "759a0e44326c652a7535f1ded7a9df63238e2dd601def67f6ca940d04af3a547"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "h2"
version = "0.3.3"
......@@ -731,15 +898,9 @@ dependencies = [
[[package]]
name = "httparse"
version = "1.4.1"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3a87b616e37e93c22fb19bcd386f02f3af5ea98a25670ad0fce773de23c5e68"
[[package]]
name = "httpdate"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503"
[[package]]
name = "httpdate"
......@@ -761,7 +922,7 @@ dependencies = [
"http",
"http-body",
"httparse",
"httpdate 1.0.1",
"httpdate",
"itoa",
"pin-project-lite",
"socket2",
......@@ -883,6 +1044,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "language-tags"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388"
[[package]]
name = "lazy_static"
version = "1.4.0"
......@@ -909,6 +1076,24 @@ dependencies = [
"serde_json",
]
[[package]]
name = "local-channel"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6246c68cf195087205a0512559c97e15eaf95198bf0e206d662092cdcb03fe9f"
dependencies = [
"futures-core",
"futures-sink",
"futures-util",
"local-waker",
]
[[package]]
name = "local-waker"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84f9a2d3e27ce99ce2c3aad0b09b1a7b916293ea9b2bf624c13fe646fadd8da4"
[[package]]
name = "lock_api"
version = "0.4.4"
......@@ -1157,12 +1342,27 @@ dependencies = [
"winapi",
]
[[package]]
name = "paste"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58"
[[package]]
name = "percent-encoding"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "pest"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53"
dependencies = [
"ucd-trie",
]
[[package]]
name = "petgraph"
version = "0.5.1"
......@@ -1505,7 +1705,16 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
"semver",
"semver 0.9.0",
]
[[package]]
name = "rustc_version"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee"
dependencies = [
"semver 0.11.0",
]
[[package]]
......@@ -1582,7 +1791,16 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [
"semver-parser",
"semver-parser 0.7.0",
]
[[package]]
name = "semver"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6"
dependencies = [
"semver-parser 0.10.2",
]
[[package]]
......@@ -1591,6 +1809,15 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "semver-parser"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7"
dependencies = [
"pest",
]
[[package]]
name = "serde"
version = "1.0.126"
......@@ -1714,6 +1941,15 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
dependencies = [
"libc",
]
[[package]]
name = "siphasher"
version = "0.3.6"
......@@ -1781,7 +2017,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d022496b16281348b52d0e30ae99e01a73d737b2f45d38fed4edf79f9325a1d5"
dependencies = [
"discard",
"rustc_version",
"rustc_version 0.2.3",
"stdweb-derive",
"stdweb-internal-macros",
"stdweb-internal-runtime",
......@@ -1949,7 +2185,10 @@ dependencies = [
"memchr",
"mio",
"num_cpus",
"once_cell",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"tokio-macros",
"winapi",
]
......@@ -2152,6 +2391,12 @@ version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06"
[[package]]
name = "ucd-trie"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
[[package]]
name = "unicase"
version = "2.6.0"
......@@ -2225,15 +2470,6 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "uuid"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom 0.2.3",
]
[[package]]
name = "uwl"
version = "0.6.0"
......
......@@ -43,9 +43,7 @@ eyre = "0.6.5"
stable-eyre = "0.2.2"
# RPC
gotham = { version = "0.6.0", default-features = false }
gotham_derive = "0.6.0"
mime = "0.3.16"
actix-web = { version = "4.0.0-beta.9", default-features = false }
datastructs = { path = "../datastructs" }
cryptoutils = { path = "../cryptoutils" }
......
......@@ -47,7 +47,7 @@ impl TypeMapKey for TopGgContainer {
static BOT_PREFIX: Lazy<String> =
Lazy::new(|| std::env::var("BOT_PREFIX").expect("Expected a BOT_PREFIX in the environment"));
#[tokio::main]
#[actix_web::main]
async fn main() -> eyre::Result<()> {
stable_eyre::install()?;
tracing_subscriber::fmt()
......@@ -138,12 +138,15 @@ async fn main() -> eyre::Result<()> {
}
// S'occuper du serveur le temps d'un instant
{
let http = client.cache_and_http.clone();
rpc_server::initialize_client(http);
let router = make_router();
tokio::spawn(gotham::init_server("127.0.0.1:9000", router));
}
rpc_server::initialize_client(client.cache_and_http.clone());
let server = rpc_server::prepare_server();
let server_task = tokio::spawn(async move {
if let Err(e) = server.await {
tracing::error!("Error while running server: {:?}", e);
}
});
info!("Starting client");
......
mod ping;
use std::sync::Arc;
use gotham::helpers::http::response::create_response;
use gotham::hyper::StatusCode;
use gotham::state::State;
use gotham::router::Router;
use gotham::router::builder::{build_router, DrawRoutes, DefineSingleRoute};
use gotham::pipeline::new_pipeline;
use gotham::pipeline::single::single_pipeline;
use actix_web::{HttpServer, App};
use actix_web::dev::Server;
use once_cell::sync::OnceCell;
use serenity::CacheAndHttp;
use std::sync::Arc;
static CLIENT_PART: OnceCell<Arc<CacheAndHttp>> = OnceCell::new();
......@@ -32,31 +26,18 @@ impl DiscordClient {
}
}
// ---
async fn hello(state: State) -> gotham::handler::HandlerResult {
let client = DiscordClient::get();
let user = client.cache.user(88644904112128000).await.unwrap();
let response = create_response(
&state,
StatusCode::OK,