Search/init.go

61 lines
998 B
Go
Raw Normal View History

2024-06-12 14:26:50 +02:00
package main
import (
"fmt"
"log"
2024-07-05 03:08:35 +02:00
"sync"
2024-08-08 13:35:50 +02:00
"time"
2024-06-12 14:26:50 +02:00
)
type Config struct {
2024-08-08 15:03:33 +02:00
Port int
AuthCode string
Peers []string
PeerID string
Domain string
2024-06-12 14:26:50 +02:00
}
var defaultConfig = Config{
2024-08-08 15:03:33 +02:00
Port: 5000,
Domain: "localhost",
2024-06-12 14:26:50 +02:00
}
2024-08-08 15:03:33 +02:00
const configFilePath = "config.ini"
2024-06-12 14:26:50 +02:00
2024-07-05 03:08:35 +02:00
var config Config
var configLock sync.RWMutex
2024-06-12 14:26:50 +02:00
func main() {
err := initConfig()
if err != nil {
fmt.Println("Error during initialization:", err)
return
}
2024-06-30 23:20:52 +02:00
loadNodeConfig()
2024-07-05 03:08:35 +02:00
go startFileWatcher()
go checkMasterHeartbeat()
if config.AuthCode == "" {
config.AuthCode = generateStrongRandomString(64)
fmt.Printf("Generated connection code: %s\n", config.AuthCode)
saveConfig(config)
}
// Generate Host ID
hostID, nodeErr := generateHostID()
2024-08-08 13:35:50 +02:00
if nodeErr != nil {
log.Fatalf("Failed to generate host ID: %v", nodeErr)
2024-08-08 13:35:50 +02:00
}
config.PeerID = hostID
2024-08-08 13:35:50 +02:00
2024-07-05 03:08:35 +02:00
if len(config.Peers) > 0 {
2024-08-08 13:35:50 +02:00
time.Sleep(2 * time.Second) // Give some time for connections to establish
2024-07-05 03:08:35 +02:00
startElection()
}
2024-06-30 23:20:52 +02:00
2024-08-08 13:35:50 +02:00
go startNodeClient()
2024-06-12 14:26:50 +02:00
runServer()
}