Added db to backend.
This commit is contained in:
Generated
+2
-2
@@ -376,9 +376,9 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "chrono"
|
name = "chrono"
|
||||||
version = "0.4.42"
|
version = "0.4.43"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2"
|
checksum = "fac4744fb15ae8337dc853fee7fb3f4e48c0fbaa23d0afe49c447b4fab126118"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::FromRow;
|
||||||
use crate::models::origin::Origin;
|
use crate::models::origin::Origin;
|
||||||
use crate::models::variant::Variant;
|
use crate::models::variant::Variant;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
|
||||||
pub struct Article {
|
pub struct Article {
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::FromRow;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
|
||||||
pub struct Variant {
|
pub struct Variant {
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -17,7 +17,6 @@ http = "1.4.0"
|
|||||||
tracing = "=0.1.44"
|
tracing = "=0.1.44"
|
||||||
tracing-subscriber = "0.3.22"
|
tracing-subscriber = "0.3.22"
|
||||||
|
|
||||||
|
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
serde_json = "1.0.149"
|
serde_json = "1.0.149"
|
||||||
|
|
||||||
@@ -25,8 +24,8 @@ serde_json = "1.0.149"
|
|||||||
env_logger = "0.11.8"
|
env_logger = "0.11.8"
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
sqlx = { version = "0.8.6", features = [ "runtime-async-std-native-tls", "postgres", "chrono" ] }
|
sqlx = { version = "0.8.6", features = [ "runtime-async-std-native-tls", "postgres", "chrono", "macros" ] }
|
||||||
|
|
||||||
# Etc.
|
# Etc.
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
chrono = { version = "0.4.42" }
|
chrono = { version = "0.4.43" }
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
-- Add migration script here
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# Migration scripts
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
````bash
|
||||||
|
cargo install sqlx-cli
|
||||||
|
````
|
||||||
|
|
||||||
|
## Create database
|
||||||
|
|
||||||
|
````bash
|
||||||
|
sqlx database setup
|
||||||
|
````
|
||||||
|
|
||||||
|
## Use migration
|
||||||
|
````bash
|
||||||
|
# Creating new script file.
|
||||||
|
sqlx migrate add <Name for script>
|
||||||
|
|
||||||
|
# Run migration
|
||||||
|
sqlx migrate run
|
||||||
|
````
|
||||||
+14
-1
@@ -4,8 +4,12 @@ use axum::{
|
|||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
Json, Router,
|
Json, Router,
|
||||||
};
|
};
|
||||||
|
use axum::extract::State;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
|
use env_logger::fmt::style::Reset;
|
||||||
use common::models::article::Article;
|
use common::models::article::Article;
|
||||||
|
use common::models::origin::Origin;
|
||||||
|
use sqlx::{ postgres::PgPoolOptions, PgPool };
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@@ -20,12 +24,20 @@ async fn main() {
|
|||||||
// initialize tracing
|
// initialize tracing
|
||||||
tracing_subscriber::fmt().init();
|
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
|
// build our application with a route
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
// `GET /` goes to `root`
|
// `GET /` goes to `root`
|
||||||
.route("/", get(root))
|
.route("/", get(root))
|
||||||
// `POST /users` goes to `create_user`
|
// `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
|
// run our app with hyper, listening globally on port 3000
|
||||||
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||||
@@ -39,6 +51,7 @@ async fn root() -> &'static str {
|
|||||||
|
|
||||||
#[tracing::instrument(ret)]
|
#[tracing::instrument(ret)]
|
||||||
async fn create_article(
|
async fn create_article(
|
||||||
|
State(pool): State<PgPool>,
|
||||||
Json(payload): Json<Article>,
|
Json(payload): Json<Article>,
|
||||||
) -> (StatusCode, Json<Article>) {
|
) -> (StatusCode, Json<Article>) {
|
||||||
let article = Article {
|
let article = Article {
|
||||||
|
|||||||
Reference in New Issue
Block a user