Search/config.go

149 lines
3 KiB
Go
Raw Normal View History

2024-08-08 14:49:30 +00:00
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/fsnotify/fsnotify"
"gopkg.in/ini.v1"
)
func initConfig() error {
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
return createConfig()
}
fmt.Println("Configuration file already exists.")
config = loadConfig()
return nil
}
func createConfig() error {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Configuration file not found.")
fmt.Print("Do you want to use default values? (yes/no): ")
useDefaults, _ := reader.ReadString('\n')
if strings.TrimSpace(useDefaults) != "yes" {
fmt.Print("Enter port (default 5000): ")
portStr, _ := reader.ReadString('\n')
if portStr != "\n" {
port, err := strconv.Atoi(strings.TrimSpace(portStr))
if err != nil {
config.Port = 5000
} else {
config.Port = port
}
}
fmt.Print("Enter your domain address (default localhost): ")
domain, _ := reader.ReadString('\n')
if domain != "\n" {
config.Domain = strings.TrimSpace(domain)
}
} else {
config = defaultConfig
}
if config.AuthCode == "" {
config.AuthCode = generateStrongRandomString(64)
fmt.Printf("Generated connection code: %s\n", config.AuthCode)
}
saveConfig(config)
return nil
}
func saveConfig(config Config) {
cfg := ini.Empty()
sec := cfg.Section("")
sec.Key("Port").SetValue(strconv.Itoa(config.Port))
sec.Key("AuthCode").SetValue(config.AuthCode)
sec.Key("PeerID").SetValue(config.PeerID)
peers := strings.Join(config.Peers, ",")
sec.Key("Peers").SetValue(peers)
sec.Key("Domain").SetValue(config.Domain)
err := cfg.SaveTo(configFilePath)
if err != nil {
fmt.Println("Error writing to config file:", err)
}
}
func loadConfig() Config {
cfg, err := ini.Load(configFilePath)
if err != nil {
log.Fatalf("Error opening config file: %v", err)
}
port, err := cfg.Section("").Key("Port").Int()
if err != nil || port == 0 {
port = 5000 // Default to 5000 if not set or error
}
peersStr := cfg.Section("").Key("Peers").String()
var peers []string
if peersStr != "" {
peers = strings.Split(peersStr, ",")
for i, peer := range peers {
peers[i] = addProtocol(peer)
}
}
domain := cfg.Section("").Key("Domain").String()
if domain == "" {
domain = "localhost" // Default to localhost if not set
}
config = Config{
Port: port,
AuthCode: cfg.Section("").Key("AuthCode").String(),
PeerID: cfg.Section("").Key("PeerID").String(),
Peers: peers,
Domain: domain,
}
return config
}
func startFileWatcher() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
go func() {
defer watcher.Close()
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("Modified file:", event.Name)
configLock.Lock()
config = loadConfig()
configLock.Unlock()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
2024-08-08 19:59:10 +00:00
log.Println("Error watching configuration file:", err)
2024-08-08 14:49:30 +00:00
}
}
}()
err = watcher.Add(configFilePath)
if err != nil {
log.Fatal(err)
}
}