Moved server, added backend structs and added ui elements.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
use std::env;
|
||||
use axum::{
|
||||
routing::{get, post},
|
||||
http::StatusCode,
|
||||
Json, Router,
|
||||
};
|
||||
use dotenv::dotenv;
|
||||
use common::models::article::Article;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenv().ok();
|
||||
|
||||
let server_address = env::var("SERVER_ADDRESS").expect("SERVER_ADDRESS is not set in .env file");
|
||||
let server_port = env::var("SERVER_PORT").expect("SERVER_PORT is not set in .env file");
|
||||
let database = env::var("DATABASE_URL").expect("DATABASE is not set in .env file");
|
||||
|
||||
let addr = format!("{}:{}", server_address, server_port);
|
||||
|
||||
// initialize tracing
|
||||
tracing_subscriber::fmt().init();
|
||||
|
||||
// build our application with a route
|
||||
let app = Router::new()
|
||||
// `GET /` goes to `root`
|
||||
.route("/", get(root))
|
||||
// `POST /users` goes to `create_user`
|
||||
.route("/article", post(create_article));
|
||||
|
||||
// run our app with hyper, listening globally on port 3000
|
||||
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
// basic handler that responds with a static string
|
||||
async fn root() -> &'static str {
|
||||
"Hello, World!"
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret)]
|
||||
async fn create_article(
|
||||
Json(payload): Json<Article>,
|
||||
) -> (StatusCode, Json<Article>) {
|
||||
let article = Article {
|
||||
id: payload.id,
|
||||
name: payload.name,
|
||||
bio: payload.bio,
|
||||
origin: payload.origin,
|
||||
description: payload.description,
|
||||
ingredients: payload.ingredients,
|
||||
variants: payload.variants,
|
||||
};
|
||||
|
||||
(StatusCode::CREATED, Json(article))
|
||||
}
|
||||
Reference in New Issue
Block a user