backend update
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"port": "8080",
|
||||
"db_url": "postgres://monitor:monitorpassword@localhost:5432/vdimonitor?sslmode=disable"
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
+40
-7
@@ -6,10 +6,41 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port string `json:"port"`
|
||||
DBUrl string `json:"db_url"`
|
||||
}
|
||||
|
||||
func loadConfig() Config {
|
||||
cfg := Config{
|
||||
Port: "8080",
|
||||
DBUrl: "postgres://monitor:monitorpassword@localhost:5432/vdimonitor?sslmode=disable",
|
||||
}
|
||||
|
||||
file, err := os.Open("config.json")
|
||||
if err == nil {
|
||||
defer file.Close()
|
||||
decoder := json.NewDecoder(file)
|
||||
if err := decoder.Decode(&cfg); err != nil {
|
||||
log.Printf("Warning: Failed to parse config.json: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if envPort := os.Getenv("PORT"); envPort != "" {
|
||||
cfg.Port = envPort
|
||||
}
|
||||
if envDB := os.Getenv("DB_URL"); envDB != "" {
|
||||
cfg.DBUrl = envDB
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
type MetricPayload struct {
|
||||
Hostname string `json:"hostname"`
|
||||
TotalCpuPercent float64 `json:"total_cpu_percent"`
|
||||
@@ -25,13 +56,10 @@ type MetricPayload struct {
|
||||
var db *sql.DB
|
||||
|
||||
func main() {
|
||||
dbUrl := os.Getenv("DB_URL")
|
||||
if dbUrl == "" {
|
||||
dbUrl = "postgres://monitor:monitorpassword@localhost:5432/vdimonitor?sslmode=disable"
|
||||
}
|
||||
cfg := loadConfig()
|
||||
|
||||
var err error
|
||||
db, err = sql.Open("postgres", dbUrl)
|
||||
db, err = sql.Open("postgres", cfg.DBUrl)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to db: %v", err)
|
||||
}
|
||||
@@ -39,8 +67,13 @@ func main() {
|
||||
|
||||
http.HandleFunc("/api/metrics", handleMetrics)
|
||||
|
||||
log.Println("Server starting on :8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
port := cfg.Port
|
||||
if !strings.HasPrefix(port, ":") {
|
||||
port = ":" + port
|
||||
}
|
||||
|
||||
log.Printf("Server starting on %s\n", port)
|
||||
log.Fatal(http.ListenAndServe(port, nil))
|
||||
}
|
||||
|
||||
func handleMetrics(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -16,6 +16,7 @@ services:
|
||||
ports:
|
||||
- "8081:8080"
|
||||
environment:
|
||||
PORT: "8080"
|
||||
DB_URL: postgres://monitor:monitorpassword@db:5432/vdimonitor?sslmode=disable
|
||||
depends_on:
|
||||
- db
|
||||
|
||||
Reference in New Issue
Block a user