Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40b2e43
commit 89327a6
Showing
9 changed files
with
72 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod url_map_dao; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::FromRow; | ||
|
||
#[derive(Debug, FromRow, Clone, Serialize, Deserialize)] | ||
pub struct UrlMap { | ||
key: String, | ||
url: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(()) | ||
} |