Backend: Added config.go that creates a config file and read the photo path from it.

This commit is contained in:
Yohan Boujon 2025-05-03 22:39:03 +02:00
parent 9e86258588
commit 9c19c8afae
8 changed files with 89 additions and 16 deletions

2
backend/.gitignore vendored
View file

@ -1 +1 @@
.env config.json

View file

@ -17,7 +17,7 @@ type PictureData struct {
func listFiles(path string, actualdir string, picd []PictureData) []PictureData { func listFiles(path string, actualdir string, picd []PictureData) []PictureData {
entries, err := os.ReadDir(path) entries, err := os.ReadDir(path)
if err != nil { 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 return picd
} }
for _, entry := range entries { for _, entry := range entries {

View file

@ -1,5 +1,3 @@
module backend module backend
go 1.24.2 go 1.24.2
require github.com/joho/godotenv v1.5.1 // indirect

View file

@ -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=

View file

@ -4,23 +4,24 @@ import (
"backend/core" "backend/core"
"backend/server" "backend/server"
"backend/util" "backend/util"
"os" "strings"
"github.com/joho/godotenv"
) )
func main() { func main() {
err := godotenv.Load(".env") cfg, err := util.ReadConfig("config.json")
if err != nil { 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 // Reading files
util.Logformat(util.INFO, "Reading to folder '%s' (Recursively)...\n", os.Getenv("PHOTO_PATH")) util.Logformat(util.INFO, "Reading folder '%s'\n", cfg.PhotoPath)
picd := core.ListFiles(os.Getenv("PHOTO_PATH")) core.ListFiles(cfg.PhotoPath)
if picd == nil {
panic("Could not gather any file.")
}
// Set Log Level // Set Log Level
util.SetLevel(util.NOLOG) util.SetLevel(util.NOLOG)

62
backend/util/config.go Normal file
View file

@ -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)
}

View file

@ -17,6 +17,7 @@ const (
RED colorFormat = "\033[31m" RED colorFormat = "\033[31m"
LIGHT_RED colorFormat = "\033[91m" LIGHT_RED colorFormat = "\033[91m"
WHITE colorFormat = "\033[97m" WHITE colorFormat = "\033[97m"
GREY colorFormat = "\033[90m"
RESET colorFormat = "\033[0m" RESET colorFormat = "\033[0m"
) )

View file

@ -1,7 +1,10 @@
package util package util
import ( import (
"bufio"
"fmt"
"net/http" "net/http"
"os"
"runtime" "runtime"
"strings" "strings"
) )
@ -16,3 +19,13 @@ func HasSubURI(r *http.Request) (bool, string) {
url := strings.Split(r.URL.Path, "/") url := strings.Split(r.URL.Path, "/")
return (len(url[2]) > 0), url[2] 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)
}