From 9c19c8afaee8014179bd856ba8ed6b92d3022df5 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sat, 3 May 2025 22:39:03 +0200 Subject: [PATCH] Backend: Added config.go that creates a config file and read the photo path from it. --- backend/.gitignore | 2 +- backend/core/file.go | 2 +- backend/go.mod | 2 -- backend/go.sum | 2 -- backend/main.go | 21 +++++++------- backend/util/config.go | 62 ++++++++++++++++++++++++++++++++++++++++++ backend/util/logger.go | 1 + backend/util/util.go | 13 +++++++++ 8 files changed, 89 insertions(+), 16 deletions(-) delete mode 100644 backend/go.sum create mode 100644 backend/util/config.go diff --git a/backend/.gitignore b/backend/.gitignore index 2eea525..0cffcb3 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1 +1 @@ -.env \ No newline at end of file +config.json \ No newline at end of file diff --git a/backend/core/file.go b/backend/core/file.go index 0d53d8e..7cd52a8 100644 --- a/backend/core/file.go +++ b/backend/core/file.go @@ -17,7 +17,7 @@ type PictureData struct { func listFiles(path string, actualdir string, picd []PictureData) []PictureData { entries, err := os.ReadDir(path) if err != nil { - util.Logformat(util.WARNING, "Could not read files in '%s'", path) + util.Logformat(util.WARNING, "Could not read files in '%s'\n", path) return picd } for _, entry := range entries { diff --git a/backend/go.mod b/backend/go.mod index c085586..9be7739 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -1,5 +1,3 @@ module backend go 1.24.2 - -require github.com/joho/godotenv v1.5.1 // indirect diff --git a/backend/go.sum b/backend/go.sum deleted file mode 100644 index d61b19e..0000000 --- a/backend/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/backend/main.go b/backend/main.go index 049e652..71e36c1 100644 --- a/backend/main.go +++ b/backend/main.go @@ -4,23 +4,24 @@ import ( "backend/core" "backend/server" "backend/util" - "os" - - "github.com/joho/godotenv" + "strings" ) func main() { - err := godotenv.Load(".env") + cfg, err := util.ReadConfig("config.json") if err != nil { - panic("Error loading .env file") + if strings.Compare(err.Error(), "open config.json: no such file or directory") == 0 { + util.Logformat(util.INFO, "Creating config file...\n") + util.CreateConfig() + cfg, err = util.ReadConfig("config.json") + } else { + util.Logformat(util.ERROR, "Could not read configuration file: %s\n", err.Error()) + } } // Reading files - util.Logformat(util.INFO, "Reading to folder '%s' (Recursively)...\n", os.Getenv("PHOTO_PATH")) - picd := core.ListFiles(os.Getenv("PHOTO_PATH")) - if picd == nil { - panic("Could not gather any file.") - } + util.Logformat(util.INFO, "Reading folder '%s'\n", cfg.PhotoPath) + core.ListFiles(cfg.PhotoPath) // Set Log Level util.SetLevel(util.NOLOG) diff --git a/backend/util/config.go b/backend/util/config.go new file mode 100644 index 0000000..7405c3b --- /dev/null +++ b/backend/util/config.go @@ -0,0 +1,62 @@ +package util + +import ( + "encoding/json" + "fmt" + "os" +) + +type Config struct { + PhotoPath string `json:"photopath"` +} + +func ReadConfig(path string) (Config, error) { + var cfg Config + file, err := os.Open(path) + if err != nil { + return cfg, err + } + defer file.Close() + + decoder := json.NewDecoder(file) + err = decoder.Decode(&cfg) + return cfg, err +} + +func createConfigFile(cfg Config) { + data, err := json.MarshalIndent(cfg, "", " ") + if err != nil { + panic(err) + } + + err = os.WriteFile("config.json", data, 0644) + if err != nil { + panic(err) + } +} + +func CreateConfig() { + var cfg Config + println("┌ Please Configure your backend !\n│") + + // Checking PhotoPath + var err error = fmt.Errorf("Path") + println("├\uf15b Enter photo album path \033[90m(press Enter for './')\033[0m") + for err != nil { + photopath := InputString("│> ") + if photopath == "" { + photopath = "./" + } + _, err = os.ReadDir(photopath) + if err != nil { + fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m") + } else { + fmt.Println("│\033[32m\uf00c Path correct.\033[0m") + cfg.PhotoPath = photopath + } + } + + // Ending configuration + println("│\n└ Configuration finished !") + createConfigFile(cfg) +} diff --git a/backend/util/logger.go b/backend/util/logger.go index 7493395..24dc0b0 100644 --- a/backend/util/logger.go +++ b/backend/util/logger.go @@ -17,6 +17,7 @@ const ( RED colorFormat = "\033[31m" LIGHT_RED colorFormat = "\033[91m" WHITE colorFormat = "\033[97m" + GREY colorFormat = "\033[90m" RESET colorFormat = "\033[0m" ) diff --git a/backend/util/util.go b/backend/util/util.go index 488c318..682da95 100644 --- a/backend/util/util.go +++ b/backend/util/util.go @@ -1,7 +1,10 @@ package util import ( + "bufio" + "fmt" "net/http" + "os" "runtime" "strings" ) @@ -16,3 +19,13 @@ func HasSubURI(r *http.Request) (bool, string) { url := strings.Split(r.URL.Path, "/") return (len(url[2]) > 0), url[2] } + +func InputString(print string) string { + reader := bufio.NewReader(os.Stdin) + fmt.Print(print) + input, err := reader.ReadString('\n') + if err != nil { + panic(err) + } + return strings.TrimSpace(input) +}