diff options
Diffstat (limited to 'common/rust')
| -rw-r--r-- | common/rust/BUILD.bazel | 10 | ||||
| -rw-r--r-- | common/rust/Cargo.toml | 23 | ||||
| -rw-r--r-- | common/rust/cargo/BUILD.bazel | 139 | ||||
| -rw-r--r-- | common/rust/src/config.rs | 58 | ||||
| -rw-r--r-- | common/rust/src/error.rs | 18 | ||||
| -rw-r--r-- | common/rust/src/lib.rs | 18 | ||||
| -rw-r--r-- | common/rust/src/monitoring.rs | 61 | ||||
| -rw-r--r-- | common/rust/src/nats.rs | 63 | ||||
| -rw-r--r-- | common/rust/src/payloads.rs | 23 | ||||
| -rw-r--r-- | common/rust/src/redis.rs | 15 | ||||
| -rw-r--r-- | common/rust/src/serializable_event.rs | 165 |
11 files changed, 0 insertions, 593 deletions
diff --git a/common/rust/BUILD.bazel b/common/rust/BUILD.bazel deleted file mode 100644 index a1bbb4e..0000000 --- a/common/rust/BUILD.bazel +++ /dev/null @@ -1,10 +0,0 @@ -load("@rules_rust//rust:defs.bzl", "rust_library") -load("//cargo:crates.bzl", "all_crate_deps", "crate_deps") - -rust_library( - name = "common", - srcs = glob(["src/**"]), - proc_macro_deps = crate_deps(["serde_repr"]), - visibility = ["//visibility:public"], - deps = all_crate_deps(), -) diff --git a/common/rust/Cargo.toml b/common/rust/Cargo.toml deleted file mode 100644 index 07f9dd3..0000000 --- a/common/rust/Cargo.toml +++ /dev/null @@ -1,23 +0,0 @@ -[package] -name = "common" -version = "0.1.0" -edition = "2018" - -[dependencies] -pretty_env_logger = "0.4" -log = { version = "0.4", features = ["std"] } -serde = { version = "1.0.8", features = ["derive"] } -serde_repr = "0.1" -config = "0.11" -hyper = { version = "0.14", features = ["full"] } -tokio = { version = "1", features = ["full"] } -enumflags2 = { version = "0.7.1", features = ["serde"] } -prometheus = { version = "0.12.0", features = ["process"] } -nats = "0.15.2" -testcontainers = "0.12.0" -twilight-model = { version = "0.6" } -serde_json = { version = "1.0" } - -[dependencies.redis] -version = "*" -features = ["cluster", "connection-manager", "tokio-comp"] diff --git a/common/rust/cargo/BUILD.bazel b/common/rust/cargo/BUILD.bazel deleted file mode 100644 index 387da21..0000000 --- a/common/rust/cargo/BUILD.bazel +++ /dev/null @@ -1,139 +0,0 @@ -""" -@generated -cargo-raze generated Bazel file. - -DO NOT EDIT! Replaced on runs of cargo-raze -""" - -package(default_visibility = ["//visibility:public"]) - -licenses([ - "notice", # See individual crates for specific licenses -]) - -# Aliased targets -alias( - name = "config", - actual = "@raze__config__0_11_0//:config", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "enumflags2", - actual = "@raze__enumflags2__0_7_1//:enumflags2", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "hyper", - actual = "@raze__hyper__0_14_13//:hyper", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "log", - actual = "@raze__log__0_4_14//:log", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "nats", - actual = "@raze__nats__0_15_2//:nats", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "pretty_env_logger", - actual = "@raze__pretty_env_logger__0_4_0//:pretty_env_logger", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "prometheus", - actual = "@raze__prometheus__0_12_0//:prometheus", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "redis", - actual = "@raze__redis__0_21_3//:redis", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "serde", - actual = "@raze__serde__1_0_130//:serde", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "serde_json", - actual = "@raze__serde_json__1_0_68//:serde_json", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "serde_repr", - actual = "@raze__serde_repr__0_1_7//:serde_repr", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "testcontainers", - actual = "@raze__testcontainers__0_12_0//:testcontainers", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "tokio", - actual = "@raze__tokio__1_12_0//:tokio", - tags = [ - "cargo-raze", - "manual", - ], -) - -alias( - name = "twilight_model", - actual = "@raze__twilight_model__0_6_5//:twilight_model", - tags = [ - "cargo-raze", - "manual", - ], -) diff --git a/common/rust/src/config.rs b/common/rust/src/config.rs deleted file mode 100644 index 327f6f8..0000000 --- a/common/rust/src/config.rs +++ /dev/null @@ -1,58 +0,0 @@ -use std::env; -use config::{Config, ConfigError, Environment, File}; -use log::info; -use serde::Deserialize; - -/// Settings<T> is the base structure for all the nova's component config -/// you can specify a type T and the name of the component. the "config" -/// field will be equals to the key named after the given component name -/// and will be of type T -#[derive(Debug, Deserialize, Clone)] -#[serde(bound(deserialize = "T: Deserialize<'de> + std::default::Default + Clone"))] -pub struct Settings<T> { - #[serde(skip_deserializing)] - pub config: T, - pub monitoring: crate::monitoring::MonitoringConfiguration, - pub nats: crate::nats::NatsConfiguration, - pub redis: crate::redis::RedisConfiguration, -} - -/// -impl<T> Settings<T> -where - T: Deserialize<'static> + std::default::Default + Clone, -{ - - /// Initializes a new configuration like the other components of nova - /// And starts the prometheus metrics server if needed. - pub fn new(service_name: &str) -> Result<Settings<T>, ConfigError> { - pretty_env_logger::init(); - - let mut default = Config::default(); - // this file my be shared with all the components - default.merge(File::with_name("config/default"))?; - let mode = env::var("ENV").unwrap_or_else(|_| "development".into()); - info!("Configuration Environment: {}", mode); - - default.merge(File::with_name(&format!("config/{}", mode)).required(false))?; - default.merge(File::with_name("config/local").required(false))?; - - let env = Environment::with_prefix("NOVA").separator("__"); - // we can configure each component using environment variables - default.merge(env)?; - let mut config: Settings<T> = default.clone().try_into().unwrap(); - - // try to load the config - config.config = default.get::<T>(service_name).unwrap(); - // todo(MatthieuCodder): the following line was not present in the conflict - // pretty_env_logger::init(); - - // start the monitoring system if needed - crate::monitoring::start_monitoring(&config.monitoring); - Ok(config) - } -} - -pub fn test_init() { - pretty_env_logger::init(); -} diff --git a/common/rust/src/error.rs b/common/rust/src/error.rs deleted file mode 100644 index 1a24657..0000000 --- a/common/rust/src/error.rs +++ /dev/null @@ -1,18 +0,0 @@ -use std::fmt; - -#[derive(Debug)] -pub struct NovaError { - pub message: String, -} - -impl fmt::Display for NovaError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "An error occurred within the nova system: {}", self.message) // user-facing output - } -} - -impl From<&str> for NovaError { - fn from(message: &str) -> Self { - NovaError { message: message.to_string() } - } -}
\ No newline at end of file diff --git a/common/rust/src/lib.rs b/common/rust/src/lib.rs deleted file mode 100644 index cbe6934..0000000 --- a/common/rust/src/lib.rs +++ /dev/null @@ -1,18 +0,0 @@ -pub use ::config as config_crate; -pub use ::nats as nats_crate; -pub use ::redis as redis_crate; -pub use log; -pub use prometheus; -pub use serde; -pub use testcontainers; - -/// This crate is all the utilities shared by the nova rust projects -/// It includes logging, config and protocols. -pub mod config; -pub mod error; -pub mod monitoring; -pub mod nats; -pub mod payloads; -pub mod redis; - -mod serializable_event; diff --git a/common/rust/src/monitoring.rs b/common/rust/src/monitoring.rs deleted file mode 100644 index 4bff043..0000000 --- a/common/rust/src/monitoring.rs +++ /dev/null @@ -1,61 +0,0 @@ -use hyper::{ - header::CONTENT_TYPE, - service::{make_service_fn, service_fn}, - Body, Request, Response, Server, -}; -use log::{error, info}; -use prometheus::{Encoder, TextEncoder}; -use serde::Deserialize; -use std::net::ToSocketAddrs; - -#[derive(Clone, Debug, Deserialize)] -/// Options for the monitoring service -pub struct MonitoringConfiguration { - pub enabled: bool, - pub address: Option<String>, - pub port: Option<i32>, -} - -/// Handler for the hyper http server -async fn serve_metrics(_request: Request<Body>) -> Result<Response<Body>, hyper::Error> { - let encoder = TextEncoder::new(); - let metrics = prometheus::gather(); - - let mut buffer = vec![]; - encoder.encode(&metrics, &mut buffer).unwrap(); - - let response = Response::builder() - .status(200) - .header(CONTENT_TYPE, encoder.format_type()) - .body(Body::from(buffer)) - .unwrap(); - Ok(response) -} - -/// Starts a monitoring server on the requested port -pub fn start_monitoring(configuration: &MonitoringConfiguration) { - let config = configuration.clone(); - tokio::task::spawn(async move { - if config.enabled { - let address = format!( - "{}:{}", - config - .address - .expect("a listening address must be specified for the metrics server"), - config - .port - .expect("a listening port must be specified for the metrics server") - ); - info!("Starting monitoring server on {}", address); - - let listen_address = address.to_socket_addrs().unwrap().next().unwrap(); - let server = Server::bind(&listen_address).serve(make_service_fn(|_| async { - Ok::<_, hyper::Error>(service_fn(serve_metrics)) - })); - - if let Err(e) = server.await { - error!("failed to start the monitoring server {}", e); - } - } - }); -} diff --git a/common/rust/src/nats.rs b/common/rust/src/nats.rs deleted file mode 100644 index a001dc9..0000000 --- a/common/rust/src/nats.rs +++ /dev/null @@ -1,63 +0,0 @@ -use nats::{Connection, Options}; -use serde::Deserialize; - -#[derive(Clone, Debug, Deserialize)] -pub struct NatsConfigurationClientCert { - pub cert: String, - pub key: String, -} - -#[derive(Clone, Debug, Deserialize)] -pub struct NatsConfigurationTls { - pub mtu: Option<usize>, -} - -#[derive(Clone, Debug, Deserialize)] -pub struct NatsConfiguration { - pub client_cert: Option<NatsConfigurationClientCert>, - pub root_cert: Option<Vec<String>>, - pub jetstream_api_prefix: Option<String>, - pub max_reconnects: Option<usize>, - pub reconnect_buffer_size: Option<usize>, - pub tls: Option<NatsConfigurationTls>, - pub client_name: Option<String>, - pub tls_required: Option<bool>, - pub host: String, -} - -// todo: Prefer From since it automatically gives a free Into implementation -// Allows the configuration to directly create a nats connection -impl Into<Connection> for NatsConfiguration { - fn into(self) -> Connection { - let mut options = Options::new(); - - if let Some(client_cert) = self.client_cert { - options = options.client_cert(client_cert.cert, client_cert.key); - } - - if let Some(root_certs) = self.root_cert { - for root_cert in root_certs { - options = options.add_root_certificate(root_cert); - } - } - - if let Some(jetstream_api_prefix) = self.jetstream_api_prefix { - options = options.jetstream_api_prefix(jetstream_api_prefix) - } - - options = options.max_reconnects(self.max_reconnects); - options = options.no_echo(); - options = options.reconnect_buffer_size(self.reconnect_buffer_size.unwrap_or(64 * 1024)); - options = options.tls_required(self.tls_required.unwrap_or(false)); - options = options.with_name(&self.client_name.unwrap_or_else(|| "Nova".to_string())); - - if let Some(tls) = self.tls { - let mut config = nats::rustls::ClientConfig::new(); - config.set_mtu(&tls.mtu); - // todo: more options? - options = options.tls_client_config(config); - } - - options.connect(&self.host).unwrap() - } -} diff --git a/common/rust/src/payloads.rs b/common/rust/src/payloads.rs deleted file mode 100644 index 454baf1..0000000 --- a/common/rust/src/payloads.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::fmt::Debug; - -use serde::{Deserialize, Serialize}; -use twilight_model::gateway::event::Event; -use crate::serializable_event::SerializableEvent; - -#[derive(Deserialize, Serialize, Debug, Clone)] -pub struct SerializeHelper(#[serde(with = "SerializableEvent")] pub Event); - -/// Payload send to the nova cache queues -#[derive(Serialize, Deserialize, Debug, Clone)] -// #[serde(bound(deserialize = "T: Deserialize<'de> + std::default::Default + Clone"))] -pub struct CachePayload { - pub tracing: Tracing, - #[serde(flatten)] - pub data: SerializeHelper, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Tracing { - pub node_id: String, - pub span: Option<String>, -} diff --git a/common/rust/src/redis.rs b/common/rust/src/redis.rs deleted file mode 100644 index a196f8d..0000000 --- a/common/rust/src/redis.rs +++ /dev/null @@ -1,15 +0,0 @@ -use redis::Client; -use serde::Deserialize; - - -#[derive(Clone, Debug, Deserialize)] -pub struct RedisConfiguration { - pub url: String, -} - -// Allows the configuration to directly create a nats connection -impl Into<Client> for RedisConfiguration { - fn into(self) -> Client { - redis::Client::open(self.url).unwrap() - } -} diff --git a/common/rust/src/serializable_event.rs b/common/rust/src/serializable_event.rs deleted file mode 100644 index 269b0aa..0000000 --- a/common/rust/src/serializable_event.rs +++ /dev/null @@ -1,165 +0,0 @@ -use serde::{Deserialize, Serialize}; -use twilight_model::gateway::{ - event::{ - shard::{ - Connected, Connecting, Disconnected, Identifying, Payload, Reconnecting, Resuming, - }, - Event, - }, - payload::{ - BanAdd, BanRemove, ChannelCreate, ChannelDelete, ChannelPinsUpdate, ChannelUpdate, - GuildCreate, GuildDelete, GuildEmojisUpdate, GuildIntegrationsUpdate, GuildUpdate, - IntegrationCreate, IntegrationDelete, IntegrationUpdate, InteractionCreate, InviteCreate, - InviteDelete, MemberAdd, MemberChunk, MemberRemove, MemberUpdate, MessageCreate, - MessageDelete, MessageDeleteBulk, MessageUpdate, PresenceUpdate, ReactionAdd, - ReactionRemove, ReactionRemoveAll, ReactionRemoveEmoji, Ready, RoleCreate, RoleDelete, - RoleUpdate, StageInstanceCreate, StageInstanceDelete, StageInstanceUpdate, ThreadCreate, - ThreadDelete, ThreadListSync, ThreadMemberUpdate, ThreadMembersUpdate, ThreadUpdate, - TypingStart, UnavailableGuild, UserUpdate, VoiceServerUpdate, VoiceStateUpdate, - WebhooksUpdate, - }, -}; - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(remote = "Event")] -#[serde(rename_all = "snake_case")] -#[serde(tag = "t", content = "c")] -pub enum SerializableEvent { - /// A user was banned from a guild. - BanAdd(BanAdd), - /// A user's ban from a guild was removed. - BanRemove(BanRemove), - /// A channel was created. - ChannelCreate(ChannelCreate), - /// A channel was deleted. - ChannelDelete(ChannelDelete), - /// A channel's pins were updated. - ChannelPinsUpdate(ChannelPinsUpdate), - /// A channel was updated. - ChannelUpdate(ChannelUpdate), - /// A heartbeat was sent to or received from the gateway. - GatewayHeartbeat(u64), - /// A heartbeat acknowledgement was received from the gateway. - GatewayHeartbeatAck, - /// A "hello" packet was received from the gateway. - GatewayHello(u64), - /// A shard's session was invalidated. - /// - /// `true` if resumeable. If not, then the shard must do a full reconnect. - GatewayInvalidateSession(bool), - /// The gateway is indicating to perform a reconnect. - GatewayReconnect, - /// Undocumented event, should be ignored - GiftCodeUpdate, - /// A guild was created. - GuildCreate(Box<GuildCreate>), - /// A guild was deleted or the current user was removed from a guild. - GuildDelete(Box<GuildDelete>), - /// A guild's emojis were updated. - GuildEmojisUpdate(GuildEmojisUpdate), - /// A guild's integrations were updated. - GuildIntegrationsUpdate(GuildIntegrationsUpdate), - /// A guild was updated. - GuildUpdate(Box<GuildUpdate>), - /// A guild integration was created. - IntegrationCreate(Box<IntegrationCreate>), - /// A guild integration was updated. - IntegrationDelete(IntegrationDelete), - /// A guild integration was deleted. - IntegrationUpdate(Box<IntegrationUpdate>), - /// An interaction was invoked by a user. - InteractionCreate(Box<InteractionCreate>), - /// A invite was made. - InviteCreate(Box<InviteCreate>), - /// A invite was deleted. - InviteDelete(InviteDelete), - /// A user was added to a guild. - MemberAdd(Box<MemberAdd>), - /// A user was removed from a guild. - MemberRemove(MemberRemove), - /// A user's member object in a guild was updated. - MemberUpdate(Box<MemberUpdate>), - /// A chunk of members were received from the gateway. - MemberChunk(MemberChunk), - /// A message was created in a channel. - MessageCreate(Box<MessageCreate>), - /// A message was deleted in a channel. - MessageDelete(MessageDelete), - /// Multiple messages were deleted in a channel. - MessageDeleteBulk(MessageDeleteBulk), - /// A message was updated in a channel. - MessageUpdate(Box<MessageUpdate>), - /// A user's active presence (such as game or online status) was updated. - PresenceUpdate(Box<PresenceUpdate>), - /// Multiple presences outside of a guild were updated. - /// - /// For bots this is always empty and useless. - PresencesReplace, - /// A reaction was added to a message. - ReactionAdd(Box<ReactionAdd>), - /// A reaction was removed from a message. - ReactionRemove(Box<ReactionRemove>), - /// All reactions were removed from a message. - ReactionRemoveAll(ReactionRemoveAll), - /// All instances of a given emoji from the reactions of a message were - /// removed. - ReactionRemoveEmoji(ReactionRemoveEmoji), - /// A shard is now "ready" and fully connected. - Ready(Box<Ready>), - /// A shard has successfully resumed. - Resumed, - /// A role was created in a guild. - RoleCreate(RoleCreate), - /// A role was deleted in a guild. - RoleDelete(RoleDelete), - /// A role was updated in a guild. - RoleUpdate(RoleUpdate), - /// A shard is now in a connected stage after being fully connected to the - /// gateway. - ShardConnected(Connected), - /// A shard is now in a connecting stage after starting to connect to the - /// gateway. - ShardConnecting(Connecting), - /// A shard is now in a disconnected stage after the connection was closed. - ShardDisconnected(Disconnected), - /// A shard is now in a identifying stage after starting a new session. - ShardIdentifying(Identifying), - /// A shard is now in a reconnecting stage after a disconnect or session was - /// ended. - ShardReconnecting(Reconnecting), - /// A payload of bytes came in through the shard's connection. - ShardPayload(Payload), - /// A shard is now in a Resuming stage after a disconnect. - ShardResuming(Resuming), - /// A stage instance was created in a stage channel. - StageInstanceCreate(StageInstanceCreate), - /// A stage instance was deleted in a stage channel. - StageInstanceDelete(StageInstanceDelete), - /// A stage instance was updated in a stage channel. - StageInstanceUpdate(StageInstanceUpdate), - /// A thread has been created, relevant to the current user, - /// or the current user has been added to a thread. - ThreadCreate(ThreadCreate), - /// A thread, relevant to the current user, has been deleted. - ThreadDelete(ThreadDelete), - /// The current user has gained access to a thread. - ThreadListSync(ThreadListSync), - /// The thread member object for the current user has been updated. - ThreadMemberUpdate(ThreadMemberUpdate), - /// A user has been added to or removed from a thread. - ThreadMembersUpdate(ThreadMembersUpdate), - /// A thread has been updated. - ThreadUpdate(ThreadUpdate), - /// A user started typing in a channel. - TypingStart(Box<TypingStart>), - /// A guild is now unavailable. - UnavailableGuild(UnavailableGuild), - /// The current user was updated. - UserUpdate(UserUpdate), - /// A voice server update was sent. - VoiceServerUpdate(VoiceServerUpdate), - /// A voice state in a voice channel was updated. - VoiceStateUpdate(Box<VoiceStateUpdate>), - /// A webhook was updated. - WebhooksUpdate(WebhooksUpdate), -} |
