Skip to content

Commit 2ef002d

Browse files
authored
Merge pull request #8 from SakaDream/actix-docker
Actix docker
2 parents ebcfd48 + c4314a8 commit 2ef002d

File tree

9 files changed

+105
-21
lines changed

9 files changed

+105
-21
lines changed

.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
APP_HOST=localhost
2-
APP_PORT=8080
2+
APP_PORT=8000
33
DATABASE_URL=postgres://username:password@server/db_name

Cargo.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@ authors = ["SakaDream <bahaimt@gmail.com>"]
55
edition = "2018"
66

77
[dependencies]
8-
actix-web = "1.0.0"
9-
actix-rt = "0.2.2"
8+
actix-web = "1.0.5"
9+
actix-rt = "0.2.4"
1010
actix-service = "0.4.1"
11-
log = "0.4.6"
12-
env_logger = "0.6.1"
11+
log = "0.4.8"
12+
env_logger = "0.6.2"
1313
diesel_migrations = "1.4.0"
14-
serde = "1.0.92"
15-
serde_derive = "1.0.92"
16-
serde_json = "1.0.39"
14+
serde = "1.0.99"
15+
serde_derive = "1.0.99"
16+
serde_json = "1.0.40"
1717
dotenv = "0.14.1"
18-
futures = "0.1.27"
18+
futures = "0.1.28"
1919
failure = "0.1.5"
2020
derive_more = "0.15.0"
2121
jsonwebtoken = "6.0.1"
22-
bcrypt = "0.4.0"
22+
bcrypt = "0.5.0"
2323
time = "0.1.42"
2424

2525
[dependencies.diesel]
2626
version = "1.4.2"
2727
features = ["postgres", "r2d2", "chrono"]
2828

2929
[dependencies.chrono]
30-
version = "0.4.6"
30+
version = "0.4.7"
3131
features = ["serde"]
3232

3333
[dependencies.uuid]

Dockerfile

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# build stage
2+
FROM rustlang/rust:nightly-slim as build
3+
4+
# install libpq
5+
RUN apt-get update
6+
RUN apt-get install -y libpq-dev
7+
RUN rm -rf /var/lib/apt/lists/*
8+
9+
# create new empty binary project
10+
RUN USER=root cargo new --bin app
11+
WORKDIR /app
12+
13+
# copy manifests
14+
COPY ./Cargo.toml ./Cargo.toml
15+
16+
# build this project to cache dependencies
17+
RUN cargo build --release
18+
RUN rm src/*.rs
19+
20+
# copy project source and necessary files
21+
COPY ./src ./src
22+
COPY ./migrations ./migrations
23+
COPY ./diesel.toml .
24+
25+
# add .env and secret.key for Docker env
26+
RUN touch .env
27+
RUN mv src/secret.key.sample src/secret.key
28+
29+
# rebuild app with project source
30+
RUN rm ./target/release/deps/actix_web_rest_api_with_jwt*
31+
RUN cargo build --release
32+
33+
# deploy stage
34+
FROM debian:stretch-slim
35+
36+
# create app directory
37+
RUN mkdir app
38+
WORKDIR /app
39+
40+
# install libpq
41+
RUN apt-get update
42+
RUN apt-get install -y libpq-dev
43+
RUN rm -rf /var/lib/apt/lists/*
44+
45+
# copy binary and configuration files
46+
COPY --from=build /app/target/release/actix-web-rest-api-with-jwt .
47+
COPY --from=build /app/.env .
48+
COPY --from=build /app/diesel.toml .
49+
50+
# expose port
51+
EXPOSE 8000
52+
53+
# run the binary
54+
ENTRYPOINT ["/app/actix-web-rest-api-with-jwt"]

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
A simple CRUD backend app using Actix-web, Diesel and JWT
44

5-
# Require
5+
## Require
66

77
- [Rust Stable](https://rustup.rs)
88
- [Postgres](https://www.postgresql.org/)
99

10-
# How to run
10+
Or using [Docker](https://www.docker.com/)
11+
12+
## How to run
13+
14+
### Manual
1115

1216
- Rename `secret.key.sample` to `secret.key` or create your own key by running `head -c16 /dev/urandom > secret.key` in command line (Linux/UNIX only) and copy to `/src` folder
1317
- Create a database in postgres cli or [pgAdmin](https://www.pgadmin.org/) tool
@@ -18,11 +22,16 @@ A simple CRUD backend app using Actix-web, Diesel and JWT
1822
- Linux/UNIX: `target/release/address_book_rest_api`
1923
- Enjoy! 😄
2024

21-
# APIs
25+
### Docker
26+
27+
- Enter into project directory and run `docker-compose up`
28+
- Enjoy! 😄
29+
30+
## APIs
2231

23-
## Address: **`localhost:8080`**
32+
### Address: **`localhost:8000`**
2433

25-
### **`GET /api/ping`**: Ping
34+
### `GET /api/ping`: Ping
2635

2736
- Response:
2837
- 200 OK

docker-compose.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '3'
2+
services:
3+
db:
4+
container_name: address_book_db
5+
image: postgres
6+
restart: always
7+
ports:
8+
- "5432:5432"
9+
environment:
10+
- POSTGRES_PASSWORD=postgres
11+
app:
12+
build: .
13+
restart: always
14+
ports:
15+
- "8000:8000"
16+
environment:
17+
- APP_HOST=0.0.0.0
18+
- APP_PORT=8000
19+
- DATABASE_URL=postgres://postgres:postgres@db/postgres
20+
depends_on:
21+
- db

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn main() -> io::Result<()> {
6161
.bind(&app_url)?
6262
.start();
6363

64-
info!("Server is started at {}", &app_port);
64+
info!("Server is started at {}", &app_url);
6565

6666
sys.run()
6767
}

src/middleware/authen_middleware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ where
5252

5353
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
5454
// Bypass some account routes
55-
for ignore_route in constants::IGNORE_ROUTES.into_iter() {
55+
for ignore_route in constants::IGNORE_ROUTES.iter() {
5656
if req.path().starts_with(ignore_route) {
5757
return Either::A(self.service.call(req));
5858
}

src/models/user_token.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::models::user::LoginInfoDTO;
22
use jsonwebtoken::Header;
33

4-
pub static KEY: &'static [u8; 16] = include_bytes!("../secret.key");
4+
pub static KEY: [u8; 16] = *include_bytes!("../secret.key");
55
static ONE_WEEK: i64 = 60 * 60 * 24 * 7;
66

77
#[derive(Serialize, Deserialize)]
@@ -25,6 +25,6 @@ impl UserToken {
2525
login_session: login.login_session,
2626
};
2727

28-
jsonwebtoken::encode(&Header::default(), &payload, KEY).unwrap()
28+
jsonwebtoken::encode(&Header::default(), &payload, &KEY).unwrap()
2929
}
3030
}

src/utils/token_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use actix_web::web;
99
use jsonwebtoken::{TokenData, Validation};
1010

1111
pub fn decode_token(token: String) -> jsonwebtoken::errors::Result<TokenData<UserToken>> {
12-
jsonwebtoken::decode::<UserToken>(&token, KEY, &Validation::default())
12+
jsonwebtoken::decode::<UserToken>(&token, &KEY, &Validation::default())
1313
}
1414

1515
pub fn verify_token(token_data: &TokenData<UserToken>, pool: &web::Data<Pool>) -> Result<String, String> {

0 commit comments

Comments
 (0)