-
Notifications
You must be signed in to change notification settings - Fork 45
/
main.go
256 lines (228 loc) · 6.58 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"context"
"encoding/json"
"fmt"
logging "github.com/ipfs/go-log"
ma "github.com/multiformats/go-multiaddr"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/textileio/go-threads/util"
"github.com/textileio/textile/cmd"
"github.com/textileio/textile/core"
)
const daemonName = "buckd"
var (
log = logging.Logger(daemonName)
config = &cmd.Config{
Viper: viper.New(),
Dir: "." + daemonName,
Name: "config",
Flags: map[string]cmd.Flag{
"repo": {
Key: "repo",
DefValue: "${HOME}/." + daemonName + "/repo",
},
"debug": {
Key: "log.debug",
DefValue: false,
},
"logFile": {
Key: "log.file",
DefValue: "${HOME}/." + daemonName + "/log",
},
"addrApi": {
Key: "addr.api",
DefValue: "/ip4/127.0.0.1/tcp/3006",
},
"addrApiProxy": {
Key: "addr.api_proxy",
DefValue: "/ip4/127.0.0.1/tcp/3007",
},
"addrThreadsHost": {
Key: "addr.threads.host",
DefValue: "/ip4/0.0.0.0/tcp/4006",
},
"addrGatewayHost": {
Key: "addr.gateway.host",
DefValue: "/ip4/127.0.0.1/tcp/8006",
},
"addrGatewayUrl": {
Key: "addr.gateway.url",
DefValue: "http://127.0.0.1:8006",
},
"addrIpfsApi": {
Key: "addr.ipfs.api",
DefValue: "/ip4/127.0.0.1/tcp/5001",
},
"addrPowergateApi": {
Key: "addr.powergate.api",
DefValue: "",
},
"addrMongoUri": {
Key: "addr.mongo_uri",
DefValue: "mongodb://127.0.0.1:27017",
},
"gatewaySubdomains": {
Key: "gateway.subdomains",
DefValue: false,
},
"dnsDomain": {
Key: "dns.domain",
DefValue: "",
},
"dnsZoneID": {
Key: "dns.zone_id",
DefValue: "",
},
"dnsToken": {
Key: "dns.token",
DefValue: "",
},
},
EnvPre: "BUCK",
Global: true,
}
)
func init() {
cobra.OnInitialize(cmd.InitConfig(config))
cmd.InitConfigCmd(rootCmd, config.Viper, config.Dir)
rootCmd.PersistentFlags().StringVar(
&config.File,
"config",
"",
"Config file (default ${HOME}/"+config.Dir+"/"+config.Name+".yml)")
rootCmd.PersistentFlags().StringP(
"repo",
"r",
config.Flags["repo"].DefValue.(string),
"Path to repository")
rootCmd.PersistentFlags().BoolP(
"debug",
"d",
config.Flags["debug"].DefValue.(bool),
"Enable debug logging")
rootCmd.PersistentFlags().String(
"logFile",
config.Flags["logFile"].DefValue.(string),
"Write logs to file")
// Address settings
rootCmd.PersistentFlags().String(
"addrApi",
config.Flags["addrApi"].DefValue.(string),
"Hub API listen address")
rootCmd.PersistentFlags().String(
"addrApiProxy",
config.Flags["addrApiProxy"].DefValue.(string),
"Hub API proxy listen address")
rootCmd.PersistentFlags().String(
"addrThreadsHost",
config.Flags["addrThreadsHost"].DefValue.(string),
"Threads peer host listen address")
rootCmd.PersistentFlags().String(
"addrGatewayHost",
config.Flags["addrGatewayHost"].DefValue.(string),
"Local gateway host address")
rootCmd.PersistentFlags().String(
"addrGatewayUrl",
config.Flags["addrGatewayUrl"].DefValue.(string),
"Public gateway address")
rootCmd.PersistentFlags().String(
"addrIpfsApi",
config.Flags["addrIpfsApi"].DefValue.(string),
"IPFS API address")
rootCmd.PersistentFlags().String(
"addrPowergateApi",
config.Flags["addrPowergateApi"].DefValue.(string),
"Powergate API address")
rootCmd.PersistentFlags().String(
"addrMongoUri",
config.Flags["addrMongoUri"].DefValue.(string),
"MongoDB connection URI")
// Gateway settings
rootCmd.PersistentFlags().Bool(
"gatewaySubdomains",
config.Flags["gatewaySubdomains"].DefValue.(bool),
"Enable gateway namespace redirects to subdomains")
// DNS settings
rootCmd.PersistentFlags().String(
"dnsDomain",
config.Flags["dnsDomain"].DefValue.(string),
"Root domain for bucket subdomains")
rootCmd.PersistentFlags().String(
"dnsZoneID",
config.Flags["dnsZoneID"].DefValue.(string),
"Cloudflare ZoneID for dnsDomain")
rootCmd.PersistentFlags().String(
"dnsToken",
config.Flags["dnsDomain"].DefValue.(string),
"Cloudflare API Token for dnsDomain")
err := cmd.BindFlags(config.Viper, rootCmd, config.Flags)
cmd.ErrCheck(err)
}
func main() {
cmd.ErrCheck(rootCmd.Execute())
}
var rootCmd = &cobra.Command{
Use: daemonName,
Short: "Buckets daemon",
Long: `The Buckets daemon.`,
PersistentPreRun: func(c *cobra.Command, args []string) {
config.Viper.SetConfigType("yaml")
cmd.ExpandConfigVars(config.Viper, config.Flags)
if config.Viper.GetBool("log.debug") {
err := util.SetLogLevels(map[string]logging.LogLevel{
daemonName: logging.LevelDebug,
})
cmd.ErrCheck(err)
}
},
Run: func(c *cobra.Command, args []string) {
settings, err := json.MarshalIndent(config.Viper.AllSettings(), "", " ")
cmd.ErrCheck(err)
log.Debugf("loaded config: %s", string(settings))
addrApi := cmd.AddrFromStr(config.Viper.GetString("addr.api"))
addrApiProxy := cmd.AddrFromStr(config.Viper.GetString("addr.api_proxy"))
addrThreadsHost := cmd.AddrFromStr(config.Viper.GetString("addr.threads.host"))
addrIpfsApi := cmd.AddrFromStr(config.Viper.GetString("addr.ipfs.api"))
var addrPowergateApi ma.Multiaddr
if str := config.Viper.GetString("addr.powergate.api"); str != "" {
addrPowergateApi = cmd.AddrFromStr(str)
}
addrGatewayHost := cmd.AddrFromStr(config.Viper.GetString("addr.gateway.host"))
addrGatewayUrl := config.Viper.GetString("addr.gateway.url")
addrMongoUri := config.Viper.GetString("addr.mongo_uri")
dnsDomain := config.Viper.GetString("dns.domain")
dnsZoneID := config.Viper.GetString("dns.zone_id")
dnsToken := config.Viper.GetString("dns.token")
logFile := config.Viper.GetString("log.file")
if logFile != "" {
util.SetupDefaultLoggingConfig(logFile)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
textile, err := core.NewTextile(ctx, core.Config{
RepoPath: config.Viper.GetString("repo"),
AddrAPI: addrApi,
AddrAPIProxy: addrApiProxy,
AddrThreadsHost: addrThreadsHost,
AddrIPFSAPI: addrIpfsApi,
AddrGatewayHost: addrGatewayHost,
AddrGatewayURL: addrGatewayUrl,
AddrPowergateAPI: addrPowergateApi,
AddrMongoURI: addrMongoUri,
UseSubdomains: config.Viper.GetBool("gateway.subdomains"),
MongoName: "buckets",
DNSDomain: dnsDomain,
DNSZoneID: dnsZoneID,
DNSToken: dnsToken,
Debug: config.Viper.GetBool("log.debug"),
})
cmd.ErrCheck(err)
defer textile.Close(false)
textile.Bootstrap()
fmt.Println("Welcome to Buckets!")
fmt.Println("Your peer ID is " + textile.HostID().String())
select {}
},
}