package util import ( "encoding/json" "fmt" "os" "path/filepath" "strconv" ) type Config struct { PhotoPath string `json:"photo_path"` IgnoreFolder []string `json:"ignore_folder"` Port uint64 `json:"port"` RootUrl string `json:"root_url"` } var main_config Config func GetConfig() Config { return main_config } func GetURL() string { if main_config.RootUrl == "http://localhost" { return "http://localhost" + strconv.FormatUint(main_config.Port, 10) } else { return main_config.RootUrl } } func ReadConfig(path string) (Config, error) { // Gathering photo path file, err := os.Open(path) if err != nil { return main_config, err } defer file.Close() decoder := json.NewDecoder(file) decoder.DisallowUnknownFields() err = decoder.Decode(&main_config) return main_config, 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 println("├\uf03e Enter photo album path \033[90m(press Enter for './')\033[0m") for { photopath := InputString("│> ") if photopath == "" { photopath = "./" } if _, err := os.ReadDir(photopath); err != nil { fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m") } else { fmt.Println("│\033[32m\uf00c Path correct.\033[0m") cfg.PhotoPath = photopath fmt.Println("│\033[36m󰑓 Creating '.compressed' folder...\033[0m") err := os.MkdirAll(filepath.Join(photopath, ".compressed"), 0755) if err != nil { fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m") } else { fmt.Println("│\033[32m\uf00c Folder created/already exists.\033[0m\n│") } cfg.IgnoreFolder = append(cfg.IgnoreFolder, ".compressed") break } } // Check port println("├\uf233 Enter the port number for the server \033[90m(press Enter for '8085')\033[0m") for { port := InputString("│> ") if port == "" { port = "8085" } portnum, err := strconv.ParseUint(port, 10, 0) if err != nil { fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m") continue } fmt.Println("│\033[32m\uf00c Choosen port: " + port + ".\033[0m\n│") cfg.Port = portnum break } // Check root URL println("├󰇧 Enter the root URL for the server \033[90m(press Enter for 'http://localhost')\033[0m") rootUrl := InputString("│> ") if rootUrl == "" { rootUrl = "http://localhost" } cfg.RootUrl = rootUrl // Ending configuration println("└ Configuration finished !") createConfigFile(cfg) }