This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
forked from lyc8503/UptimeFlare
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
113 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
type MonitorState = { | ||
version: number | ||
lastUpdate: number | ||
overallUp: number | ||
overallDown: number | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
export class UptimeFlareStateDb<T> { | ||
private stateExist = false | ||
|
||
constructor(private db: D1Database) {} | ||
|
||
async get(): Promise<T | null> { | ||
const result = await this.db | ||
.prepare("SELECT * FROM kv WHERE key = 'state'") | ||
.first<{ key: 'state'; value: string }>() | ||
|
||
this.stateExist = !!result | ||
|
||
return result ? this.safeJsonParse(result.value) : null | ||
} | ||
|
||
async set(value: T) { | ||
if (this.stateExist) { | ||
await this.db | ||
.prepare("UPDATE kv SET value = ? WHERE key = 'state'") | ||
.bind(JSON.stringify(value)) | ||
.run() | ||
} else { | ||
await this.db | ||
.prepare("INSERT INTO kv (key, value) VALUES ('state', ?)") | ||
.bind(JSON.stringify(value)) | ||
.run() | ||
this.stateExist = true | ||
} | ||
} | ||
|
||
private safeJsonParse(str: string) { | ||
try { | ||
return JSON.parse(str) | ||
} catch { | ||
return null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- DROP TABLE IF EXISTS kv; | ||
CREATE TABLE IF NOT EXISTS kv (key TEXT PRIMARY KEY, value TEXT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
name = "uptimeflare_worker" | ||
main = "src/index.ts" | ||
compatibility_date = "2023-11-08" | ||
kv_namespaces = [{ binding = "UPTIMEFLARE_STATE", id = "UPTIMEFLARE_STATE" }] | ||
|
||
[[d1_databases]] | ||
binding = "UPTIMEFLARE_DB" | ||
database_name = "uptimeflare-db" | ||
database_id = "uptimeflare-db-dev" |