Skip to content

Commit aa8e2dd

Browse files
authored
feat: add code (#4)
1 parent b0b846f commit aa8e2dd

File tree

12 files changed

+2021
-12
lines changed

12 files changed

+2021
-12
lines changed

.github/workflows/ci.yml

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
lint:
14+
name: Lint & Format
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: dtolnay/rust-toolchain@stable
19+
with:
20+
components: rustfmt, clippy
21+
- uses: Swatinem/rust-cache@v2
22+
- run: cargo fmt -- --check
23+
- run: cargo clippy -- -D warnings
24+
25+
test-linux:
26+
name: Test (Linux + Redis)
27+
runs-on: ubuntu-latest
28+
needs: lint
29+
strategy:
30+
matrix:
31+
rust: ['1.82', 'stable']
32+
redis-mode: ['standalone', 'cluster']
33+
34+
services:
35+
redis:
36+
image: redis:alpine
37+
ports: [6379:6379]
38+
options: --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
39+
40+
steps:
41+
- uses: actions/checkout@v4
42+
- uses: dtolnay/rust-toolchain@stable
43+
with:
44+
toolchain: ${{ matrix.rust }}
45+
- uses: Swatinem/rust-cache@v2
46+
47+
- name: Setup Redis Cluster
48+
if: matrix.redis-mode == 'cluster'
49+
run: |
50+
sudo apt-get update && sudo apt-get install -y redis-server redis-tools
51+
mkdir -p /tmp/redis-cluster/{7000,7001,7002,7003,7004,7005}
52+
53+
# Create cluster nodes
54+
for port in 7000 7001 7002 7003 7004 7005; do
55+
cat > /tmp/redis-cluster/$port/redis.conf <<EOF
56+
port $port
57+
cluster-enabled yes
58+
cluster-config-file nodes-$port.conf
59+
cluster-node-timeout 5000
60+
appendonly yes
61+
bind 0.0.0.0
62+
protected-mode no
63+
EOF
64+
redis-server /tmp/redis-cluster/$port/redis.conf --daemonize yes --dir /tmp/redis-cluster/$port/
65+
done
66+
67+
sleep 10
68+
echo "yes" | redis-cli --cluster create 127.0.0.1:{7000,7001,7002,7003,7004,7005} --cluster-replicas 1
69+
70+
- name: Build and Test
71+
run: |
72+
cargo build --all-features
73+
cargo test --lib
74+
75+
- name: Run Integration Tests
76+
env:
77+
REDIS_AVAILABLE: ${{ matrix.redis-mode == 'standalone' }}
78+
REDIS_URL: redis://127.0.0.1:6379
79+
REDIS_CLUSTER_AVAILABLE: ${{ matrix.redis-mode == 'cluster' }}
80+
REDIS_CLUSTER_PUBSUB_NODE: redis://127.0.0.1:7000
81+
RUST_LOG: debug
82+
run: |
83+
if [ "${{ matrix.redis-mode }}" = "standalone" ]; then
84+
echo "Running Standalone Redis Tests"
85+
echo "Redis URL: $REDIS_URL"
86+
echo "Note: Cluster-specific tests will be skipped"
87+
echo ""
88+
# Run all non-ignored tests (cluster tests are marked with #[ignore])
89+
cargo test --lib
90+
elif [ "${{ matrix.redis-mode }}" = "cluster" ]; then
91+
echo "Running Redis Cluster Tests"
92+
echo "PubSub Node: $REDIS_CLUSTER_PUBSUB_NODE"
93+
echo "Note: All instances MUST use the SAME node for PubSub"
94+
echo ""
95+
echo "Checking cluster status..."
96+
redis-cli -p 7000 cluster nodes || echo "Warning: Could not get cluster status"
97+
echo ""
98+
echo "Running all tests including cluster-specific tests..."
99+
# Run all tests including ignored ones (cluster tests)
100+
timeout 300s cargo test --lib -- --include-ignored --nocapture
101+
fi
102+
103+
- name: Security Audit
104+
if: matrix.rust == 'stable'
105+
run: |
106+
cargo install cargo-audit
107+
cargo audit
108+
109+
test-cross-platform:
110+
name: Test (${{ matrix.os }})
111+
runs-on: ${{ matrix.os }}
112+
needs: lint
113+
strategy:
114+
matrix:
115+
os: [windows-latest, macos-latest]
116+
rust: ['1.82', 'stable']
117+
118+
steps:
119+
- uses: actions/checkout@v4
120+
- uses: dtolnay/rust-toolchain@stable
121+
with:
122+
toolchain: ${{ matrix.rust }}
123+
- uses: Swatinem/rust-cache@v2
124+
- run: cargo build --all-features
125+
- run: cargo test --lib

.github/workflows/coverage.yml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: Code Coverage
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
cover:
13+
name: Auto Codecov Coverage
14+
runs-on: ubuntu-latest
15+
16+
services:
17+
redis:
18+
image: redis
19+
ports:
20+
- 6379:6379
21+
options: >-
22+
--health-cmd "redis-cli ping"
23+
--health-interval 10s
24+
--health-timeout 5s
25+
--health-retries 5
26+
27+
steps:
28+
- name: Checkout Repository
29+
uses: actions/checkout@v4
30+
31+
- name: Install Rust toolchain
32+
run: |
33+
rustup set profile minimal
34+
rustup update --no-self-update stable
35+
rustup default stable
36+
37+
- name: Install Redis server and tools for cluster setup
38+
run: |
39+
sudo apt-get update
40+
sudo apt-get install -y redis-server redis-tools
41+
42+
- name: Setup Redis Cluster
43+
run: |
44+
# Create Redis cluster configuration
45+
mkdir -p /tmp/redis-cluster
46+
for port in 7000 7001 7002 7003 7004 7005; do
47+
mkdir -p /tmp/redis-cluster/$port
48+
cat > /tmp/redis-cluster/$port/redis.conf << EOF
49+
port $port
50+
cluster-enabled yes
51+
cluster-config-file nodes-$port.conf
52+
cluster-node-timeout 5000
53+
appendonly yes
54+
bind 0.0.0.0
55+
protected-mode no
56+
EOF
57+
done
58+
59+
# Start Redis cluster nodes
60+
for port in 7000 7001 7002 7003 7004 7005; do
61+
redis-server /tmp/redis-cluster/$port/redis.conf --daemonize yes --dir /tmp/redis-cluster/$port/
62+
done
63+
64+
# Wait for nodes to start
65+
sleep 15
66+
67+
# Verify nodes are running
68+
for port in 7000 7001 7002 7003 7004 7005; do
69+
redis-cli -p $port ping
70+
done
71+
72+
# Create cluster
73+
echo "yes" | redis-cli --cluster create \
74+
127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 \
75+
127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \
76+
--cluster-replicas 1
77+
78+
# Verify cluster is working
79+
redis-cli -c -p 7000 cluster nodes
80+
81+
- name: Run cargo-tarpaulin
82+
run: |
83+
# Set environment variables for Redis services
84+
export REDIS_AVAILABLE=true
85+
export REDIS_URL=redis://127.0.0.1:6379
86+
export REDIS_CLUSTER_AVAILABLE=true
87+
export REDIS_CLUSTER_URLS=redis://127.0.0.1:7000,redis://127.0.0.1:7001,redis://127.0.0.1:7002
88+
89+
cargo install cargo-tarpaulin
90+
cargo tarpaulin --out xml
91+
92+
- name: Upload to codecov.io
93+
uses: codecov/codecov-action@v4
94+
with:
95+
token: ${{secrets.CODECOV_TOKEN}}

.github/workflows/release.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Release-plz
2+
3+
on:
4+
workflow_run:
5+
workflows: ["CI"]
6+
types: [completed]
7+
8+
jobs:
9+
# Release unpublished packages.
10+
release-plz-release:
11+
name: Release-plz release
12+
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push' && github.event.workflow_run.head_branch == 'master' }}
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: write
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v5
19+
with:
20+
fetch-depth: 0
21+
persist-credentials: false
22+
- name: Install Rust toolchain
23+
uses: dtolnay/rust-toolchain@stable
24+
- name: Run release-plz
25+
uses: release-plz/[email protected]
26+
with:
27+
command: release
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_TOKEN }}
31+
32+
# Create a PR with the new versions and changelog, preparing the next release.
33+
release-plz-pr:
34+
name: Release-plz PR
35+
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push' && github.event.workflow_run.head_branch == 'master' }}
36+
runs-on: ubuntu-latest
37+
permissions:
38+
contents: write
39+
pull-requests: write
40+
concurrency:
41+
group: release-plz-${{ github.ref }}
42+
cancel-in-progress: false
43+
steps:
44+
- name: Checkout repository
45+
uses: actions/checkout@v5
46+
with:
47+
fetch-depth: 0
48+
persist-credentials: false
49+
- name: Install Rust toolchain
50+
uses: dtolnay/rust-toolchain@stable
51+
- name: Run release-plz
52+
uses: release-plz/[email protected]
53+
with:
54+
command: release-pr
55+
env:
56+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
57+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_TOKEN }}

.gitignore

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,70 @@
11
# Generated by Cargo
22
# will have compiled files and executables
3-
debug
4-
target
3+
debug/
4+
target/
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
8+
Cargo.lock
59

610
# These are backup files generated by rustfmt
711
**/*.rs.bk
812

913
# MSVC Windows builds of rustc generate these, which store debugging information
1014
*.pdb
1115

12-
# Generated by cargo mutants
13-
# Contains mutation testing data
14-
**/mutants.out*/
16+
# IDE files
17+
.vscode/
18+
.idea/
19+
*.swp
20+
*.swo
21+
*~
22+
23+
# OS generated files
24+
.DS_Store
25+
.DS_Store?
26+
._*
27+
.Spotlight-V100
28+
.Trashes
29+
ehthumbs.db
30+
Thumbs.db
31+
32+
# Logs
33+
*.log
34+
35+
# Runtime data
36+
pids
37+
*.pid
38+
*.seed
39+
*.pid.lock
40+
41+
# Coverage directory used by tools like istanbul
42+
coverage/
43+
44+
# nyc test coverage
45+
.nyc_output
46+
47+
# Dependency directories
48+
node_modules/
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Output of 'npm pack'
57+
*.tgz
58+
59+
# Yarn Integrity file
60+
.yarn-integrity
61+
62+
# dotenv environment variables file
63+
.env
64+
.env.test
65+
.env.production
66+
.env.local
1567

16-
# RustRover
17-
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
18-
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
19-
# and can be added to the global gitignore or merged into this file. For a more nuclear
20-
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
21-
#.idea/
68+
# Temporary files
69+
*.tmp
70+
*.temp

Cargo.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[package]
2+
name = "redis-watcher"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "Redis watcher for Casbin-RS"
6+
license = "Apache-2.0"
7+
authors = ["Casbin Contributors"]
8+
homepage = "https://github.com/casbin-rs/redis-watcher"
9+
repository = "https://github.com/casbin-rs/redis-watcher"
10+
documentation = "https://docs.rs/redis-watcher"
11+
readme = "README.md"
12+
keywords = ["casbin", "redis", "watcher", "authorization", "access-control"]
13+
include = ["src/**/*", "examples/**/*", "README.md", "LICENSE", "Cargo.toml"]
14+
15+
[dependencies]
16+
casbin = { version = "2.13.0", features = ["watcher"] }
17+
redis = { version = "0.32.6", features = [
18+
"tokio-comp",
19+
"cluster-async",
20+
"aio",
21+
] }
22+
tokio = { version = "1.0", features = ["rt-multi-thread", "macros", "time"] }
23+
tokio-stream = "0.1"
24+
serde = { version = "1.0", features = ["derive"] }
25+
serde_json = "1.0"
26+
uuid = { version = "1.0", features = ["v4"] }
27+
log = "0.4"
28+
thiserror = "1.0"
29+
30+
[dev-dependencies]
31+
tokio-test = "0.4"
32+
env_logger = "0.10"

0 commit comments

Comments
 (0)