remove async and fix queries - inserts working

master
lalanza808 4 years ago
parent ceeb329fd1
commit b87fafc9ed

104
Cargo.lock generated

@ -1,11 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "arc-swap"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d663a8e9a99154b5fb793032533f6328da35e23aac63d5c152279aa8ba356825"
[[package]]
name = "async-trait"
version = "0.1.24"
@ -82,11 +76,9 @@ dependencies = [
name = "bones"
version = "0.0.1"
dependencies = [
"http",
"irc",
"openssl",
"tokio 0.2.13",
"tokio-postgres",
"postgres",
]
[[package]]
@ -508,17 +500,6 @@ dependencies = [
"digest",
]
[[package]]
name = "http"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b708cc7f06493459026f53b9a61a7a121a5d1ec6238dee58ea4941132b30156b"
dependencies = [
"bytes 0.5.4",
"fnv",
"itoa",
]
[[package]]
name = "iovec"
version = "0.1.4"
@ -553,12 +534,6 @@ dependencies = [
"toml",
]
[[package]]
name = "itoa"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
@ -645,24 +620,12 @@ dependencies = [
"kernel32-sys",
"libc",
"log",
"miow 0.2.1",
"miow",
"net2",
"slab 0.4.2",
"winapi 0.2.8",
]
[[package]]
name = "mio-named-pipes"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3"
dependencies = [
"log",
"mio",
"miow 0.3.3",
"winapi 0.3.8",
]
[[package]]
name = "mio-uds"
version = "0.6.7"
@ -686,16 +649,6 @@ dependencies = [
"ws2_32-sys",
]
[[package]]
name = "miow"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "396aa0f2003d7df8395cb93e09871561ccc3e785f0acb369170e8cc74ddf9226"
dependencies = [
"socket2",
"winapi 0.3.8",
]
[[package]]
name = "native-tls"
version = "0.2.4"
@ -895,6 +848,20 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
[[package]]
name = "postgres"
version = "0.17.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18741b59a558e4dfdb9c968e8126dccbefd6a16bf54e3bc5668d420b4c4ed71b"
dependencies = [
"bytes 0.5.4",
"fallible-iterator",
"futures 0.3.4",
"log",
"tokio 0.2.13",
"tokio-postgres",
]
[[package]]
name = "postgres-protocol"
version = "0.5.1"
@ -1119,16 +1086,6 @@ dependencies = [
"opaque-debug",
]
[[package]]
name = "signal-hook-registry"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94f478ede9f64724c5d173d7bb56099ec3e2d9fc2774aac65d34b8b890405f41"
dependencies = [
"arc-swap",
"libc",
]
[[package]]
name = "siphasher"
version = "0.3.2"
@ -1162,18 +1119,6 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c2fb2ec9bcd216a5b0d0ccf31ab17b5ed1d627960edff65bbe95d3ce221cefc"
[[package]]
name = "socket2"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8b74de517221a2cb01a53349cf54182acdc31a074727d3079068448c0676d85"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"winapi 0.3.8",
]
[[package]]
name = "stringprep"
version = "0.1.2"
@ -1269,21 +1214,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa5e81d6bc4e67fe889d5783bd2a128ab2e0cfa487e0be16b6a8d177b101616"
dependencies = [
"bytes 0.5.4",
"fnv",
"futures-core",
"iovec",
"lazy_static",
"libc",
"memchr",
"mio",
"mio-named-pipes",
"mio-uds",
"num_cpus",
"pin-project-lite",
"signal-hook-registry",
"slab 0.4.2",
"tokio-macros",
"winapi 0.3.8",
]
[[package]]
@ -1358,17 +1297,6 @@ dependencies = [
"log",
]
[[package]]
name = "tokio-macros"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio-mockstream"
version = "1.1.0"

@ -6,7 +6,5 @@ edition = "2018"
[dependencies]
irc = "0.13.6"
http = "0.2"
openssl = { version = "0.10", features = ["vendored"] }
tokio-postgres = "0.5.3"
tokio = { version = "0.2.13", features = ["full"] }
postgres = "0.17.2"

@ -1,26 +1,22 @@
extern crate irc;
extern crate tokio_postgres;
extern crate postgres;
use irc::client::prelude::{ClientExt, Command, Config, IrcReactor};
use tokio_postgres::{NoTls, Error, Client};
use postgres::{NoTls, Client};
#[tokio::main]
async fn main() -> Result<(), Error> {
fn main() {
// Connect to IRC server
println!("[DEBUG] Connecting to IRC server...");
let irc_config = Config::load("config.toml").unwrap();
let (pg_client, connection) = tokio_postgres::connect("host=localhost user=postgres password=postgres", NoTls)
.await?;
let mut reactor = IrcReactor::new().unwrap();
let irc_client = reactor.prepare_client_and_connect(&irc_config).unwrap();
irc_client.identify().unwrap();
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
reactor.register_client_with_handler(irc_client, |c, m| {
// Connect to database
let mut pg_client = Client::connect("host=localhost user=postgres password=postgres dbname=communitybot", NoTls).unwrap();
if let Command::NOTICE(channel, message) = &m.command {
println!("[{:?}][{}]: {}", &m.response_target(), &channel, &message);
}
@ -46,8 +42,19 @@ async fn main() -> Result<(), Error> {
let mut user_msg: Vec<&str> = user_msg.collect();
user_msg.remove(0);
let user_msg = user_msg.join(" ");
println!("it looks like you said: {:?} - Trying to save to the DB", user_msg);
// save to db
let query_res = pg_client.query_one(
"INSERT INTO posts (nick, post_title) VALUES ($1, $2) RETURNING id",
&[&src_nick, &user_msg]
);
match query_res {
Ok(row) => {
let post_id: i32 = row.get("id");
c.send_privmsg(&channel, format!("Created new post: {}!", post_id));
},
Err(err) => {
c.send_privmsg(&channel, format!("There was an error storing to DB! {}", err));
}
};
} else if message.starts_with("!delete") {
let _ = c.send_privmsg(&channel, "This feature is still being worked on.");
} else if message.starts_with("!help") {
@ -58,14 +65,4 @@ async fn main() -> Result<(), Error> {
});
reactor.run().unwrap();
Ok(())
}
// async fn insert_post(client: &Client, nick: &str, user_msg: &str) -> String {
// let rows2 = pg_client.query(
// "INSERT INTO posts (nick, post_title) VALUES ('$1', '$2')",
// &[&src_nick, &user_msg]
// ).await?;
// let value: &str = rows2[0].get(0);
// println!("{}", value);
// }

Loading…
Cancel
Save