Compare commits

...

7 Commits
v0.1.3 ... main

Author SHA1 Message Date
Camerin Figueroa 2030d2ad1e
Merge pull request #15 from RaspberryProgramming/test
Updated Cargo packages and added configurable cookie expiration
2025-01-24 19:51:32 -05:00
Camerin Figueroa e3a2e9d632 Cargo Updates 2025-01-24 16:32:54 -05:00
Camerin Figueroa a408e753a8 Added Cookie expiration option/environment variable
The default expiration is 52 weeks.
2025-01-24 15:53:39 -05:00
Camerin Figueroa 0394851e97
Merge pull request #13 from RaspberryProgramming/test
Remove Self hosted runner
2024-12-04 15:46:02 -05:00
Camerin Figueroa 5eda5d0604 Remove Self hosted runner 2024-12-04 15:23:23 -05:00
Camerin Figueroa e646772844
Merge pull request #12 from RaspberryProgramming/test
Added information on deployment with an ansible playbook
2024-10-21 11:46:05 -04:00
RaspberryProgramming e358f8b3a7 Added information on deployment with an ansible playbook 2024-07-25 00:22:18 +00:00
10 changed files with 425 additions and 262 deletions

View File

@ -9,7 +9,7 @@ on:
jobs:
build:
runs-on: self-hosted
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: docker build . -t raspberrypi99/request-mirror

View File

@ -8,7 +8,7 @@ on:
jobs:
build:
runs-on: self-hosted
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: docker build . -t raspberrypi99/request-mirror

View File

@ -11,9 +11,7 @@ env:
jobs:
build:
runs-on: self-hosted
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4

581
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,21 +6,22 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.34"
diesel = { version = "2.1.4", features = ["postgres", "chrono"] }
chrono = "0.4.39"
diesel = { version = "2.2.6", features = ["postgres", "chrono"] }
dotenvy = "0.15.7"
regex = "1.10.5"
regex = "1.11.1"
rocket = { version = "0.5.1", features = ["tls"] }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["full"] }
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.137"
tokio = { version = "1.43.0", features = ["full"] }
time = "0.3.37"
[dependencies.rocket_dyn_templates]
version = "0.2.0"
features = ["handlebars"]
[dependencies.uuid]
version = "1.7.0"
version = "1.12.1"
features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG

View File

@ -1,14 +1,14 @@
FROM rustlang/rust:nightly as builder
FROM rustlang/rust:nightly AS builder
WORKDIR /app
COPY src ./src
COPY Cargo.toml .
COPY Cargo.lock .
RUN cargo install --path .
FROM rustlang/rust:nightly as runner
FROM rustlang/rust:nightly AS runner
COPY migrations /migrations
RUN cargo install diesel_cli --no-default-features --features postgres
RUN apt update && apt install -y libpq-dev libc6
RUN apt update && apt upgrade -y && apt install -y libpq-dev libc6
COPY --from=builder /usr/local/cargo/bin/request-mirror /usr/local/bin/request-mirror
COPY ./templates /templates
COPY .env.docker /.env

View File

@ -118,3 +118,36 @@ Next, you can run the project using the following command. This can be run even
```bash
docker compose up -d
```
## Ansible Playbook
You can get access to an ansible playbook and associated files by checking out the following repository
[https://github.com/RaspberryProgramming/CamsAnsibleLibrary/](https://github.com/RaspberryProgramming/CamsAnsibleLibrary)
You can clone the repository
```bash
git clone https://github.com/RaspberryProgramming/CamsAnsibleLibrary
cd CamsAnsibleLibrary/Request\ Mirror/
```
Next, you can create your "inventory" file
```
[request-mirror-host]
10.1.2.3
```
If you haven't already, setup an ssh key so you can automatically log into your remote host as root. If you decide to use a user with sudo privileges you may need to modify the playbook on your own.
```bash
ansible-playbook request_mirror_deployment.yml -i inventory
```
If you need to enter a root password, you can add the -K argument
```bash
ansible-playbook request_mirror_deployment.yml -i inventory -K
```

View File

@ -17,6 +17,8 @@ services:
- 80:80
environment:
- DATABASE_URL=postgres://postgres:Password123@postgres/request_mirror_db
# Set COOKIE_EXPIRATION in weeks
# - COOKIE_EXPIRATION=52
depends_on:
postgres:
condition: service_healthy

View File

@ -47,6 +47,38 @@ pub fn establish_connection() -> PgConnection {
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
}
/// returns default cookie expiration in weeks
///
/// This value can be set by modifying an environment variable or setting in the .env file.
///
/// Example:
///
/// ```rust,ignore
/// use request_mirror::establish_connection;
///
/// let connection = establish_connection();
/// ```
pub fn cookie_expiration() -> i64 {
let key: &str = "COOKIE_EXPIRATION";
let expiration: i64;
match env::var(key) {
Ok(val) => {
match val.parse::<i64>() {
Ok(v) => expiration = v,
Err(e) => expiration = 52
}
},
Err(_e) => {
dotenv().ok();
expiration = 52;
}
}
expiration
}
/// Used to create a new client in the database. You need to pass a connection, the ip and client_id
///
/// Example:

View File

@ -9,6 +9,7 @@ use rocket::outcome::Outcome;
use rocket_dyn_templates::Template;
use serde::Serialize;
use rocket::http::{Cookie, CookieJar, Status};
use time::{Duration, OffsetDateTime};
use uuid::Uuid;
use request_mirror::models::*;
use diesel::prelude::*;
@ -61,7 +62,14 @@ impl<'r> FromRequest<'r> for RequestInfo {
let new_uuid = Uuid::new_v4().to_string();
println!("Creating new cookie");
req_cookies.add(Cookie::new("mirror-id", new_uuid.clone()));
let mut cookie = Cookie::new("mirror-id", new_uuid.clone());
let mut now = OffsetDateTime::now_utc();
now += Duration::weeks(cookie_expiration()); // Default expiration is 52 weeks
cookie.set_expires(now);
req_cookies.add(cookie);
let address = if req.client_ip().is_some() {
req.client_ip().unwrap().to_string()