Added db to backend.
This commit is contained in:
+14
-1
@@ -4,8 +4,12 @@ use axum::{
|
||||
http::StatusCode,
|
||||
Json, Router,
|
||||
};
|
||||
use axum::extract::State;
|
||||
use dotenv::dotenv;
|
||||
use env_logger::fmt::style::Reset;
|
||||
use common::models::article::Article;
|
||||
use common::models::origin::Origin;
|
||||
use sqlx::{ postgres::PgPoolOptions, PgPool };
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -20,12 +24,20 @@ async fn main() {
|
||||
// initialize tracing
|
||||
tracing_subscriber::fmt().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");
|
||||
|
||||
// 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));
|
||||
.route("/article", post(create_article))
|
||||
.with_state(pool);
|
||||
|
||||
// run our app with hyper, listening globally on port 3000
|
||||
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||
@@ -39,6 +51,7 @@ async fn root() -> &'static str {
|
||||
|
||||
#[tracing::instrument(ret)]
|
||||
async fn create_article(
|
||||
State(pool): State<PgPool>,
|
||||
Json(payload): Json<Article>,
|
||||
) -> (StatusCode, Json<Article>) {
|
||||
let article = Article {
|
||||
|
||||
Reference in New Issue
Block a user