mirror of
https://gitea.farpn.net/w1cdn/farpn_udp_server.git
synced 2025-10-13 13:08:42 -05:00
Pass JSON into the database as fields.
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -109,6 +109,7 @@ dependencies = [
|
||||
"fallible-streaming-iterator",
|
||||
"hashlink",
|
||||
"libsqlite3-sys",
|
||||
"serde_json",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
|
@ -4,5 +4,5 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
rusqlite = "0.37.0"
|
||||
rusqlite = {version = "0.37.0", features = ["serde_json"]}
|
||||
serde_json = "1.0.145"
|
||||
|
33
src/main.rs
33
src/main.rs
@ -3,7 +3,6 @@ use rusqlite::{params, Connection, Result}; // Database operations and result ha
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use serde_json::{Value};
|
||||
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
|
||||
// Set up db and make sure it works
|
||||
@ -39,8 +38,12 @@ fn main() -> std::io::Result<()> {
|
||||
// look for type code in JSON here, only insert if data
|
||||
let p: Value = serde_json::from_str(p_raw)?;
|
||||
|
||||
//p.what_is_this;
|
||||
|
||||
//println!("{}",p["type"]);
|
||||
|
||||
if p["type"] != "status" {
|
||||
let _ = insert_packet(p_raw);
|
||||
let _ = insert_packet(p);
|
||||
}
|
||||
|
||||
// Echo the data back to the client
|
||||
@ -58,8 +61,11 @@ fn create_database() -> Result<()> {
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS packets (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
raw TEXT NOT NULL,
|
||||
date INTEGER NOT NULL
|
||||
date INTEGER NOT NULL,
|
||||
source TEXT,
|
||||
summary TEXT,
|
||||
final_destination TEXT,
|
||||
text TEXT
|
||||
)",
|
||||
[], // No parameters needed
|
||||
)?;
|
||||
@ -68,15 +74,28 @@ fn create_database() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_packet(raw: &str) -> Result<()> {
|
||||
fn insert_packet(p_json: serde_json::Value) -> Result<()> {
|
||||
let conn = Connection::open("pkt_database.db")?;
|
||||
|
||||
let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
|
||||
|
||||
//println!{"{}", p_json["type"]}
|
||||
|
||||
//println!("{}", p_json);
|
||||
//println!("{}", &p_json["source"]);
|
||||
|
||||
// Insert
|
||||
conn.execute(
|
||||
"INSERT INTO packets (raw, date) VALUES (?1, ?2)",
|
||||
params![raw, ds], // Bind parameters
|
||||
"INSERT INTO packets (date,
|
||||
source,
|
||||
summary,
|
||||
final_destination,
|
||||
text) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
params![ds,
|
||||
&p_json["source"],
|
||||
&p_json["summary"],
|
||||
&p_json["final_destination"],
|
||||
&p_json["text"]], // Bind parameters
|
||||
)?;
|
||||
|
||||
//println!("Row inserted successfully.");
|
||||
|
Reference in New Issue
Block a user