func (s *server) SetTerm(term uint64) {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	s.currentTerm = term
}
SetTerm set current term
package raft
import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net"
	"net/http"
	"os"
	"path"
	"strings"
	"sync"
	"time"
	pb "github.com/moxiaomomo/goRaft/proto"
	"github.com/moxiaomomo/goRaft/util"
	"github.com/moxiaomomo/goRaft/util/logger"
	"google.golang.org/grpc"
)
type HandleFuncType func(w http.ResponseWriter, r *http.Request)
type server struct {
	mutex sync.RWMutex
	path  string
	state string
	currentLeaderName   string
	currentLeaderHost   string
	currentLeaderExHost string
	currentTerm         uint64
	log      *Log
	conf     *Config
	confPath string
	lastHeartbeatTime int64
	lastSnapshotTime  int64
	heartbeatInterval uint64
	votedForTerm      uint64  
	stopped    chan bool
	ch         chan interface{}
	handlefunc map[string]HandleFuncType
	peers    map[string]*Peer
	syncpeer map[string]int
}
type Server interface {
	Start() error
	State() string
	IsRunning() bool
	CurLeaderExHost() string
	AddPeer(name string, host string) error
	RemovePeer(name string, host string) error
	RegisterCommand(cmd Command)
	RegisterHandler(urlpath string, fc HandleFuncType)
	OnAppendEntry(cmd Command, cmds []byte)
}
func pathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return true, err
}
func NewServer(workdir string, confPath string) (Server, error) {
	_ = os.Mkdir(workdir, 0700)
	params := ParseEnvParams()
	logger.Info(params)
	if confPath == "" || !strings.Contains(confPath, "/") {
		confPath = fmt.Sprintf("/opt/raft/raft_%s.cfg", params.SvrName)
	}
	confdir := confPath[0:strings.LastIndex(confPath, "/")]
	if exist, _ := pathExists(confdir); !exist {
		os.MkdirAll(confdir, 0700)
	}
	if exist, _ := pathExists(confPath); !exist {
		ioutil.WriteFile(confPath, []byte(DefaultConfig), 0644)
	}
	s := &server{
		path:              workdir,
		confPath:          confPath,
		state:             Stopped,
		log:               newLog(),
		heartbeatInterval: 300,  
		lastSnapshotTime:  util.GetTimestampInMilli(),
		syncpeer:          make(map[string]int),
		handlefunc:        make(map[string]HandleFuncType),
	}
	err := s.loadConf(params)
	if err != nil {
		return nil, err
	}
	if !s.ConfigValidate() {
		return nil, errors.New("Invalid configuration")
	}
	return s, nil
}
func (s *server) ConfigValidate() bool {
	if s.conf == nil {
		return false
	}
	if s.conf.Name == "" || s.conf.Host == "" || s.conf.Client == "" {
		return false
	}
	return true
}