diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..adc8851 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,19 @@ +image: rustlang/rust:nightly + +basic-build: + stage: build + only: + - testing + script: + - cd server/ + - cargo build --release + + +basic-test: + stage: test + only: + - testing + script: + - cargo run --release + - fc_id=$! + - bash ./tests/main.sh body diff --git a/server/migrations/2020-07-06-022319_messages/up.sql b/server/migrations/2020-07-06-022319_messages/up.sql index d97f62f..1674188 100644 --- a/server/migrations/2020-07-06-022319_messages/up.sql +++ b/server/migrations/2020-07-06-022319_messages/up.sql @@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS `messages`( `time` BIGINT NOT NULL, `content` VARCHAR(2048) NOT NULL, `author_id` BIGINT UNSIGNED NOT NULL, - `channel_id` BIGINT UNSIGNED NOT NULL, + `channel_name` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`author_id`) REFERENCES members(`id`), FOREIGN KEY (`channel_id`) REFERENCES channels(`id`) diff --git a/server/src/auth.rs b/server/src/auth.rs index 66180b7..f1d180f 100644 --- a/server/src/auth.rs +++ b/server/src/auth.rs @@ -14,7 +14,8 @@ pub enum AuthReason { fn open_route(path: &str) -> bool { return path == routes::INVITE_JOIN } -pub async fn wall_entry(path: &str, pool: &Pool, params: &serde_json::Value) -> Result { +pub async fn wall_entry(path: &str, pool: &Pool, params: &mut serde_json::Value) -> Result { + use serde_json::json; // Start by Checking if the api key is in our keystore if open_route(path) { Ok(AuthReason::OpenAuth) @@ -32,7 +33,11 @@ pub async fn wall_entry(path: &str, pool: &Pool, params: &serde_json::Value) -> match db_result { Ok((_, row)) => { match row{ - Some(_) => Ok(AuthReason::Good), + Some(user_row) => { + params["userid"] = json!(user_row.0); + params["username"] = json!(user_row.1); + Ok(AuthReason::Good) + }, None => Ok(AuthReason::NoKey) } } diff --git a/server/src/main.rs b/server/src/main.rs index 0430ff0..e692829 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -81,9 +81,9 @@ async fn main_responder(request: Request) -> Result, hyper: let params_res = http_params::parse_params(&mut body).await; - if let Ok(params) = params_res { + if let Ok(mut params) = params_res { let pool = Pool::new(&env::var("DATABASE_URL").unwrap()); - if let Ok(auth_result) = auth::wall_entry(path, &pool, ¶ms).await { + if let Ok(auth_result) = auth::wall_entry(path, &pool, &mut params).await { // Deal with permissions errors at this point match auth_result { OpenAuth | Good => route_dispatcher(&pool, &mut response, &method, path, params).await, @@ -110,18 +110,42 @@ async fn shutdown_signal() { .expect("Failed to capture ctrl-c signal"); } #[tokio::main] -async fn main() { - dotenv().ok(); - println!("Servering on localhost:8888"); - let addr = SocketAddr::from(([127,0,0,1], 8888)); +async fn main() -> Result<(), u16>{ + const NO_ERR: u16 = 0; + const CONFIG_ERR: u16 = 1; + const SHUTDOWN_ERR: u16 = 2; - let service = make_service_fn(|_conn| async { - Ok::<_, Infallible>(service_fn(main_responder)) - }); + let mut main_ret: u16 = 0; + // setting up environment variables + let d_result = dotenv(); + if let Err(_d) = d_result { + // we may be on a pipeline/prod environment so .env may not be there + if let Err(_e) = env::var("DATABASE_URL") { + main_ret |= CONFIG_ERR; + } + } + if main_ret == NO_ERR { + println!("Servering on localhost:8888"); + let addr = SocketAddr::from(([127,0,0,1], 8888)); + let service = make_service_fn(|_conn| async { + Ok::<_, Infallible>(service_fn(main_responder)) + }); + let server = Server::bind(&addr).serve(service); + let graceful_shutdown = server.with_graceful_shutdown(shutdown_signal()); - let server = Server::bind(&addr).serve(service); - let graceful_shutdown = server.with_graceful_shutdown(shutdown_signal()); - if let Err(e) = graceful_shutdown.await { - eprintln!("Server shutdown error: {}", e); + if let Err(e) = graceful_shutdown.await { + main_ret |= SHUTDOWN_ERR; + eprintln!("Server shutdown error: {}", e); + } + } + + if main_ret != 0 { + // dumb as heck loggin method here + if main_ret & CONFIG_ERR != 0 {println!("ERROR: Config was not setup properly => Missing {{DATABASE_URL}}");} + if main_ret & SHUTDOWN_ERR != 0 {println!("ERROR: Couldn't shutdown gracefully");} + Err(main_ret) + } + else { + Ok(()) } } diff --git a/server/src/messages.rs b/server/src/messages.rs index 943707a..8bba9fb 100644 --- a/server/src/messages.rs +++ b/server/src/messages.rs @@ -7,21 +7,12 @@ use hyper::{Response, Body, StatusCode}; use serde_json::Value; use chrono::Utc; -use crate::db_types::{UBigInt, VarChar}; +use crate::db_types::{UBigInt}; -struct Message { - id: UBigInt, - content: Option, // some messages later might only have file attachments and not text content - author_id: UBigInt, - channel_id: UBigInt, - permissions: UBigInt, -} - - -pub async fn insert_message_table(pool: &Pool, content: &Value, channel_id: &Value, author_id: UBigInt) +pub async fn insert_message_table(pool: &Pool, content: &Value, channel_name: &Value, author_id: UBigInt) -> Result<(), Error>{ - match (content.as_str(), channel_id.as_u64()) { + match (content.as_str(), channel_name.as_str()) { (Some(content), Some(channel)) => { let conn = pool.get_conn().await?; let time = Utc::now().timestamp(); diff --git a/server/tests/verify_basic_cases.sh b/server/tests/verify_basic_cases.sh index 94eee66..1eda5da 100644 --- a/server/tests/verify_basic_cases.sh +++ b/server/tests/verify_basic_cases.sh @@ -24,6 +24,18 @@ delete_channel() { log_result good_delete_channel 200 $code "$result" } +send_message() { + # creating a bs channel, we don't care about this operation + # as long as the other test passes this should be fine + $crl $POST $url/channel/create -d '{"secret":"secret","name":"msgchannel"}' + + kv='{"secret":"secret", "content":"message sample", "channel":123}' + result=$($crl $POST $url/message/send -d "$kv") + code=$(echo "$result" | grep HTTP\/1.1 | awk '{print $2}') + # non-existant channel for now but whatever ignore for now + log_result good_send_message 200 $code "$result" +} + # Dispatcher to run our tests if [ -z $1 ];then for cmd in $active_tests;do