-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (47 loc) · 1.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"fmt"
"github.com/iris-contrib/middleware/cors"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/core/router"
"os"
"raft/config"
"raft/rest"
"raft/server"
"strconv"
)
func main() {
fmt.Println("Raft服务启动")
args := os.Args
if len(args) < 2 {
fmt.Printf("请携带上config文件的地址参数信息...")
return
}
fmt.Printf("传入的配置文件路径参数:%s\n", args[1])
config.InitConfig(args[1])
server.InitElectionLeader()
app := route()
_ = app.Run(iris.Addr(config.Conf.Server.Domain + ":" + strconv.Itoa(config.Conf.Server.Port)))
}
func route() (app *iris.Application) {
app = iris.New()
v1 := app.Party("/v1.0.0/raft", corsConfig()).AllowMethods(iris.MethodOptions)
{
v1.Get("/information", server.RaftInformation)
v1.PartyFunc("/election-leader", func(leaderParty router.Party) {
leaderParty.Post("/vote", rest.Vote)
leaderParty.Post("/follower", rest.MaintainAuthority)
})
v1.PartyFunc("/log-replication", func(logParty router.Party) {
logParty.Post("/keys", rest.Command)
})
}
return app
}
func corsConfig() iris.Handler {
crs := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, //允许通过的主机名称
AllowCredentials: true,
})
return crs
}