Skip to content

Commit

Permalink
Merge pull request #3 from JacquesBeets:feature/sqlite-2
Browse files Browse the repository at this point in the history
Feature/sqlite-2
  • Loading branch information
JacquesBeets authored Oct 6, 2024
2 parents 69360f2 + 1719ea9 commit f54208d
Show file tree
Hide file tree
Showing 18 changed files with 671 additions and 80 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/build_and_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: 1.21


- name: Install MinGW-w64
run: |
choco install mingw -y
echo "C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
- name: Build Go binary
run: |
cd backend
$env:CGO_ENABLED=1
go build -o ../WinSenseConnect.exe
- name: Set up Node.js
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# /frontend
/scripts/*.ps1
!scripts/test_notification.ps1
/data/**
*.exe
*.log
*.txt
Expand Down
58 changes: 15 additions & 43 deletions backend/config.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,31 @@
package main

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)

type Config struct {
BrokerAddress string `json:"broker_address"`
Username string `json:"username"`
Password string `json:"password"`
ClientID string `json:"client_id"`
Topic string `json:"topic"`
LogLevel string `json:"log_level"`
ScriptTimeout int `json:"script_timeout"`
Commands map[string]ScriptConfig `json:"commands"`
SensorConfig SensorConfig `json:"sensor_config"`
}

type ScriptConfig struct {
ScriptPath string `json:"script_path"`
RunAsUser bool `json:"run_as_user"`
}
type SensorConfig struct {
Enabled bool `json:"enabled"`
Interval int `json:"interval"`
SensorTopic string `json:"sensor_topic"`
ID int64 `json:"id"`
BrokerAddress string `json:"broker_address"`
Username string `json:"username"`
Password string `json:"password"`
ClientID string `json:"client_id"`
Topic string `json:"topic"`
LogLevel string `json:"log_level"`
ScriptTimeout int `json:"script_timeout"`
SensorConfigEnabled bool `json:"sensor_config_enabled"`
Commands map[string]ScriptConfig `json:"commands"`
Sensors map[string]SensorConfig `json:"sensors"`
}

func (p *program) loadConfig() error {
p.logger.Debug("Starting to load config...")
exePath, err := os.Executable()
if err != nil {
p.logger.Error(fmt.Sprintf("Failed to get executable path: %v", err))
return fmt.Errorf("failed to get executable path: %v", err)
}
p.logger.Debug(fmt.Sprintf("Executable path: %s", exePath))

configPath := filepath.Join(filepath.Dir(exePath), "config.json")
p.logger.Debug(fmt.Sprintf("Config path: %s", configPath))

file, err := os.Open(configPath)
conf, err := p.db.GetConfig()
if err != nil {
p.logger.Error(fmt.Sprintf("Failed to open config file: %v", err))
return fmt.Errorf("failed to open config file: %v", err)
p.logger.Error(fmt.Sprintf("Failed to get config: %v", err))
return err
}
defer file.Close()

decoder := json.NewDecoder(file)
if err := decoder.Decode(&p.config); err != nil {
p.logger.Error(fmt.Sprintf("Failed to decode config: %v", err))
return fmt.Errorf("failed to decode config: %v", err)
}

p.config = *conf
p.logger.Debug("Config loaded successfully")
return nil
}
Loading

0 comments on commit f54208d

Please sign in to comment.