wip
This commit is contained in:
parent
8f913eca0d
commit
f6576a9134
5 changed files with 237 additions and 36 deletions
73
init.go
73
init.go
|
@ -10,14 +10,17 @@ import (
|
|||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
// Configuration structure
|
||||
type Config struct {
|
||||
Port int
|
||||
ConnectionCode string
|
||||
Peers []string
|
||||
OpenSearch OpenSearchConfig
|
||||
Port int
|
||||
AuthCode string
|
||||
Peers []string
|
||||
OpenSearch OpenSearchConfig
|
||||
}
|
||||
|
||||
type OpenSearchConfig struct {
|
||||
|
@ -34,18 +37,31 @@ var defaultConfig = Config{
|
|||
|
||||
const configFilePath = "config.json"
|
||||
|
||||
var config Config
|
||||
var configLock sync.RWMutex
|
||||
|
||||
func main() {
|
||||
// Run the initialization process
|
||||
err := initConfig()
|
||||
if err != nil {
|
||||
fmt.Println("Error during initialization:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// This is stupid
|
||||
loadNodeConfig()
|
||||
go startFileWatcher()
|
||||
go checkMasterHeartbeat()
|
||||
|
||||
if config.AuthCode == "" {
|
||||
config.AuthCode = generateStrongRandomString(64)
|
||||
fmt.Printf("Generated connection code: %s\n", config.AuthCode)
|
||||
saveConfig(config)
|
||||
}
|
||||
|
||||
if len(config.Peers) > 0 {
|
||||
go startNodeClient(config.Peers)
|
||||
startElection()
|
||||
}
|
||||
|
||||
// Start the main application
|
||||
runServer()
|
||||
}
|
||||
|
||||
|
@ -55,6 +71,7 @@ func initConfig() error {
|
|||
}
|
||||
|
||||
fmt.Println("Configuration file already exists.")
|
||||
config = loadConfig()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -94,9 +111,9 @@ func createConfig() error {
|
|||
}
|
||||
}
|
||||
|
||||
if config.ConnectionCode == "" {
|
||||
config.ConnectionCode = generateStrongRandomString(32)
|
||||
fmt.Printf("Generated connection code: %s\n", config.ConnectionCode)
|
||||
if config.AuthCode == "" {
|
||||
config.AuthCode = generateStrongRandomString(64)
|
||||
fmt.Printf("Generated connection code: %s\n", config.AuthCode)
|
||||
}
|
||||
|
||||
saveConfig(config)
|
||||
|
@ -146,3 +163,39 @@ func generateStrongRandomString(length int) string {
|
|||
}
|
||||
return base64.URLEncoding.EncodeToString(bytes)[:length]
|
||||
}
|
||||
|
||||
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()
|
||||
// Perform your logic here to handle the changes in the config file
|
||||
}
|
||||
case err, ok := <-watcher.Errors:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
log.Println("Error:", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
err = watcher.Add(configFilePath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue