code auth added

This commit is contained in:
partisan 2024-08-08 13:35:50 +02:00
parent f6576a9134
commit 9f655ba8c0
5 changed files with 127 additions and 145 deletions

18
init.go
View file

@ -11,15 +11,16 @@ import (
"strconv"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
)
// Configuration structure
type Config struct {
Port int
AuthCode string
Peers []string
PeerID string
OpenSearch OpenSearchConfig
}
@ -27,7 +28,6 @@ type OpenSearchConfig struct {
Domain string
}
// Default configuration values
var defaultConfig = Config{
Port: 5000,
OpenSearch: OpenSearchConfig{
@ -57,11 +57,21 @@ func main() {
saveConfig(config)
}
// Initialize P2P
var nodeErr error
hostID, nodeErr = initP2P()
if nodeErr != nil {
log.Fatalf("Failed to initialize P2P: %v", nodeErr)
}
config.PeerID = hostID.String()
if len(config.Peers) > 0 {
go startNodeClient(config.Peers)
time.Sleep(2 * time.Second) // Give some time for connections to establish
startElection()
}
go startNodeClient()
runServer()
}
@ -103,7 +113,7 @@ func createConfig() error {
fmt.Print("Do you want to connect to other nodes? (yes/no): ")
connectNodes, _ := reader.ReadString('\n')
if strings.TrimSpace(connectNodes) == "yes" {
fmt.Println("Enter peer addresses (comma separated, e.g., http://localhost:5000,http://localhost:5001): ")
fmt.Println("Enter peer addresses (comma separated, e.g., /ip4/127.0.0.1/tcp/5000,/ip4/127.0.0.1/tcp/5001): ")
peersStr, _ := reader.ReadString('\n')
if peersStr != "\n" {
config.Peers = strings.Split(strings.TrimSpace(peersStr), ",")