From f6a700413707efdcc1eca92848335f121f02e1a1 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 22 Feb 2022 13:05:33 +0100 Subject: [PATCH] =?UTF-8?q?Chargement=20du=20fichier=20de=20config=20fonct?= =?UTF-8?q?ionnelle.=20Reste=20=C3=A0=20utiliser=20ses=20donn=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 11 +++++++++++ pkg/config/AppConfig.go | 21 +++------------------ pkg/config/JsonConfigReader.go | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 pkg/config/JsonConfigReader.go diff --git a/cmd/main.go b/cmd/main.go index ef8b308..0526c46 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,6 +1,7 @@ package main import ( + "_StorBackEnd/pkg/config" "_StorBackEnd/pkg/network" "_StorBackEnd/pkg/protocol/managers" "_StorBackEnd/pkg/protocol/repository" @@ -9,6 +10,7 @@ import ( ) const ( + FILE_PATH = "resources/AppConfig.json" MULTICAST_ADDRESS = "226.66.66.1:42500" MULTICAST_SECOND = 10 // TODO : Changer en 30 secondes UNICAST_ADDRESS = "0.0.0.0:58000" @@ -17,6 +19,15 @@ const ( func main() { println("StorBackEnd started !") + // Loading App config + appConfig, err := config.Read(FILE_PATH) + if err != nil { + println("Impossible de charger la configuration du server : " + err.Error()) + return + } + + println("Adresse multicast : " + appConfig.MulticastAddress) + protocolRepository := repository.CreateProtocolRepository() // Création des Writers diff --git a/pkg/config/AppConfig.go b/pkg/config/AppConfig.go index 566e9bb..0d04491 100644 --- a/pkg/config/AppConfig.go +++ b/pkg/config/AppConfig.go @@ -3,26 +3,11 @@ package config // AppConfig Contient toute la configuration du server type AppConfig struct { // multicastAddress Contient l'adresse multicast du FileFrontEnd (adresse:port) - multicastAddress string `json:"multicastAddress"` + MulticastAddress string `json:"multicastAddress"` // multicastSecond Contient le nombre de seconde entre chaque annonce - multicastSecond int `json:"multicastSecond"` + MulticastSecond int `json:"multicastSecond"` // unicastAddress Contient l'adresse unicast auquel le FileFrontEnd se connecte (adresse:port) - unicastAddress string `json:"unicastAddress"` -} - -// MulticastAddress Accesseur pour multicastAddress -func (a *AppConfig) MulticastAddress() string { - return a.multicastAddress -} - -// MulticastSecond Accesseur pour multicastSecond -func (a *AppConfig) MulticastSecond() int { - return a.multicastSecond -} - -// UnicastAddress Accesseur pour unicastAddress -func (a *AppConfig) UnicastAddress() string { - return a.unicastAddress + UnicastAddress string `json:"unicastAddress"` } diff --git a/pkg/config/JsonConfigReader.go b/pkg/config/JsonConfigReader.go new file mode 100644 index 0000000..1cd3e96 --- /dev/null +++ b/pkg/config/JsonConfigReader.go @@ -0,0 +1,21 @@ +package config + +import ( + "encoding/json" + "io/ioutil" +) + +func Read(filePath string) (*AppConfig, error) { + config := AppConfig{} + file, err := ioutil.ReadFile(filePath) + if err != nil { + return nil, err + } + + errJson := json.Unmarshal(file, &config) + if errJson != nil { + return nil, errJson + } + + return &config, nil +}