mirror of
https://gitea.farpn.net/w1cdn/farpn_udp_server.git
synced 2025-10-13 13:08:42 -05:00
Clean up db more.
This commit is contained in:
14
src/main.rs
14
src/main.rs
@ -5,8 +5,9 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
|
||||
// Set up db and make sure it works
|
||||
let _ = create_database();
|
||||
let _ = insert_packet("foo", 1);
|
||||
let _ = insert_packet("foo");
|
||||
|
||||
let socket_result: Result<UdpSocket, std::io::Error> = UdpSocket::bind("127.0.0.1:7878");
|
||||
let socket: UdpSocket = match socket_result {
|
||||
@ -32,7 +33,7 @@ fn main() -> std::io::Result<()> {
|
||||
String::from_utf8_lossy(&buffer[..bytes_received])
|
||||
);
|
||||
|
||||
let _ = insert_packet(&String::from_utf8_lossy(&buffer[..bytes_received]), 1);
|
||||
let _ = insert_packet(&String::from_utf8_lossy(&buffer[..bytes_received]));
|
||||
|
||||
// Echo the data back to the client
|
||||
socket.send_to(&buffer[..bytes_received], src_addr)?;
|
||||
@ -50,7 +51,6 @@ fn create_database() -> Result<()> {
|
||||
"CREATE TABLE IF NOT EXISTS packets (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
raw TEXT NOT NULL,
|
||||
age INTEGER NOT NULL,
|
||||
date INTEGER NOT NULL
|
||||
)",
|
||||
[], // No parameters needed
|
||||
@ -60,18 +60,18 @@ fn create_database() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_packet(raw: &str, age: i32) -> Result<()> {
|
||||
fn insert_packet(raw: &str) -> Result<()> {
|
||||
let conn = Connection::open("pkt_database.db")?;
|
||||
|
||||
let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
|
||||
|
||||
// Insert
|
||||
conn.execute(
|
||||
"INSERT INTO packets (raw, age, date) VALUES (?1, ?2, ?3)",
|
||||
params![raw, age, ds], // Bind parameters
|
||||
"INSERT INTO packets (raw, date) VALUES (?1, ?2)",
|
||||
params![raw, ds], // Bind parameters
|
||||
)?;
|
||||
|
||||
println!("Row inserted successfully.");
|
||||
//println!("Row inserted successfully.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user