60 lines
998 B
Go
60 lines
998 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
Port int
|
|
AuthCode string
|
|
Peers []string
|
|
PeerID string
|
|
Domain string
|
|
}
|
|
|
|
var defaultConfig = Config{
|
|
Port: 5000,
|
|
Domain: "localhost",
|
|
}
|
|
|
|
const configFilePath = "config.ini"
|
|
|
|
var config Config
|
|
var configLock sync.RWMutex
|
|
|
|
func main() {
|
|
err := initConfig()
|
|
if err != nil {
|
|
fmt.Println("Error during initialization:", err)
|
|
return
|
|
}
|
|
|
|
loadNodeConfig()
|
|
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()
|
|
if nodeErr != nil {
|
|
log.Fatalf("Failed to generate host ID: %v", nodeErr)
|
|
}
|
|
config.PeerID = hostID
|
|
|
|
if len(config.Peers) > 0 {
|
|
time.Sleep(2 * time.Second) // Give some time for connections to establish
|
|
startElection()
|
|
}
|
|
|
|
go startNodeClient()
|
|
|
|
runServer()
|
|
}
|