Skip to content

Commit

Permalink
Improve library with logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bb4L committed Jun 25, 2021
1 parent e505cae commit e91cea7
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 73 deletions.
42 changes: 22 additions & 20 deletions api/alarm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package api contains the data / helper functions to interact with the api
package api

import (
Expand All @@ -9,10 +10,11 @@ import (
"net/http"
"strconv"

"github.com/bb4L/rpi-radio-alarm-go-library/logging"
"github.com/bb4L/rpi-radio-alarm-go-library/types"
)

// GetAlarms get all alarms
// Get all alarms
func (helper *Helper) GetAlarms() ([]types.Alarm, error) {
url := helper.AlarmURL + "/alarm"
req, err := http.NewRequest("GET", url, nil)
Expand All @@ -21,10 +23,10 @@ func (helper *Helper) GetAlarms() ([]types.Alarm, error) {
}

res, err := helper.prepareAndDoRequest(req)
helper.Logger.Println(res)
logging.GetInfoLogger().Println(res)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return nil, err
}

Expand All @@ -38,14 +40,14 @@ func (helper *Helper) GetAlarms() ([]types.Alarm, error) {
err = json.Unmarshal(jsonData, &data)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return nil, err
}

return data, nil
}

// GetAlarm get a specific alarm
// Get a specific alarm by index
func (helper *Helper) GetAlarm(idx int) (types.Alarm, error) {
url := helper.AlarmURL + "/alarm/" + strconv.Itoa(idx)
req, err := http.NewRequest("GET", url, nil)
Expand All @@ -54,10 +56,10 @@ func (helper *Helper) GetAlarm(idx int) (types.Alarm, error) {
}

res, err := helper.prepareAndDoRequest(req)
helper.Logger.Println(res)
logging.GetInfoLogger().Println(res)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return types.Alarm{}, err
}

Expand All @@ -71,14 +73,14 @@ func (helper *Helper) GetAlarm(idx int) (types.Alarm, error) {
err = json.Unmarshal(jsonData, &data)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return types.Alarm{}, err
}

return data, nil
}

// ChangeAlarm change the alarm on the fiven index with the data of the passed instance
// Change the alarm on the given index with the data of the passed instance
func (helper *Helper) ChangeAlarm(alarm types.Alarm, idx int) (types.Alarm, error) {
url := helper.AlarmURL + "/alarm/" + strconv.Itoa(idx)

Expand All @@ -93,10 +95,10 @@ func (helper *Helper) ChangeAlarm(alarm types.Alarm, idx int) (types.Alarm, erro
}

res, err := helper.prepareAndDoRequest(req)
helper.Logger.Println(res)
logging.GetInfoLogger().Println(res)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return types.Alarm{}, err
}

Expand All @@ -110,14 +112,14 @@ func (helper *Helper) ChangeAlarm(alarm types.Alarm, idx int) (types.Alarm, erro
err = json.Unmarshal(jsonData, &data)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return types.Alarm{}, fmt.Errorf("could not unmarshal result")
}

return data, nil
}

// AddAlarm Adds the given alarm
// Adds the given alarm
func (helper *Helper) AddAlarm(alarm types.Alarm) ([]types.Alarm, error) {
url := helper.AlarmURL + "/alarm"

Expand All @@ -133,9 +135,9 @@ func (helper *Helper) AddAlarm(alarm types.Alarm) ([]types.Alarm, error) {
}

res, err := helper.prepareAndDoRequest(req)
helper.Logger.Println(res)
logging.GetInfoLogger().Println(res)
if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return nil, err
}

Expand All @@ -148,14 +150,14 @@ func (helper *Helper) AddAlarm(alarm types.Alarm) ([]types.Alarm, error) {
var data []types.Alarm
err = json.Unmarshal(jsonData, &data)
if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return nil, err
}

return data, nil
}

// DeleteAlarm Delete the alarm with the given index
// Delete the alarm with the given index
func (helper *Helper) DeleteAlarm(idx int) ([]types.Alarm, error) {
url := helper.AlarmURL + "/alarm/" + strconv.Itoa(idx)

Expand All @@ -165,9 +167,9 @@ func (helper *Helper) DeleteAlarm(idx int) ([]types.Alarm, error) {
}

res, err := helper.prepareAndDoRequest(req)
helper.Logger.Println(res)
logging.GetInfoLogger().Println(res)
if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return nil, err
}

Expand All @@ -180,7 +182,7 @@ func (helper *Helper) DeleteAlarm(idx int) ([]types.Alarm, error) {
var data []types.Alarm
err = json.Unmarshal(jsonData, &data)
if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return nil, err
}

Expand Down
7 changes: 2 additions & 5 deletions api/helper.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// Package api contains the data / helper functions to interact with the api
package api

import (
"fmt"
"log"
"net/http"
)

// Helper A struct to make the api accessible
// Struct to define the api settings being used
type Helper struct {
AlarmURL string
ExtraHeader string
ExtreaHeaderValue string
Logger *log.Logger
}

// CheckHealth checks wether the api is reachable and returns 200 on the health endpoint
// Checks wether the api is reachable and returns 200 on the health endpoint
func (helper *Helper) CheckHealth() error {
url := helper.AlarmURL + "/health"
req, err := http.NewRequest("GET", url, nil)
Expand Down
25 changes: 13 additions & 12 deletions api/radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"io/ioutil"
"net/http"

"github.com/bb4L/rpi-radio-alarm-go-library/logging"
"github.com/bb4L/rpi-radio-alarm-go-library/types"
)

// GetRadio returns the radio status
// Get the radio status
func (helper *Helper) GetRadio() (types.Radio, error) {
url := helper.AlarmURL + "/radio"
req, err := http.NewRequest("GET", url, nil)
Expand All @@ -19,10 +20,10 @@ func (helper *Helper) GetRadio() (types.Radio, error) {
}

res, err := helper.prepareAndDoRequest(req)
helper.Logger.Println(res)
logging.GetInfoLogger().Println(res)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return types.Radio{}, err
}

Expand All @@ -36,14 +37,14 @@ func (helper *Helper) GetRadio() (types.Radio, error) {
err = json.Unmarshal(jsonData, &data)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return types.Radio{}, err
}

return data, nil
}

// StartRadio starts the radio
// Start the radio
func (helper *Helper) StartRadio() (types.Radio, error) {
values := map[string]string{"switch": "on"}
jsonData, _ := json.Marshal(values)
Expand All @@ -55,10 +56,10 @@ func (helper *Helper) StartRadio() (types.Radio, error) {
}

res, err := helper.prepareAndDoRequest(req)
helper.Logger.Println(res)
logging.GetInfoLogger().Println(res)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return types.Radio{}, err
}

Expand All @@ -72,14 +73,14 @@ func (helper *Helper) StartRadio() (types.Radio, error) {
err = json.Unmarshal(jsonData, &data)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return types.Radio{}, err
}

return data, nil
}

// StopRadio stops the radio
// Stop the radio
func (helper *Helper) StopRadio() (types.Radio, error) {
values := map[string]string{"switch": "off"}
jsonData, _ := json.Marshal(values)
Expand All @@ -91,10 +92,10 @@ func (helper *Helper) StopRadio() (types.Radio, error) {
}

res, err := helper.prepareAndDoRequest(req)
helper.Logger.Println(res)
logging.GetInfoLogger().Println(res)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return types.Radio{}, err
}

Expand All @@ -108,7 +109,7 @@ func (helper *Helper) StopRadio() (types.Radio, error) {
err = json.Unmarshal(jsonData, &data)

if err != nil {
helper.Logger.Println(err)
logging.GetErrorLogger().Println(err)
return types.Radio{}, err
}

Expand Down
21 changes: 0 additions & 21 deletions constants/constants.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/bb4L/rpi-radio-alarm-go-library

go 1.15
go 1.16

require github.com/joho/godotenv v1.3.0
47 changes: 47 additions & 0 deletions logging/loggers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Package logging contains some logging utils
package logging

import (
"log"
"os"
"sync"
)

const commonPrefix = "[rpi-radio-alarm-library]\t"

var (
// InfoLogger logging infos
InfoLogger *log.Logger

// ErrorLogger logging errors
ErrorLogger *log.Logger

// FatalLogger logging fatal
FatalLogger *log.Logger

once sync.Once
)

// Get the InfoLogger
func GetInfoLogger() *log.Logger {
once.Do(func() {
InfoLogger = log.New(os.Stderr, commonPrefix+"INFO: ", log.Ldate|log.Ltime|log.Lmsgprefix)
})
return InfoLogger
}

// Get the ErrorLogger
func GetErrorLogger() *log.Logger {
once.Do(func() {
ErrorLogger = log.New(os.Stderr, commonPrefix+"ERROR: ", log.Ldate|log.Ltime|log.Lmsgprefix)
})
return ErrorLogger
}

// Get the FatalLogger
func GetFatalLogger() *log.Logger {
once.Do(func() {
FatalLogger = log.New(os.Stderr, commonPrefix+"FATAL: ", log.Ldate|log.Ltime|log.Lmsgprefix)
})
return FatalLogger
}
19 changes: 7 additions & 12 deletions rpi-radio-alarm-go-library.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
// Package rpiradioalarmgolibrary contains some main helper functions
// Package rpiradioalarmgolibrary contains main helper functions
package rpiradioalarmgolibrary

import (
"os"

"github.com/bb4L/rpi-radio-alarm-go-library/api"
"github.com/bb4L/rpi-radio-alarm-go-library/constants"
"github.com/bb4L/rpi-radio-alarm-go-library/logging"
"github.com/joho/godotenv"
)

// GetHelperFromEnv returns the working helper object with the config loaded from a .env file
// Returns the working helper object with the config loaded from a .env file
func GetHelperFromEnv() api.Helper {

if constants.RpiLibraryLogger == nil {
constants.RpiLibraryLogger = constants.GetLogger()
}
logger := constants.RpiLibraryLogger

err := godotenv.Load()
if err != nil {
logger.Fatal("Error loading .env file")
logging.GetFatalLogger().Fatal("error loading .env file")
}

if os.Getenv("ALARMURL") == "" {
logger.Fatal("You have to specify ALARMURL in the .env")
logging.GetFatalLogger().Fatal("you have to specify ALARMURL in the .env")
}

helper := api.Helper{AlarmURL: os.Getenv("ALARMURL"), ExtraHeader: os.Getenv("EXTRAHEADER"), ExtreaHeaderValue: os.Getenv("EXTRAHEADERVALUE"), Logger: logger}
helper := api.Helper{AlarmURL: os.Getenv("ALARMURL"), ExtraHeader: os.Getenv("EXTRAHEADER"), ExtreaHeaderValue: os.Getenv("EXTRAHEADERVALUE")}

err = helper.CheckHealth()
if err != nil {
logger.Fatalf("Health check failed with: %s", err)
logging.GetFatalLogger().Fatalf("health check failed with: %s", err)
}

return helper
Expand Down
Loading

0 comments on commit e91cea7

Please sign in to comment.