forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
166 lines (142 loc) · 4.24 KB
/
server.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
package cmd
import (
"fmt"
"os"
"path"
"github.com/MakeNowJust/heredoc"
"github.com/goto/shield/config"
"github.com/goto/shield/internal/store/postgres/migrations"
"github.com/goto/shield/pkg/db"
shieldlogger "github.com/goto/shield/pkg/logger"
"github.com/spf13/cobra"
cli "github.com/spf13/cobra"
)
func ServerCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "server <command>",
Aliases: []string{"s"},
Short: "Server management",
Long: "Server management commands.",
Example: heredoc.Doc(`
$ shield server init
$ shield server start
$ shield server start -c ./config.yaml
$ shield server migrate
$ shield server migrate -c ./config.yaml
$ shield server migrate-rollback
$ shield server migrate-rollback -c ./config.yaml
`),
}
cmd.AddCommand(serverInitCommand())
cmd.AddCommand(serverStartCommand())
cmd.AddCommand(serverMigrateCommand())
cmd.AddCommand(serverMigrateRollbackCommand())
return cmd
}
func serverInitCommand() *cobra.Command {
var configFile string
var resourcesURL string
var rulesURL string
c := &cli.Command{
Use: "init",
Short: "Initialize server",
Long: heredoc.Doc(`
Initializing server. Creating a sample of shield server config.
Default: ./config.yaml
`),
Example: "shield server init",
RunE: func(cmd *cli.Command, args []string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
defaultResourcesURL := fmt.Sprintf("file://%s", path.Join(cwd, "resources_config"))
defaultRulesURL := fmt.Sprintf("file://%s", path.Join(cwd, "rules"))
if resourcesURL == "" {
resourcesURL = defaultResourcesURL
}
if rulesURL == "" {
rulesURL = defaultRulesURL
}
if err := config.Init(resourcesURL, rulesURL, configFile); err != nil {
return err
}
fmt.Printf("server config created: %v\n", configFile)
return nil
},
}
c.Flags().StringVarP(&configFile, "output", "o", "./config.yaml", "Output config file path")
c.Flags().StringVarP(&resourcesURL, "resources", "r", "", heredoc.Doc(`
URL path of resources. Full path prefixed with scheme where resources config yaml files are kept
e.g.:
local storage file "file:///tmp/resources_config"
GCS Bucket "gs://shield-bucket-example"
(default: file://{pwd}/resources_config)
`))
c.Flags().StringVarP(&rulesURL, "rule", "u", "", heredoc.Doc(`
URL path of rules. Full path prefixed with scheme where ruleset yaml files are kept
e.g.:
local storage file "file:///tmp/rules"
GCS Bucket "gs://shield-bucket-example"
(default: file://{pwd}/rules)
`))
return c
}
func serverStartCommand() *cobra.Command {
var configFile string
c := &cli.Command{
Use: "start",
Short: "Start server and proxy default on port 8080",
Example: "shield server start",
RunE: func(cmd *cli.Command, args []string) error {
appConfig, err := config.Load(configFile)
if err != nil {
panic(err)
}
logger := shieldlogger.InitLogger(shieldlogger.Config{Level: appConfig.Log.Level})
return StartServer(logger, appConfig)
},
}
c.Flags().StringVarP(&configFile, "config", "c", "", "Config file path")
return c
}
func serverMigrateCommand() *cobra.Command {
var configFile string
c := &cli.Command{
Use: "migrate",
Short: "Run DB Schema Migrations",
Example: "shield migrate",
RunE: func(c *cli.Command, args []string) error {
appConfig, err := config.Load(configFile)
if err != nil {
panic(err)
}
return db.RunMigrations(db.Config{
Driver: appConfig.DB.Driver,
URL: appConfig.DB.URL,
}, migrations.MigrationFs, migrations.ResourcePath)
},
}
c.Flags().StringVarP(&configFile, "config", "c", "", "Config file path")
return c
}
func serverMigrateRollbackCommand() *cobra.Command {
var configFile string
c := &cli.Command{
Use: "migration-rollback",
Short: "Run DB Schema Migrations Rollback to last state",
Example: "shield migration-rollback",
RunE: func(c *cli.Command, args []string) error {
appConfig, err := config.Load(configFile)
if err != nil {
panic(err)
}
return db.RunRollback(db.Config{
Driver: appConfig.DB.Driver,
URL: appConfig.DB.URL,
}, migrations.MigrationFs, migrations.ResourcePath)
},
}
c.Flags().StringVarP(&configFile, "config", "c", "", "Config file path")
return c
}