Added functionality to added article.
This commit is contained in:
+26
-5
@@ -7,9 +7,14 @@ use axum::{
|
||||
use axum::extract::State;
|
||||
use dotenv::dotenv;
|
||||
use env_logger::fmt::style::Reset;
|
||||
use common::models::article::Article;
|
||||
use http::{header, Method};
|
||||
use common::models::article::{insert_article, Article};
|
||||
use common::models::origin::Origin;
|
||||
use sqlx::{ postgres::PgPoolOptions, PgPool };
|
||||
use tower_http::cors::{AllowOrigin, Any, CorsLayer};
|
||||
use tracing::info;
|
||||
use http::header::{AUTHORIZATION, ACCEPT, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE, ACCESS_CONTROL_ALLOW_CREDENTIALS};
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -23,24 +28,36 @@ async fn main() {
|
||||
|
||||
// initialize tracing
|
||||
tracing_subscriber::fmt().init();
|
||||
/*
|
||||
tracing_subscriber::fmt()
|
||||
.with_max_level(tracing::Level::INFO)
|
||||
.init();
|
||||
*/
|
||||
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(50)
|
||||
.connect(&database)
|
||||
.await
|
||||
.expect(&format!("Can't connect to database: {}", &database));
|
||||
|
||||
sqlx::migrate!().run(&pool).await.expect("Migrations failed");
|
||||
|
||||
let cors = CorsLayer::new()
|
||||
.allow_methods([Method::GET, Method::POST])
|
||||
.allow_headers([ACCEPT, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE])
|
||||
.allow_origin(AllowOrigin::any());
|
||||
|
||||
// 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))
|
||||
.with_state(pool);
|
||||
.with_state(pool)
|
||||
.layer(cors);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
|
||||
info!("Server started on: {}", &addr);
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
@@ -54,6 +71,8 @@ async fn create_article(
|
||||
State(pool): State<PgPool>,
|
||||
Json(payload): Json<Article>,
|
||||
) -> (StatusCode, Json<Article>) {
|
||||
info!("Test");
|
||||
|
||||
let article = Article {
|
||||
id: payload.id,
|
||||
name: payload.name,
|
||||
@@ -64,5 +83,7 @@ async fn create_article(
|
||||
variants: payload.variants,
|
||||
};
|
||||
|
||||
insert_article(&article, pool).await;
|
||||
|
||||
(StatusCode::CREATED, Json(article))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user