Skip to content

Commit

Permalink
fix refacto/optimisation errors and logs in httpServer and API Key ma…
Browse files Browse the repository at this point in the history
…nager
  • Loading branch information
Tetragramato committed Jun 23, 2021
1 parent caa170f commit a5bf962
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions internal/httpServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"net/http"
)

func Serve(repo SensorRepository) {
func Serve(repo SensorRepository) error {
http.HandleFunc(
"/sensors",
func(w http.ResponseWriter, r *http.Request) {
handleSensors(repo, w, r)
},
)
log.Fatal(http.ListenAndServe(Config.HttpPort, nil))
return http.ListenAndServe(Config.HttpPort, nil)
}

func handleSensors(repo SensorRepository, w http.ResponseWriter, r *http.Request) {
Expand Down
17 changes: 8 additions & 9 deletions internal/phosconApiKeyManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package internal

import (
"encoding/json"
"errors"
"fmt"
"github.com/dgraph-io/badger/v3"
"github.com/go-resty/resty/v2"
"log"
Expand Down Expand Up @@ -41,33 +41,32 @@ func NewApiKeyConfig(database Operable) *apiKeyConfig {
}
}

// RegisterApiKey TODO renvoyer les erreurs plutot que les logguer
func (config *apiKeyConfig) RegisterApiKey(gateway *Gateway) string {
func (config *apiKeyConfig) RegisterApiKey(gateway *Gateway) (string, error) {
log.Println("Getting API Key from DB...")
apiKey, err := config.database.Get(DbApiKey)
var tmpApiKey string
if err != nil {
//TODO faire une erreur perso pour eviter l'import de badger
if err == badger.ErrKeyNotFound {
log.Printf("Key not found for %s", DbApiKey)
log.Printf("API Key not found for %s", DbApiKey)
log.Println("Trying to get the API Key...")
jsonApiKey, err := config.getAndParseAPIKey(gateway)
if err != nil {
log.Fatal("Can't get API Key from Gateway", err)
return "", fmt.Errorf("can't get API Key from Gateway: %w", err)
}
tmpApiKey = jsonApiKey.Success.Username
log.Println("Trying to insert the API Key in DB...")
err = config.database.InsertOrUpdate(tmpApiKey, DbApiKey)
if err != nil {
log.Fatal("Can't insert API Key in DB", err)
return "", fmt.Errorf("can't insert API Key in DB: %w", err)
}
} else {
log.Fatal("Can't get and set apiKey", err)
return "", fmt.Errorf("can't get and set API Key: %w", err)
}
} else {
tmpApiKey = string(apiKey)
}
return tmpApiKey
return tmpApiKey, nil
}

func (config *apiKeyConfig) getAndParseAPIKey(gateway *Gateway) (*APIKey, error) {
Expand All @@ -77,7 +76,7 @@ func (config *apiKeyConfig) getAndParseAPIKey(gateway *Gateway) (*APIKey, error)
}

if retryCounter > CountRetry {
return nil, errors.New("fail to get APIKey : number of retries exceeded. Ensure you opened the Gateway to register a new application")
return nil, fmt.Errorf("number of retries exceeded (%v). Ensure you opened the Gateway to register a new application", CountRetry)
}

var parsedJson []interface{}
Expand Down
18 changes: 11 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func main() {
}

log.Println("Getting and setting API Key...")
apiKey := internal.NewApiKeyConfig(db).RegisterApiKey(gatewayResp)
apiKey, err := internal.NewApiKeyConfig(db).RegisterApiKey(gatewayResp)
if err != nil {
log.Fatal(err)
}
internal.Config.ApiKey = apiKey

sensorRepo := internal.NewSensorRepository(db)
Expand All @@ -31,17 +34,18 @@ func main() {
for {
listOfSensors, err := httpClient.GetAndParseSensors(gatewayResp)
if err != nil {
log.Fatal(err)
}
err = sensorRepo.SaveAll(listOfSensors)
if err != nil {
log.Fatal(err)
log.Printf("Error while retrieving sensors: %v", err.Error())
} else {
err = sensorRepo.SaveAll(listOfSensors)
if err != nil {
log.Printf("Error while saving sensors: %v", err.Error())
}
}
time.Sleep(internal.Config.DelayInSecond * time.Second)
}
},
func() {
internal.Serve(sensorRepo)
log.Fatal(internal.Serve(sensorRepo))
},
)
log.Println("Close GO-CONZ")
Expand Down

0 comments on commit a5bf962

Please sign in to comment.