From e26bb3cc0fd34ef510f91bf9af8152b041134fb1 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Sun, 13 Oct 2024 20:06:36 +0300 Subject: [PATCH] Initial support for SQL database --- .gitignore | 1 + README.md | 10 ++++++++-- main.go | 45 +++++++++++++++++++++++++++++++++++++++++---- meet.db | Bin 0 -> 12288 bytes 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 .gitignore create mode 100644 meet.db diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..10297ce --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +jilo-server diff --git a/README.md b/README.md index 6a63426..5bf6e22 100644 --- a/README.md +++ b/README.md @@ -12,16 +12,22 @@ This project is licensed under the GNU General Public License version 2 (GPL-2.0 Clone the git repo. Either run the server with Go or build it and run the executable. +Dependencies: + +```bash +apt install gcc sqlite3 libsqlite3-dev +``` + Run it (mainly used for tests): ```bash -go run main.go +CGO_ENABLED=1 go run main.go ``` Build the agent: ```bash -go build -o jilo-server main.go +CGO_ENABLED=1 go build -o jilo-server main.go ``` ## configuration diff --git a/main.go b/main.go index b386b1e..540a87c 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,10 @@ package main import ( -// "database/sql" -// "fmt" + "database/sql" "io/ioutil" "log" "net/http" -// "os" "time" _ "github.com/mattn/go-sqlite3" @@ -43,11 +41,35 @@ func readConfig(filePath string) Config { return config } +func setupDatabase(dbPath string) (*sql.DB, error) { + + // Open the database + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, err + } + + // If the table is not there, initialize it + createTable := ` + CREATE TABLE IF NOT EXISTS endpoint_data ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, + status_code INTEGER, + response_time_ms INTEGER + );` + _, err = db.Exec(createTable) + if err != nil { + return nil, err + } + + return db, nil +} + func checkEndpoint(endpoint string) (int, int64) { start := time.Now() resp, err := http.Get(endpoint) if err != nil { - log.Println("Failed to check the endpoint: ", err) + log.Println("Failed to check the endpoint:", err) return 0, 0 } defer resp.Body.Close() @@ -56,9 +78,24 @@ func checkEndpoint(endpoint string) (int, int64) { return resp.StatusCode, elapsed } +func saveData(db *sql.DB, statusCode int, responseTime int64) { + _, err := db.Exec("INSERT INTO endpoint_data (status_code, response_time_ms) VALUES (?, ?)", statusCode, responseTime) + if err != nil { + log.Println("Failed to insert data into the database:", err) + } +} + func main() { + // config file config := readConfig("jilo-server.conf") + // Connect to or setup the database + db, err := setupDatabase(config.DatabasePath) + if err != nil { + log.Fatal("Failed to initialize the database:", err) + } + defer db.Close() + ticker := time.NewTicker(time.Duration(config.CheckPeriod) * time.Minute) defer ticker.Stop() diff --git a/meet.db b/meet.db new file mode 100644 index 0000000000000000000000000000000000000000..a6613233247b038b9dfa557e2d18b1257e8290a5 GIT binary patch literal 12288 zcmeI#&r8EF6bJBR69vH^2(s(DZDYfW|A2LiQf%w&(!)-rt`UlByDizpoA{6Ur zt&OXPW9j&#?JB#%taLmrQ&k_fom?tCFAJ^WYVSDLb;bJ3Zq=LHrXK+T2tWV=5P$## sAOHafKmY;|fWQF@p#MMMpNmg}00bZa0SG_<0uX=z1Rwwb2y6s?0Eu#HdjJ3c literal 0 HcmV?d00001