add DB env
This commit is contained in:
parent
b5db41f907
commit
e7772af6d8
5
.cargo/config.toml
Normal file
5
.cargo/config.toml
Normal file
@ -0,0 +1,5 @@
|
||||
[env]
|
||||
POSTGRES_USER = "postgres"
|
||||
POSTGRES_PASSWORD = "DB_PASS_HERE"
|
||||
POSTGRES_HOST = "localhost"
|
||||
POSTGRES_DBNAME = "test"
|
1144
Cargo.lock
generated
1144
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -9,4 +9,5 @@ edition = "2021"
|
||||
axum = "0.7.5"
|
||||
serde = { version = "1.0.198", features = ["derive"] }
|
||||
serde_json = "1.0.116"
|
||||
sqlx = { version = "0.7.4", features = ["postgres", "migrate", "runtime-tokio-rustls"] }
|
||||
tokio = { version = "1.2", features = ["full"] }
|
||||
|
20
src/main.rs
20
src/main.rs
@ -10,8 +10,14 @@ use axum::{
|
||||
Form, Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{
|
||||
self,
|
||||
postgres::{PgPool, Postgres},
|
||||
Pool,
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
env::var,
|
||||
fs::File,
|
||||
io::{ErrorKind, Read, Seek, Write},
|
||||
net::SocketAddr,
|
||||
@ -28,7 +34,8 @@ async fn handler_404() -> Response {
|
||||
|
||||
// generic handler to serve static for non-api routes
|
||||
async fn serve_file(uri: Uri) -> impl IntoResponse {
|
||||
let mut path_buf: PathBuf = PathBuf::from("./src/static/").join(uri.path().trim_start_matches('/'));
|
||||
let mut path_buf: PathBuf =
|
||||
PathBuf::from("./src/static/").join(uri.path().trim_start_matches('/'));
|
||||
if path_buf.is_dir() {
|
||||
path_buf.push("index.html")
|
||||
}
|
||||
@ -47,6 +54,16 @@ async fn serve_file(uri: Uri) -> impl IntoResponse {
|
||||
}
|
||||
}
|
||||
|
||||
async fn db_connection() -> Result<Pool<Postgres>, sqlx::Error> {
|
||||
let url: String = format!(
|
||||
"postgres://{}:{}@{}:5432",
|
||||
var("POSTGRES_USER").unwrap(),
|
||||
var("POSTGRES_PASSWORD").unwrap(),
|
||||
var("POSTGRES_HOST").unwrap(),
|
||||
);
|
||||
PgPool::connect(&url).await
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let store: Arc<RwLock<Store>> = Arc::new(RwLock::new(store::Store::new()));
|
||||
@ -65,5 +82,6 @@ async fn main() {
|
||||
.route("/", get(serve_file))
|
||||
.route("/*path", get(serve_file))
|
||||
.fallback(handler_404);
|
||||
let _db: Pool<Postgres> = db_connection().await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
21
src/migrations/0_init_schema.sql
Normal file
21
src/migrations/0_init_schema.sql
Normal file
@ -0,0 +1,21 @@
|
||||
CREATE TABLE IF NOT EXISTS questions {
|
||||
id PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
}
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tags {
|
||||
id PRIMARY KEY,
|
||||
label TEXT NOT NULL,
|
||||
}
|
||||
|
||||
CREATE TABLE IF NOT EXISTS question_tag {
|
||||
question_id REFERENCES questions(id),
|
||||
tag_id REFERENCES tags(id),
|
||||
}
|
||||
|
||||
CREATE TABLE IF NOT EXISTS answers {
|
||||
id PRIMARY KEY,
|
||||
question_id REFERENCES questions(id),
|
||||
content TEXT NOT NULL,
|
||||
}
|
Reference in New Issue
Block a user