Skip to content

Commit 0803d61

Browse files
authored
Init Github actions
1 parent adfbb16 commit 0803d61

File tree

7 files changed

+105
-25
lines changed

7 files changed

+105
-25
lines changed

.github/workflows/format.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: "Run Format"
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
# Check formatting with rustfmt
10+
jobs:
11+
format:
12+
name: cargo fmt
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Cache cargo dependencies
18+
uses: actions/cache@v4
19+
with:
20+
path: |
21+
~/.cargo/bin/
22+
~/.cargo/registry/index/
23+
~/.cargo/registry/cache/
24+
~/.cargo/git/db/
25+
target/
26+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
27+
28+
- name: Set up rust toolchain
29+
uses: actions-rust-lang/setup-rust-toolchain@v1
30+
with:
31+
components: "clippy, rustfmt"
32+
toolchain: 1.83
33+
34+
- name: Add wasm32-wasi target
35+
run: rustup target add wasm32-wasip1
36+
37+
- name: Run cargo fmt
38+
uses: actions-rust-lang/rustfmt@v1
39+
40+
- name: Run cargo clipply
41+
run: cargo clippy --all-targets --all-features
42+

.github/workflows/test.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: "Run Tests"
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
name: cargo test
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Cache Cargo dependencies
17+
uses: actions/cache@v4
18+
with:
19+
path: |
20+
~/.cargo/bin/
21+
~/.cargo/registry/index/
22+
~/.cargo/registry/cache/
23+
~/.cargo/git/db/
24+
target/
25+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
26+
27+
- name: Set up Rust tool chain
28+
uses: actions-rust-lang/setup-rust-toolchain@v1
29+
with:
30+
toolchain: 1.83
31+
32+
- name: Add wasm32-wasi target
33+
run: rustup target add wasm32-wasip1
34+
35+
- name: Setup Viceroy
36+
run: cargo install viceroy
37+
38+
- name: Run tests
39+
run: cargo test

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ edition = "2021"
77
# Otherwise, `publish = false` prevents an accidental `cargo publish` from revealing private source.
88
publish = false
99
license = "Apache-2.0"
10-
license-file = "LICENSE"
1110

1211
[profile.release]
1312
debug = 1

src/cookies.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use cookie::{Cookie, CookieJar};
22
use fastly::http::header;
33
use fastly::Request;
4-
use log;
54

6-
const COOKIE_MAX_AGE: i32 = 1 * 365 * 24 * 60 * 60; // 1 year
5+
const COOKIE_MAX_AGE: i32 = 365 * 24 * 60 * 60; // 1 year
76

87
// return empty cookie jar for unparsable cookies
98
pub fn parse_cookies_to_jar(s: &str) -> CookieJar {
@@ -80,8 +79,8 @@ mod tests {
8079

8180
#[test]
8281
fn test_handle_request_cookies() {
83-
let mut req = Request::get("http://example.com").with_header(header::COOKIE, "c1=v1;c2=v2");
84-
let jar = handle_request_cookies(&mut req).unwrap();
82+
let req = Request::get("http://example.com").with_header(header::COOKIE, "c1=v1;c2=v2");
83+
let jar = handle_request_cookies(&req).unwrap();
8584

8685
assert!(jar.iter().count() == 2);
8786
assert_eq!(jar.get("c1").unwrap().value(), "v1");
@@ -90,24 +89,24 @@ mod tests {
9089

9190
#[test]
9291
fn test_handle_request_cookies_with_empty_cookie() {
93-
let mut req = Request::get("http://example.com").with_header(header::COOKIE, "");
94-
let jar = handle_request_cookies(&mut req).unwrap();
92+
let req = Request::get("http://example.com").with_header(header::COOKIE, "");
93+
let jar = handle_request_cookies(&req).unwrap();
9594

9695
assert!(jar.iter().count() == 0);
9796
}
9897

9998
#[test]
10099
fn test_handle_request_cookies_no_cookie_header() {
101-
let mut req: Request = Request::get("https://example.com");
102-
let jar = handle_request_cookies(&mut req);
100+
let req: Request = Request::get("https://example.com");
101+
let jar = handle_request_cookies(&req);
103102

104103
assert!(jar.is_none());
105104
}
106105

107106
#[test]
108107
fn test_handle_request_cookies_invalid_cookie_header() {
109-
let mut req = Request::get("http://example.com").with_header(header::COOKIE, "invalid");
110-
let jar = handle_request_cookies(&mut req).unwrap();
108+
let req = Request::get("http://example.com").with_header(header::COOKIE, "invalid");
109+
let jar = handle_request_cookies(&req).unwrap();
111110

112111
assert!(jar.iter().count() == 0);
113112
}

src/main.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,22 @@ fn handle_ad_request(settings: &Settings, req: Request) -> Result<Response, Erro
129129
println!("Fetching current count for synthetic ID: {}", synthetic_id);
130130
let current_count: i32 = store
131131
.lookup(&synthetic_id)
132-
.and_then(|mut val| {
132+
.map(|mut val| {
133133
// Convert LookupResponse to bytes first
134134
match String::from_utf8(val.take_body_bytes()) {
135135
Ok(s) => {
136136
println!("Value from KV store: {}", s);
137-
Ok(Some(s))
137+
Some(s)
138138
}
139139
Err(e) => {
140140
println!("Error converting bytes to string: {}", e);
141-
Ok(None)
141+
None
142142
}
143143
}
144144
})
145-
.and_then(|opt_s| {
145+
.map(|opt_s| {
146146
println!("Parsing string value: {:?}", opt_s);
147-
Ok(opt_s.and_then(|s| s.parse().ok()))
147+
opt_s.and_then(|s| s.parse().ok())
148148
})
149149
.unwrap_or_else(|_| {
150150
println!("No existing count found, starting at 0");
@@ -163,7 +163,10 @@ fn handle_ad_request(settings: &Settings, req: Request) -> Result<Response, Erro
163163
println!("Synthetic ID {} visit count: {}", synthetic_id, new_count);
164164

165165
// Construct URL with synthetic ID
166-
let ad_server_url = settings.ad_server.sync_url.replace("{{synthetic_id}}", &synthetic_id);
166+
let ad_server_url = settings
167+
.ad_server
168+
.sync_url
169+
.replace("{{synthetic_id}}", &synthetic_id);
167170

168171
println!("Sending request to backend: {}", ad_server_url);
169172

src/prebid.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use fastly::http::{header, Method};
22
use fastly::{Error, Request, Response};
33
use serde_json::json;
4-
use url;
54

65
use crate::constants::{SYNTH_HEADER_FRESH, SYNTH_HEADER_POTSI};
76
use crate::settings::Settings;

src/synthetic.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use fastly::http::header;
22
use fastly::Request;
33
use hmac::{Hmac, Mac};
4-
use log;
54
use sha2::Sha256;
65

76
use crate::constants::SYNTH_HEADER_POTSI;
@@ -65,7 +64,7 @@ pub fn get_or_generate_synthetic_id(settings: &Settings, req: &Request) -> Strin
6564
return potsi;
6665
}
6766

68-
let req_cookie_jar: Option<cookie::CookieJar> = handle_request_cookies(&req);
67+
let req_cookie_jar: Option<cookie::CookieJar> = handle_request_cookies(req);
6968
match req_cookie_jar {
7069
Some(jar) => {
7170
let potsi_cookie = jar.get("synthetic_id");
@@ -121,11 +120,11 @@ mod tests {
121120
fn test_generate_synthetic_id() {
122121
let settings: Settings = create_settings();
123122
let req = create_test_request(vec![
124-
(&header::USER_AGENT.to_string(), "Mozilla/5.0"),
125-
(&header::COOKIE.to_string(), "pub_userid=12345"),
123+
(header::USER_AGENT.as_ref(), "Mozilla/5.0"),
124+
(header::COOKIE.as_ref(), "pub_userid=12345"),
126125
("X-Pub-User-ID", "67890"),
127-
(&header::HOST.to_string(), "example.com"),
128-
(&header::ACCEPT_LANGUAGE.to_string(), "en-US,en;q=0.9"),
126+
(header::HOST.as_ref(), "example.com"),
127+
(header::ACCEPT_LANGUAGE.as_ref(), "en-US,en;q=0.9"),
129128
]);
130129

131130
let synthetic_id = generate_synthetic_id(&settings, &req);
@@ -148,7 +147,7 @@ mod tests {
148147
fn test_get_or_generate_synthetic_id_with_cookie() {
149148
let settings = create_settings();
150149
let req = create_test_request(vec![(
151-
&header::COOKIE.to_string(),
150+
header::COOKIE.as_ref(),
152151
"synthetic_id=existing_cookie_id",
153152
)]);
154153

0 commit comments

Comments
 (0)