diff --git a/src/callers/common.rs b/src/callers/common.rs new file mode 100644 index 0000000..c39654b --- /dev/null +++ b/src/callers/common.rs @@ -0,0 +1,4 @@ +// basic handler that responds with a static string +pub async fn root() -> &'static str { + "Hello, World!" +} diff --git a/src/callers/mod.rs b/src/callers/mod.rs new file mode 100644 index 0000000..79b9089 --- /dev/null +++ b/src/callers/mod.rs @@ -0,0 +1,7 @@ +pub mod common; +pub mod register; + +pub mod endpoints { + pub const ROOT: &str = "/"; + pub const REGISTER: &str = "api/v2/register"; +} diff --git a/src/callers/register.rs b/src/callers/register.rs new file mode 100644 index 0000000..7a1299a --- /dev/null +++ b/src/callers/register.rs @@ -0,0 +1,12 @@ +use axum::{Json, http::StatusCode}; + +use crate::models; + +pub async fn register_user( + Json(payload): Json, +) -> (StatusCode, Json) { + let user = models::common::User { + username: payload.username.clone(), + }; + (StatusCode::CREATED, Json(user)) +} diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..f34b535 --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,10 @@ +pub fn get_full() -> String { + get_address() + ":" + &get_port() +} +fn get_address() -> String { + String::from("0.0.0.0") +} + +fn get_port() -> String { + String::from("3000") +} diff --git a/src/main.rs b/src/main.rs index 6a44cd6..a18c999 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ use axum::{ - // Json, Router, - // http::StatusCode, - routing::get, - // routing::{get, post}, + routing::{get, post}, }; -// use serde::{Deserialize, Serialize}; + +pub mod callers; +pub mod config; +pub mod models; #[tokio::main] async fn main() { @@ -14,28 +14,14 @@ async fn main() { // build our application with a route let app = Router::new() - // `GET /` goes to `root` - .route("/", get(root)); - // `POST /users` goes to `create_user` - // .route("/users", post(create_user)); + .route(callers::endpoints::ROOT, get(callers::common::root)) + .route( + callers::endpoints::REGISTER, + post(callers::register::register_user), + ); // run our app with hyper, listening globally on port 3000 - let listener = tokio::net::TcpListener::bind(get_full()).await.unwrap(); + let url = config::get_full(); + let listener = tokio::net::TcpListener::bind(url).await.unwrap(); axum::serve(listener, app).await.unwrap(); } - -fn get_full() -> String { - get_address() + ":" + &get_port() -} -fn get_address() -> String { - String::from("0.0.0.0") -} - -fn get_port() -> String { - String::from("3000") -} - -// basic handler that responds with a static string -async fn root() -> &'static str { - "Hello, World!" -} diff --git a/src/models/common.rs b/src/models/common.rs new file mode 100644 index 0000000..cda15f2 --- /dev/null +++ b/src/models/common.rs @@ -0,0 +1,11 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize)] +pub struct CreateUser { + pub username: String, +} + +#[derive(Serialize)] +pub struct User { + pub username: String, +} diff --git a/src/models/mod.rs b/src/models/mod.rs new file mode 100644 index 0000000..34994bf --- /dev/null +++ b/src/models/mod.rs @@ -0,0 +1 @@ +pub mod common;