From ce6e443c4159db1a0d77c8ad82a7607c102840af Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 22 May 2023 10:42:07 +0100 Subject: [PATCH] Created code to upload packets to a mongo instance. Pretty boring but just practising how to use the mongo driver --- Cargo.toml | 11 +++++++++++ src/main.rs | 35 +++++++++++++++++++++++++++++++++++ src/packet.rs | 7 +++++++ src/test.json | 19 +++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/packet.rs create mode 100644 src/test.json diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9088c33 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rustic_mongo" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +mongodb = "2.5.0" +serde = "1.0.163" +tokio = "1.28.1" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..112d487 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,35 @@ +use core::panic; +use mongodb::{Client, options::{ClientOptions, Credential}}; +mod packet; +use packet::Packet; +use std::time; + +#[tokio::main] +async fn main() { + // Parse a connection string into an options struct. + let mut client_options = ClientOptions::parse("mongodb://localhost:27017").await.unwrap(); + + // Manually set an option. + client_options.app_name = Some("My App".to_string()); + client_options.credential = Some(Credential::default()); + client_options.credential.as_mut().unwrap().username = Some(std::env::var("mongo_username").unwrap()); + client_options.credential.as_mut().unwrap().password = Some(std::env::var("mongo_password").unwrap()); + + // Get a handle to the deployment. + let client = Client::with_options(client_options).unwrap(); + + let db = client.database("test"); + + match db.create_collection("test", None).await { + Err(_) => panic!("ERROR: incorrect usage"), + _ => {} + } + let collection = db.collection::("test"); + + loop { + collection.insert_one(Packet{ + time: time::SystemTime::now(), + text: format!("Current Unix time: {:?}", time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap()) + }, None).await.unwrap(); + } +} \ No newline at end of file diff --git a/src/packet.rs b/src/packet.rs new file mode 100644 index 0000000..a3c6398 --- /dev/null +++ b/src/packet.rs @@ -0,0 +1,7 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Packet { + pub time: std::time::SystemTime, + pub text: String +} \ No newline at end of file diff --git a/src/test.json b/src/test.json new file mode 100644 index 0000000..4bfdb82 --- /dev/null +++ b/src/test.json @@ -0,0 +1,19 @@ +{ + "id": 1, + "first_name": "Tom", + "last_name": "Cruise", + "photo": "https://jsonformatter.org/img/tom-cruise.jpg" +}, +{ + "id": 2, + "first_name": "Maria", + "last_name": "Sharapova", + "photo": "https://jsonformatter.org/img/Maria-Sharapova.jpg + " +}, +{ + "id": 3, + "first_name": "Robert", + "last_name": "Downey Jr", + "photo": "https://jsonformatter.org/img/Robert-Downey-Jr.jpg" +} \ No newline at end of file