-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (91 loc) · 2.51 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
112
113
package main
import (
"context"
"flag"
"fmt"
app "github.com/cometbft/abci-v2-forum-app/abci"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/cometbft/cometbft/p2p"
"github.com/cometbft/cometbft/proxy"
"github.com/spf13/viper"
db "github.com/cometbft/cometbft-db"
cfg "github.com/cometbft/cometbft/config"
cmtflags "github.com/cometbft/cometbft/libs/cli/flags"
cmtlog "github.com/cometbft/cometbft/libs/log"
nm "github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/privval"
)
var homeDir string
func init() {
flag.StringVar(&homeDir, "home", "", "Path to the CometBFT config directory (if empty, uses $HOME/.forumapp)")
}
func main() {
flag.Parse()
if homeDir == "" {
homeDir = os.ExpandEnv("$HOME/.forumapp")
}
config := cfg.DefaultConfig()
config.SetRoot(homeDir)
viper.SetConfigFile(fmt.Sprintf("%s/%s", homeDir, "config/config.toml"))
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("failed to read config: %v", err)
}
store, err := db.NewGoLevelDB(filepath.Join(homeDir, "forum-db"), ".")
if err != nil {
log.Fatalf("failed to create database: %v", err)
}
defer store.Close()
dbPath := "forum-db"
appConfigPath := "app.toml"
app, err := app.NewForumApp(dbPath, appConfigPath)
if err != nil {
log.Fatalf("failed to create ForumApp instance: %v", err)
}
logger := cmtlog.NewTMLogger(cmtlog.NewSyncWriter(os.Stdout))
logger, err = cmtflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel)
if err != nil {
log.Fatalf("failed to read genesis doc: %v", err)
}
nodeKey, err := p2p.LoadNodeKey(config.NodeKeyFile())
if err != nil {
log.Fatalf("failed to load node key: %v", err)
}
pv := privval.LoadFilePV(
config.PrivValidatorKeyFile(),
config.PrivValidatorStateFile(),
)
node, err := nm.NewNode(
context.Background(),
config,
pv,
nodeKey,
proxy.NewLocalClientCreator(app),
nm.DefaultGenesisDocProviderFunc(config),
cfg.DefaultDBProvider,
nm.DefaultMetricsProvider(config.Instrumentation),
logger,
)
if err != nil {
log.Fatalf("failed to create CometBFT node: %v", err)
}
if err := node.Start(); err != nil {
log.Fatalf("failed to start CometBFT node: %v", err)
}
defer func() {
_ = node.Stop()
node.Wait()
}()
httpAddr := "127.0.0.1:8080"
if err := http.ListenAndServe(httpAddr, nil); err != nil {
log.Fatalf("failed to start HTTP server: %v", err)
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
fmt.Println("Forum application stopped")
}