-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
124 lines (103 loc) · 3.31 KB
/
main.go
File metadata and controls
124 lines (103 loc) · 3.31 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/anywherelan/awl/p2p"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
"go.uber.org/zap/zapcore"
"github.com/anywherelan/awl-bootstrap-node/config"
)
// @title Anywherelan bootstrap node API
// @version 0.1
// @description Anywherelan bootstrap node API
// @Host localhost:9090
// @BasePath /api/v0/
//go:generate swag init --parseDependency
//go:generate rm -f docs/docs.go docs/swagger.json
func main() {
configName := flag.String("config-name", "config.yaml", "Config filepath")
generateConfig := flag.Bool("generate-config", false, "Enable generating config instead of starting the server")
flag.Parse()
// go run main.go -generate-config -config-name=config.yaml
if *generateConfig {
generateExampleConfig(*configName)
return
}
app := New()
logger := app.SetupLoggerAndConfig()
ctx, ctxCancel := context.WithCancel(context.Background())
err := app.Init(ctx)
if err != nil {
logger.Fatal(err)
}
quit := make(chan os.Signal, 2)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
logger.Infof("received signal %s", <-quit)
finishedCh := make(chan struct{})
go func() {
select {
case <-time.After(3 * time.Second):
logger.Fatal("exit timeout reached: terminating")
case <-finishedCh:
// ok
case sig := <-quit:
logger.Fatalf("duplicate exit signal %s: terminating", sig)
}
}()
ctxCancel()
app.Close()
finishedCh <- struct{}{}
logger.Info("exited normally")
}
func generateExampleConfig(configName string) {
_, err := os.Stat(configName)
if os.IsNotExist(err) {
// ok
} else if err != nil {
fmt.Printf("Error reading config file: %s\n", err)
os.Exit(1)
} else {
fmt.Printf("Config file %s already exists, specify different name with flag -config-name=new-config.yaml\n", configName)
os.Exit(1)
}
// disable logs
_ = os.Setenv("QUIC_GO_DISABLE_RECEIVE_BUFFER_WARNING", "true")
log.SetupLogging(zapcore.NewNopCore(), func(name string) zapcore.Level {
return zapcore.FatalLevel
})
conf := config.NewExampleConfig()
peerstore, _ := pstoremem.NewPeerstore()
p2pSrv := p2p.NewP2p(context.Background())
host, err := p2pSrv.InitHost(p2p.HostConfig{
AllowEmptyBootstrapPeers: true,
Peerstore: peerstore,
DHTDatastore: dssync.MutexWrap(ds.NewMapDatastore()),
})
if err != nil {
fmt.Printf("Error initializing test host: %s\n", err)
os.Exit(1)
}
privKey := host.Peerstore().PrivKey(host.ID())
conf.SetIdentity(privKey, host.ID())
_ = p2pSrv.Close()
err = config.SaveConfig(conf, configName)
if err != nil {
fmt.Printf("Error saving config file: %s\n", err)
os.Exit(1)
}
fmt.Printf("Generated example config file: %s\n", configName)
fmt.Printf("Below are addresses that can be used to connect to this bootstrap node\n")
fmt.Printf("Replace 127.0.0.1 or ::1 with the appropriate IP address. If you don't have IPv6 support, just skip it\n\n")
fmt.Printf("/ip4/127.0.0.1/tcp/6150/p2p/%s\n", conf.P2pNode.PeerID)
fmt.Printf("/ip4/127.0.0.1/udp/6150/quic-v1/p2p/%s\n", conf.P2pNode.PeerID)
fmt.Printf("/ip6/::1/tcp/7250/p2p/%s\n", conf.P2pNode.PeerID)
fmt.Printf("/ip6/::1/udp/7250/quic-v1/p2p/%s\n", conf.P2pNode.PeerID)
}