-
Notifications
You must be signed in to change notification settings - Fork 390
/
main.go
143 lines (120 loc) · 3.55 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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"crypto/tls"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/internal/fpath"
"storj.io/storj/lib/uplink"
"storj.io/storj/linksharing"
"storj.io/storj/linksharing/httpserver"
"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/process"
)
// LinkSharing defines link sharing configuration
type LinkSharing struct {
Address string `user:"true" help:"public address to listen on" devDefault:"localhost:8080" releaseDefault:":8443"`
CertFile string `user:"true" help:"server certificate file" devDefault:"" releaseDefault:"server.crt.pem"`
KeyFile string `user:"true" help:"server key file" devDefault:"" releaseDefault:"server.key.pem"`
PublicURL string `user:"true" help:"public url for the server" devDefault:"http://localhost:8080" releaseDefault:""`
}
var (
rootCmd = &cobra.Command{
Use: "link sharing service",
Short: "Link Sharing Service",
}
runCmd = &cobra.Command{
Use: "run",
Short: "Run the link sharing service",
RunE: cmdRun,
}
setupCmd = &cobra.Command{
Use: "setup",
Short: "Create config files",
RunE: cmdSetup,
Annotations: map[string]string{"type": "setup"},
}
runCfg LinkSharing
setupCfg LinkSharing
confDir string
)
func init() {
defaultConfDir := fpath.ApplicationDir("storj", "linksharing")
cfgstruct.SetupFlag(zap.L(), rootCmd, &confDir, "config-dir", defaultConfDir, "main directory for link sharing configuration")
defaults := cfgstruct.DefaultsFlag(rootCmd)
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(setupCmd)
process.Bind(runCmd, &runCfg, defaults, cfgstruct.ConfDir(confDir))
process.Bind(setupCmd, &setupCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.SetupMode())
}
func cmdRun(cmd *cobra.Command, args []string) (err error) {
ctx, _ := process.Ctx(cmd)
log := zap.L()
uplink, err := uplink.NewUplink(ctx, nil)
if err != nil {
return err
}
tlsConfig, err := configureTLS(runCfg.CertFile, runCfg.KeyFile)
if err != nil {
return err
}
handler, err := linksharing.NewHandler(log, linksharing.HandlerConfig{
Uplink: uplink,
URLBase: runCfg.PublicURL,
})
if err != nil {
return err
}
server, err := httpserver.New(log, httpserver.Config{
Name: "Link Sharing",
Address: runCfg.Address,
Handler: handler,
TLSConfig: tlsConfig,
ShutdownTimeout: -1,
})
if err != nil {
return err
}
return server.Run(ctx)
}
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
setupDir, err := filepath.Abs(confDir)
if err != nil {
return err
}
valid, _ := fpath.IsValidSetupDir(setupDir)
if !valid {
return fmt.Errorf("link sharing configuration already exists (%v)", setupDir)
}
err = os.MkdirAll(setupDir, 0700)
if err != nil {
return err
}
return process.SaveConfig(cmd, filepath.Join(setupDir, "config.yaml"))
}
func configureTLS(certFile, keyFile string) (*tls.Config, error) {
switch {
case certFile != "" && keyFile != "":
case certFile == "" && keyFile == "":
return nil, nil
case certFile != "" && keyFile == "":
return nil, errs.New("key file must be provided with cert file")
case certFile == "" && keyFile != "":
return nil, errs.New("cert file must be provided with key file")
}
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, errs.New("unable to load server keypair: %v", err)
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
}, nil
}
func main() {
process.Exec(rootCmd)
}