package main

import (
	"log"
	"os"

	"igit.com/xbase/raft"
)

func main() {
	// 1. 配置初始化
	config := raft.DefaultConfig()
	config.NodeID = "node2"
	config.ListenAddr = "127.0.0.1:9002"
	config.HTTPAddr = ":8002" // Enable HTTP API
	config.DataDir = "data/node2"
	config.Logger = &raft.DefaultLogger{Prefix: "node2"}

	// Node 2 starts empty and joins later, or can be part of cluster if we update config
	// Here we let it join later via CLI or pre-configure it empty
	// To make it easy to test clustered auth, let's just join manually via CLI from node1
	
	// Ensure data directory exists
	os.MkdirAll(config.DataDir, 0755)

	// 2. 创建 KV Server
	server, err := raft.NewKVServer(config)
	if err != nil {
		log.Fatalf("Failed to create server: %v", err)
	}

	// 3. 启动服务
	if err := server.Start(); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}
	defer server.Stop()

	log.Println("Node2 started on 127.0.0.1:9002 (HTTP :8002)")
	
	// Block forever
	select {}
}

