Skip to content

Commit

Permalink
config file management + log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
halacs committed Oct 31, 2024
1 parent 474380a commit 85cf830
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion fmb920/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *Server) sendCommandToDevice(imei string) error {

log.Infof("Command has been sent: %s", commandStr)
default:
log.Tracef("No command to be sent for this remote endpoint, for this device.")
log.Tracef("No command to be sent for this device")
}

return nil
Expand Down
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func parseConfig() *config.Config {
log := config.NewLogger()

// Read configuration
viper.SetConfigName("cfg") // Name of cfg file (without extension)
viper.SetConfigName("config") // Name of cfg file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the cfg file does not have the extension in the name
viper.AddConfigPath(fmt.Sprintf("/etc/%s/", config.AppName)) // path to look for the cfg file in
viper.AddConfigPath(fmt.Sprintf("$HOME/.%s/", config.AppName)) // call multiple times to add many search paths
Expand All @@ -35,8 +35,8 @@ func parseConfig() *config.Config {
err := viper.ReadInConfig() // Find and read the cfg file
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Infof("Config file was not found. Using defaults.")
} else {
log.Fatalf("Failed to parse cfg file. %v", err)
} else if err != nil {
log.Errorf("Failed to parse cfg file. %v", err)
}

// General configs
Expand Down Expand Up @@ -103,6 +103,15 @@ func parseConfig() *config.Config {
BasePath: viper.GetString(config.UdsServerConfigBasePath),
}

err = viper.SafeWriteConfig()
if _, ok := err.(viper.ConfigFileAlreadyExistsError); ok {
log.Tracef("Config file already exists. %v", err)
} else if err != nil {
log.Errorf("Failed to write config file. %v", err)
} else {
log.Debug("Config has been file created")
}

cfg := config.NewConfig(log, influxConfig, teltonikaConfig, metricsConfig, udsServerConfig)
return cfg
}
Expand Down
16 changes: 0 additions & 16 deletions package/debian/etc/haltonika/config.env

This file was deleted.

14 changes: 14 additions & 0 deletions package/debian/etc/haltonika/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
database: haltonika
debug: true
imeilist: 111111111111111,222222222222222
listenip: 0.0.0.0
listenport: 9160
measurement: gps
metricsip: 0.0.0.0
metricsport: 9161
mp: haltonika.met
password: "123"
udsbasepath: /var/run/haltonika/
url: http://localhost:8086
username: haltonika
verbose: true
1 change: 0 additions & 1 deletion package/debian/lib/systemd/system/haltonika.service
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Restart=always
#ReadWritePaths=/etc/haltonika/ /var/run/haltonika/
WorkingDirectory=/etc/haltonika/
PrivateTmp=true
EnvironmentFile=-/etc/haltonika/config.env
TimeoutSec=120

[Install]
Expand Down

0 comments on commit 85cf830

Please sign in to comment.