Skip to content

Commit

Permalink
finished: Part II : Database Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonkayZK committed Dec 5, 2021
1 parent 40b2e43 commit 89327a6
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -14,3 +14,5 @@ anyhow = "1.0.41"
config = { version = "0.11.0", features = ["json"] }
lazy_static = "1.4.0"
serde = { version = "1.0.126", features = ["derive"] }
sqlx = { version = "0.5.5", features = ["runtime-tokio-rustls", "postgres", "migrate"] }
tokio = { version = "1.6.2", features = ["full"] }
10 changes: 6 additions & 4 deletions Makefile
@@ -1,12 +1,14 @@
.PHONY: fmt clippy clean build pack
.PHONY: fmt clippy clean build pack all ci

all: clean fmt clippy pack

ci: fmt clippy

fmt:
cargo fmt --all -- --check
cargo fmt --all --

clippy:
cargo clippy -- -D warnings
cargo clippy -- -D warnings

clean:
rm -rf ./target
Expand All @@ -15,4 +17,4 @@ build:
cargo build

pack:
cargo build --
cargo build --release
7 changes: 7 additions & 0 deletions addons/start_postgres.sh
@@ -0,0 +1,7 @@
# Run
docker run --name mypostgres -d -p 15432:5432 -e POSTGRES_PASSWORD=123456 postgres:14.1

# Test
docker exec -it mypostgres psql -U postgres -d postgres

# -- select * from pg_tables;
3 changes: 2 additions & 1 deletion configs/default.json
Expand Up @@ -2,6 +2,7 @@
"host": "127.0.0.1",
"port": 9527,
"database": {
"url": "postgres:///testdb?sslmode=disable"
"url": "postgres://username:passwd@host:port/db_name",
"max_connections": 16
}
}
12 changes: 12 additions & 0 deletions dbschema/schema.sql
@@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS url_maps
(
key VARCHAR(50) PRIMARY KEY,
url TEXT NOT NULL
);

INSERT INTO url_maps (key, url)
VALUES ('qq', 'qq.com'),
('google', 'google.com'),
('facebook', 'facebook.com'),
('twitter', 'twitter.com'),
('bilibili', 'bilibili.com');
20 changes: 20 additions & 0 deletions src/configs/database.rs
@@ -1,6 +1,26 @@
use crate::CONFIG;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPoolOptions;
use sqlx::{Pool, Postgres};

#[derive(Debug, Serialize, Deserialize)]
pub struct Database {
pub url: String,
pub max_connections: u32,
}

pub struct DbHandle {
pub pool: Pool<Postgres>,
}

impl DbHandle {
pub async fn new() -> Result<Self> {
let pool = PgPoolOptions::new()
.max_connections(CONFIG.database.max_connections)
.connect(&CONFIG.database.url)
.await?;

Ok(Self { pool })
}
}
1 change: 1 addition & 0 deletions src/dao/mod.rs
@@ -0,0 +1 @@
pub mod url_map_dao;
8 changes: 8 additions & 0 deletions src/dao/url_map_dao.rs
@@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
use sqlx::FromRow;

#[derive(Debug, FromRow, Clone, Serialize, Deserialize)]
pub struct UrlMap {
key: String,
url: String,
}
15 changes: 14 additions & 1 deletion src/main.rs
@@ -1,7 +1,20 @@
use crate::configs::conf::CONFIG;
use crate::configs::database::DbHandle;
use crate::dao::url_map_dao::UrlMap;
use anyhow::Result;

mod configs;
mod dao;

fn main() {
#[tokio::main]
async fn main() -> Result<()> {
println!("{:?}", CONFIG);

let db = DbHandle::new().await.unwrap();
let res = sqlx::query_as::<_, UrlMap>("select * from url_maps")
.fetch_all(&db.pool)
.await?;
println!("result: {:?}", res);

Ok(())
}

0 comments on commit 89327a6

Please sign in to comment.