Skip to content

Commit

Permalink
feat: support deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
fan-tastic-z committed May 30, 2024
1 parent d2c3433 commit 457b61e
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace]
members = ["./migration", "."]

[dependencies]
thiserror = "1"
clap = { version = "4.5.4", features = ["derive"] }
Expand Down
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ARG BASE_IMAGE=rust:1.78.0-slim-buster

FROM $BASE_IMAGE as planner
WORKDIR /app
RUN cargo install cargo-chef
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM $BASE_IMAGE as cacher
WORKDIR /app
RUN cargo install cargo-chef \
&& apt update -y \
&& apt install pkg-config libssl-dev -y
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json

FROM $BASE_IMAGE as builder
WORKDIR /app
COPY . .
RUN apt update -y \
&& apt install pkg-config libssl-dev -y
# Copy over the cached dependencies
COPY --from=cacher /app/target target
COPY --from=cacher $CARGO_HOME $CARGO_HOME
RUN cargo build --release

FROM gcr.io/distroless/cc-debian10
WORKDIR /app
COPY --from=builder /app/target/release/watchvuln-rs .
CMD ["./watchvuln-rs"]
7 changes: 6 additions & 1 deletion _typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@


[files]
extend-exclude = ["CHANGELOG.md", "notebooks/*", "config/development.yaml"]
extend-exclude = [
"CHANGELOG.md",
"notebooks/*",
"config/development.yaml",
"migration",
]

[default.extend-identifiers]
vuln_informations = "vuln_informations"
Expand Down
40 changes: 40 additions & 0 deletions deployment/config/development.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
database:
# Database connection URI
uri: {{ get_env(name="DATABASE_URL", default="postgres://postgres:123456@localhost:5432/watchvuln") }}
# When enabled, the sql query will be logged.
enable_logging: false
# Set the timeout duration when acquiring a connection.
connect_timeout: 500
# Set the idle duration before closing a connection.
idle_timeout: 500
# Minimum number of connections for a pool.
min_connections: 1
# Maximum number of connections for a pool.
max_connections: 1

task:
# every day 7:00-22:00 interval 30 minute Execute task
cron_config: "0 */1 7-21 * * *"

# Application logging configuration
logger:
# Enable or disable logging.
enable: true
# Enable pretty backtrace (sets RUST_BACKTRACE=1)
pretty_backtrace: true
# Log level, options: trace, debug, info, warn or error.
level: info
# Define the logging format. options: compact, pretty or Json
format: compact
# By default the logger has filtering only logs that came from your code or logs that came from `loco` framework. to see all third party libraries
# Uncomment the line below to override to see all third party libraries you can enable this config and override the logger filters.
# override_filter: trace

# Application push message configuration, Now just support tg bot
tg_bot:
chat_id: {{ get_env(name="TG_CHAT_ID", default=0) }}
token: {{ get_env(name="TG_TOKEN", default="") }}

ding_bot:
access_token: {{ get_env(name="DING_ACCESS_TOKEN", default="") }}
secret_token: {{ get_env(name="DING_SECRET_TOKEN", default="") }}
25 changes: 25 additions & 0 deletions deployment/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3"
services:
app:
restart: always
image: fantasticzf/watchvuln-rs:latest
env_file:
- env/app.env
volumes:
- "./config:/app/config"
networks:
- vuln
depends_on:
- db

db:
image: postgres:15.3-alpine
restart: unless-stopped
# ports:
# - 5432:5432
networks:
- vuln
volumes:
- "./data:/var/lib/postgresql/data"
env_file:
- env/postgres.env
5 changes: 5 additions & 0 deletions deployment/env/app.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DATABASE_URL=postgres://watchvuln:watchvuln@db:5432/watchvuln
DING_ACCESS_TOKEN=
DING_SECRET_TOKEN=
TG_CHAT_ID=0
TG_TOKEN=
3 changes: 3 additions & 0 deletions deployment/env/postgres.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POSTGRES_DB=watchvuln
POSTGRES_USER=watchvuln
POSTGRES_PASSWORD=watchvuln
6 changes: 3 additions & 3 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20240417_015641_create_vuln_information::Migration),
]
vec![Box::new(
m20240417_015641_create_vuln_information::Migration,
)]
}
}
1 change: 1 addition & 0 deletions migration/src/m20240417_015641_create_vuln_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl MigrationTrait for Migration {
}
}

#[allow(clippy::upper_case_acronyms)]
#[derive(DeriveIden)]
enum VulnInformations {
Table,
Expand Down
4 changes: 2 additions & 2 deletions src/push/dingding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl MessageBot for DingDing {
});

let sign = self.generate_sign()?;
println!("{:?}", self);

let res: DingResponse = help
.http_client
.post(DING_API_URL)
Expand All @@ -46,7 +46,7 @@ impl MessageBot for DingDing {
.await?
.json()
.await?;
println!("{:?}", res);

if res.errcode != 0 {
warn!(
"ding push markdown message error, err msg is {}",
Expand Down

0 comments on commit 457b61e

Please sign in to comment.