Separated the code (#7)
All checks were successful
Release Tagging / release (push) Successful in 26s
Rust Build / Check (push) Successful in 27s
Rust Build / Test Suite (push) Successful in 30s
Rust Build / Rustfmt (push) Successful in 23s
Rust Build / Clippy (push) Successful in 31s
Rust Build / build (push) Successful in 35s
All checks were successful
Release Tagging / release (push) Successful in 26s
Rust Build / Check (push) Successful in 27s
Rust Build / Test Suite (push) Successful in 30s
Rust Build / Rustfmt (push) Successful in 23s
Rust Build / Clippy (push) Successful in 31s
Rust Build / build (push) Successful in 35s
Reviewed-on: #7
This commit is contained in:
4
src/callers/common.rs
Normal file
4
src/callers/common.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
// basic handler that responds with a static string
|
||||
pub async fn root() -> &'static str {
|
||||
"Hello, World!"
|
||||
}
|
7
src/callers/mod.rs
Normal file
7
src/callers/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
pub mod common;
|
||||
pub mod register;
|
||||
|
||||
pub mod endpoints {
|
||||
pub const ROOT: &str = "/";
|
||||
pub const REGISTER: &str = "api/v2/register";
|
||||
}
|
12
src/callers/register.rs
Normal file
12
src/callers/register.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use axum::{Json, http::StatusCode};
|
||||
|
||||
use crate::models;
|
||||
|
||||
pub async fn register_user(
|
||||
Json(payload): Json<models::common::CreateUser>,
|
||||
) -> (StatusCode, Json<models::common::User>) {
|
||||
let user = models::common::User {
|
||||
username: payload.username.clone(),
|
||||
};
|
||||
(StatusCode::CREATED, Json(user))
|
||||
}
|
10
src/config/mod.rs
Normal file
10
src/config/mod.rs
Normal file
@@ -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")
|
||||
}
|
38
src/main.rs
38
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!"
|
||||
}
|
||||
|
11
src/models/common.rs
Normal file
11
src/models/common.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreateUser {
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct User {
|
||||
pub username: String,
|
||||
}
|
1
src/models/mod.rs
Normal file
1
src/models/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod common;
|
Reference in New Issue
Block a user