Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Add redis tower session store
Browse files Browse the repository at this point in the history
  • Loading branch information
0rzech committed Apr 6, 2024
1 parent 33d5bd8 commit f1ce3f4
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ jobs:
POSTGRES_DB: postgres
ports:
- 5432:5432
redis:
image: redis:7
ports:
- 6379:6379
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
Expand Down
193 changes: 193 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ time = { version = "0.3.34", features = ["macros", "serde"] }
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
tower = "0.4.13"
tower-http = { version = "0.5.1", features = ["request-id", "trace", "util"] }
tower-sessions = "0.12.1"
tower-sessions-redis-store = "0.12.0"
tracing = "0.1.40"
tracing-bunyan-formatter = "0.3.9"
tracing-log = "0.2.0"
Expand Down
1 change: 1 addition & 0 deletions configuration/base.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
application:
port: 8000
hmac_secret: long-and-very-secret-random-key-needed-to-verify-message-integrity
redis_uri: redis://localhost:6379
database:
host: localhost
port: 5432
Expand Down
2 changes: 1 addition & 1 deletion scripts/init_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if [[ ${SKIP_PODMAN:=false} == false ]]; then
--env POSTGRES_USER="${db_user}" \
--env POSTGRES_PASSWORD="${db_password}" \
--env POSTGRES_DB="${db_name}" \
postgres \
docker.io/library/postgres:latest \
postgres -N 1000

sleep 5
Expand Down
24 changes: 24 additions & 0 deletions scripts/init_redis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

set -eou pipefail

podman_cmd=('podman')
if [[ -f /run/.containerenv ]] && [[ -f /run/.toolboxenv ]]; then
podman_cmd=('flatpak-spawn' '--host' 'podman')
fi

RUNNING_CONTAINER=$("${podman_cmd[@]}" ps --filter 'name=redis' --format '{{.ID}}')
if [[ -n $RUNNING_CONTAINER ]]; then
>&2 echo 'there is a redis container already running, kill it with'
>&2 echo " ${podman_cmd[@]} kill ${RUNNING_CONTAINER}"
exit 1
fi

"${podman_cmd[@]}" run \
--rm \
--detach \
--name "redis_$(date '+%s')" \
--publish 6379:6379 \
docker.io/library/redis:latest

>&2 echo 'Readis is ready to go!'
1 change: 1 addition & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct ApplicationSettings {
pub port: u16,
pub base_url: String,
pub hmac_secret: Secret<String>,
pub redis_uri: Secret<String>,
}

#[derive(Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use zero2prod::{
};

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
async fn main() -> Result<(), anyhow::Error> {
let subscriber = get_subscriber("zero2prod".into(), "info".into(), std::io::stdout);
init_subscriber(subscriber);

Expand Down
Loading

0 comments on commit f1ce3f4

Please sign in to comment.