Skip to content

Commit

Permalink
feat: add support for different watchpoints
Browse files Browse the repository at this point in the history
currently only the URL kind is implemented, but the foundations for more types are set.
  • Loading branch information
Data5tream committed Jul 25, 2023
1 parent 81aefa7 commit 8e39845
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion config_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ url = "redis://redis:6379/0"
[watcher]
interval = 30
watchlist = [
{ id = "serv", name = "Local dev", ip = "127.0.0.1", url = "http://localhost:8080/" }
{ id = "serv", name = "Local dev", kind = "url", target = "http://localhost:8080/" }
]

[log]
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/lib/components/StatusEntry.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
let statusMsg = '';
$: if (data) {
if (data.status < 600) {
if (data.status === 200) {
statusMsg = 'OK (200)';
} else if (data.status === 404) {
statusMsg = `Not found (${data.status})`;
} else if (data.status < 600) {
statusMsg = `${data.status}`;
} else if (data.status === 600) {
statusMsg = 'Invalid DNS';
Expand All @@ -20,7 +24,9 @@
<h4>{data.watchpoint.name}</h4>
<div class="content">
Status: <StatusIcon status={data.status} />&nbsp;{statusMsg}<br />
URL: <a href={data.watchpoint.url} class="url">{data.watchpoint.url}</a>
{#if data.watchpoint.kind === 'url'}
URL: <a href={data.watchpoint.target} class="url">{data.watchpoint.target}</a>
{/if}
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/dataprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export interface Watchpoint {
status: number;
watchpoint: {
id: string;
ip: string;
name: string;
url: string;
kind: string;
target: string;
};
}

Expand Down
72 changes: 37 additions & 35 deletions src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::get_config;
pub struct Watchpoint {
pub id: String,
name: String,
ip: String,
url: String,
kind: String,
target: String,
}

#[derive(Deserialize, Serialize)]
Expand All @@ -23,45 +23,47 @@ pub struct WatchpointStatus {

/// Check a watchpoints status and save it to redis
async fn check_watchpoint(wp: Watchpoint) {
debug!(" - Run watcher for {} - {}", wp.name, wp.ip);
debug!(" - Run watcher of type {} for {}", wp.kind, wp.name);

let mut con = get_redis_connection();

let res = reqwest::get(&wp.url).await;
match res {
Ok(response) => {
let _: () = con
.set(
format!("status:{}:status-code", wp.id),
response.status().as_u16(),
)
.unwrap();
}
Err(err) => {
// Standard catch-all error code
let mut status = 999;
if wp.kind == "target" {
let res = reqwest::get(&wp.target).await;
match res {
Ok(response) => {
let _: () = con
.set(
format!("status:{}:status-code", wp.id),
response.status().as_u16(),
)
.unwrap();
}
Err(err) => {
// Standard catch-all error code
let mut status = 999;

if err.is_connect() {
// Handle connection errors
let err_msg = err.to_string();
if err_msg.contains("dns error: failed to lookup address information: Name or service not known") {
// DNS lookup failed
status = 600;
} else if err_msg.contains("(unable to get local issuer certificate)") {
// TLS error
status = 601;
if err.is_connect() {
// Handle connection errors
let err_msg = err.to_string();
if err_msg.contains("dns error: failed to lookup address information: Name or service not known") {
// DNS lookup failed
status = 600;
} else if err_msg.contains("(unable to get local issuer certificate)") {
// TLS error
status = 601;
}
} else if err.is_status() {
// Handle standard HTTP status codes
status = err.status().unwrap().as_u16();
}
} else if err.is_status() {
// Handle standard HTTP status codes
status = err.status().unwrap().as_u16();
}

// Save status code to cache
let _: () = con
.set(format!("status:{}:status-code", wp.id), status)
.unwrap();
}
};
// Save status code to cache
let _: () = con
.set(format!("status:{}:status-code", wp.id), status)
.unwrap();
}
};
}
}

/// Get config from redis and ran watchpoints in parallel
Expand Down

0 comments on commit 8e39845

Please sign in to comment.