Initial support for SQL database
parent
b6567e2ef1
commit
e26bb3cc0f
|
@ -0,0 +1 @@
|
||||||
|
jilo-server
|
10
README.md
10
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.
|
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):
|
Run it (mainly used for tests):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go run main.go
|
CGO_ENABLED=1 go run main.go
|
||||||
```
|
```
|
||||||
|
|
||||||
Build the agent:
|
Build the agent:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build -o jilo-server main.go
|
CGO_ENABLED=1 go build -o jilo-server main.go
|
||||||
```
|
```
|
||||||
|
|
||||||
## configuration
|
## configuration
|
||||||
|
|
43
main.go
43
main.go
|
@ -1,12 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "database/sql"
|
"database/sql"
|
||||||
// "fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
// "os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
@ -43,6 +41,30 @@ func readConfig(filePath string) Config {
|
||||||
return 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) {
|
func checkEndpoint(endpoint string) (int, int64) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
resp, err := http.Get(endpoint)
|
resp, err := http.Get(endpoint)
|
||||||
|
@ -56,9 +78,24 @@ func checkEndpoint(endpoint string) (int, int64) {
|
||||||
return resp.StatusCode, elapsed
|
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() {
|
func main() {
|
||||||
|
// config file
|
||||||
config := readConfig("jilo-server.conf")
|
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)
|
ticker := time.NewTicker(time.Duration(config.CheckPeriod) * time.Minute)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue