Skip to content

Commit 7e0c0ca

Browse files
committed
start account system
1 parent 24b0c89 commit 7e0c0ca

File tree

10 files changed

+71
-19
lines changed

10 files changed

+71
-19
lines changed

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
**/.DS_Store
88
**/.classpath
99
**/.dockerignore
10-
**/.env
10+
# **/.env
1111
**/.git
1212
**/.gitignore
1313
**/.project

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ Cargo.lock
1515
.env
1616
# password for db
1717
db
18+
.sqlx
19+
rustc-ice*

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
1818
dotenvy = "0.15"
1919
thiserror = "1"
2020
clap = { version = "4", features = ["derive"] }
21+
serde = { version = "1", features = ["derive"] }
22+
serde_json = "1"
23+
uuid = { version = "1", features = ["v4", "serde"] }
24+
chrono = { version = "0.4", features = ["serde"] }
25+
2126

2227
[features]
2328
postgres = ["sqlx/postgres", "sqlx/uuid"]

Dockerfile

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ WORKDIR /app
2222
# source code into the container. Once built, copy the executable to an
2323
# output directory before the cache mounted /app/target is unmounted.
2424
RUN --mount=type=bind,source=src,target=src \
25-
--mount=type=bind,source=migrations,target=migrations \
26-
--mount=type=bind,source=Cargo.toml,target=Cargo.toml \
27-
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
28-
--mount=type=cache,target=/app/target/ \
29-
--mount=type=cache,target=/usr/local/cargo/registry/ \
30-
<<EOF
25+
--mount=type=bind,source=migrations,target=migrations \
26+
--mount=type=bind,source=./.env,target=/app/.env \
27+
--mount=type=bind,source=Cargo.toml,target=Cargo.toml \
28+
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
29+
--mount=type=cache,target=/app/target/ \
30+
--mount=type=cache,target=/usr/local/cargo/registry/ \
31+
<<EOF
3132
set -e
3233
cargo build --locked --release
3334
cp ./target/release/$APP_NAME /bin/server
@@ -50,13 +51,13 @@ FROM debian:bullseye-slim AS final
5051
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
5152
ARG UID=10001
5253
RUN adduser \
53-
--disabled-password \
54-
--gecos "" \
55-
--home "/nonexistent" \
56-
--shell "/sbin/nologin" \
57-
--no-create-home \
58-
--uid "${UID}" \
59-
appuser
54+
--disabled-password \
55+
--gecos "" \
56+
--home "/nonexistent" \
57+
--shell "/sbin/nologin" \
58+
--no-create-home \
59+
--uid "${UID}" \
60+
appuser
6061
USER appuser
6162

6263
# Copy the executable from the "build" stage.

compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ services:
1313
- db-password
1414
build:
1515
context: .
16+
network: host
1617
target: final
1718
command: /bin/server -z db -u postgres --db-pw /run/secrets/db-password -p 5432
1819
develop:

src/db/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
pub mod user;
12
use sqlx::{postgres::PgConnectOptions, PgPool};
23

4+
//test
35
pub async fn init_db(database_opts: PgConnectOptions) -> Result<PgPool, sqlx::Error> {
46
let pool = PgPool::connect_with(database_opts).await?;
57

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
7070

7171
let app_router = construct_router(db_pool);
7272
let listener = tokio::net::TcpListener::bind("0.0.0.0:6087").await.unwrap();
73-
println!("Server running on port 6087 so true fr");
73+
println!("Server running on port 6087");
7474
axum::serve(listener, app_router).await.unwrap();
7575
Ok(())
7676
}

src/routes/account.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use axum::{extract::State, Json};
2+
use serde::Deserialize;
3+
use sqlx::PgPool;
4+
5+
use crate::db::user::User;
6+
7+
#[derive(Deserialize, Debug)]
8+
pub struct RegisterData {
9+
username: String,
10+
password: String,
11+
first_name: Option<String>,
12+
content_key: String,
13+
}
14+
15+
pub async fn register(State(db): State<PgPool>, Json(reg_data): Json<RegisterData>) {
16+
let new_user = User::new(
17+
reg_data.username,
18+
reg_data.password,
19+
reg_data.content_key,
20+
reg_data.first_name,
21+
);
22+
}

src/routes/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
use axum::{handler::Handler, routing::get, Router};
1+
pub mod account;
2+
use axum::{
3+
handler::Handler,
4+
routing::{get, post},
5+
Router,
6+
};
27
use sqlx::PgPool;
38

9+
use self::account::register;
10+
411
pub async fn test_route() -> &'static str {
512
"server works"
613
}
714

815
pub fn construct_router(db: PgPool) -> Router {
9-
let router = Router::new().route("/test", get(test_route)).with_state(db);
10-
11-
router
16+
Router::new()
17+
.route("/account/register", post(register))
18+
.route("/test", get(test_route))
19+
.with_state(db)
1220
}

0 commit comments

Comments
 (0)