-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (97 loc) · 2.38 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"another_node/conf"
"another_node/internal/community/node"
"another_node/internal/community/storage"
"another_node/internal/plugin"
"another_node/internal/web_server/routers"
plugin_passkey_relay_party "another_node/plugins/passkey_relay_party"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var httpPlugins = make([]plugin.HttpPlugin, 0)
func preventSuspend(host string) {
go func() {
t := time.Tick(time.Second * 30)
for {
if resp, err := http.Get(host); err != nil {
log.Default().Printf("error: " + err.Error())
} else {
if b, err := io.ReadAll(resp.Body); err == nil {
log.Default().Printf("health: " + string(b))
}
}
<-t
}
}()
}
func getFlags() (listen *uint16, name *string, joinAddrs *string, genesis *bool) {
// 解析命令行参数
listenTmp := flag.Int("listen", 0, "Listen port number")
name = flag.String("name", "", "Node name")
joinAddrs = flag.String("join", "", "Addresses of nodes to join (comma-separated)")
explicitGenesisVar := flag.Bool("genesis", false, "Is this genesis node")
flag.Parse()
if listenTmp == nil {
m := uint16(7946)
listen = &m
} else {
m := uint16(*listenTmp)
listen = &m
}
flag.CommandLine.Visit(func(f *flag.Flag) {
if f.Name == "genesis" {
genesis = explicitGenesisVar
}
})
return
}
// @securityDefinitions.apikey JWT
// @in header
// @name Authorization
// @contact.name AAStar Support
// @contact.url https://aastar.xyz
// @BasePath /api
func main() {
if healthz := os.Getenv("healthz"); len(healthz) > 0 {
preventSuspend(healthz)
}
defer func() {
storage.Close()
}()
// start community node
if community, err := node.New(getFlags()); err != nil {
panic(err)
} else {
routers := routers.SetRouters()
// load http plugins
for _, plugin := range RegisterHttpPlugin() {
plugin.RegisterRoutes(routers, community)
}
// start web server
routers.Run(func(port int) string {
if port == 0 {
panic("port is invalid")
}
fmt.Println("community node created Port is: ", port)
portStr := strconv.Itoa(conf.GetWeb().Port)
if !strings.HasPrefix(portStr, ":") {
portStr = ":" + portStr
}
return portStr
}(conf.GetWeb().Port))
}
}
func RegisterHttpPlugin() []plugin.HttpPlugin {
if len(httpPlugins) == 0 {
httpPlugins = append(httpPlugins, plugin_passkey_relay_party.NewRelay())
}
return httpPlugins
}