From a704318878d898c388347fb6ca45d9667bbb431e Mon Sep 17 00:00:00 2001 From: mrasheduzzaman Date: Fri, 24 Nov 2023 09:11:17 +0600 Subject: [PATCH] Add test script --- src/main.rs | 16 +++++++++++++++- test.sh | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test.sh diff --git a/src/main.rs b/src/main.rs index b46c4b9..ce2e750 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,9 @@ mod services; use controllers::user_controller; use dotenv::dotenv; +use salvo::cors::Cors; +use salvo::http::Method; +use salvo::logging::Logger; use salvo::prelude::*; use std::env; @@ -14,6 +17,11 @@ async fn health() -> &'static str { "I'm alive" } +#[handler] +async fn index() -> &'static str { + "Welcome to rustful-service" +} + #[tokio::main] async fn main() { @@ -26,7 +34,13 @@ async fn main() { let port = env::var("PORT").expect("PORT is not set in .env file"); let server_url = format!("{host}:{port}"); - let router = Router::new() + let cors = Cors::new() + .allow_methods(vec![Method::GET, Method::POST, Method::DELETE]) + .into_handler(); + + + let router = Router::with_hoop(cors).hoop(Logger::new()) + .get(index) .push(Router::with_path("/healthz").get(health)) .push(Router::with_path("/users") .get(user_controller::get_all_users) diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..4207313 --- /dev/null +++ b/test.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -ex + +res=$(curl -s -X POST http://127.0.0.1:5001/users -H "Content-Type: application/json" \ + -d '{"name": "Rashed", "age": "27"}') + +echo "${res}" + +# lastid="$(echo "${res}" | jq ".id")" + +# can get it individually +# curl -s -X GET "http://127.0.0.1:5001/users/${lastid}" + +# post not published yet +# curl -s -X GET http://127.0.0.1:5001/users | grep -v "${lastid}" + +# publish +# curl -s -X PUT "http://127.0.0.1:5001/users/${lastid}" + +# post now published +# curl -s -X GET http://127.0.0.1:5001/users | grep "${lastid}" + +# delete post +# curl -s -X DELETE "http://127.0.0.1:5001/users/${lastid}"