Skip to content

Commit

Permalink
config: use function instead of global variable (with the help of tem…
Browse files Browse the repository at this point in the history
…plates)

Change-Id: Ice6ea9086da5639d5fe79ffe4feb74a9de703c80
  • Loading branch information
elek authored and Elek, Márton committed Sep 13, 2022
1 parent 53e945d commit f123e22
Show file tree
Hide file tree
Showing 13 changed files with 2,079 additions and 1,023 deletions.
6 changes: 4 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ func printConfigs(services []string) error {
}

emptySelection := true
allOptions := config.All()

for _, s := range resolvedServices {
if configs, found := config.Config[s]; found {
if configs, found := allOptions[s]; found {
printConfigStruct(configs)
fmt.Println()
emptySelection = false
Expand All @@ -51,7 +53,7 @@ func printConfigs(services []string) error {
return errs.New("Couldn't find config type with selector %s. "+
"Command is supported for the following services: %s",
strings.Join(services, ","),
strings.Join(keys(config.Config), ", "))
strings.Join(keys(allOptions), ", "))
}
return nil
}
Expand Down
16 changes: 16 additions & 0 deletions cmd/config/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.

package config

// All returns with all known options for registered services.
func All() map[string][]Option {
return map[string][]Option{
"authservice": authserviceConfig(),
"linksharing": linksharingConfig(),
"satellite-admin": satelliteadminConfig(),
"satellite-api": satelliteapiConfig(),
"satellite-core": satellitecoreConfig(),
"storagenode": storagenodeConfig(),
}
}
71 changes: 47 additions & 24 deletions cmd/config/authservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,120 @@

package config

func init() {
Config["authservice"] = []Option{
func authserviceConfig() []Option {
return []Option{

{
Name: "STORJ_ENDPOINT",
Description: "Gateway endpoint URL to return to clients",
Default: "",
}, {
},
{
Name: "STORJ_AUTH_TOKEN",
Description: "auth security token to validate requests",
Default: "",
}, {
},
{
Name: "STORJ_ALLOWED_SATELLITES",
Description: "list of satellite NodeURLs allowed for incoming access grants",
Default: "https://www.storj.io/dcs-satellites",
}, {
},
{
Name: "STORJ_CACHE_EXPIRATION",
Description: "length of time satellite addresses are cached for",
Default: "10m",
}, {
},
{
Name: "STORJ_GET_ACCESS_RATE_LIMITERS_MAX_REQS_SECOND",
Description: "maximum number of allowed operations per second starting when first failure operation happens",
Default: "2",
}, {
},
{
Name: "STORJ_GET_ACCESS_RATE_LIMITERS_BURST",
Description: "maximum number of allowed operations to overpass the maximum operations per second",
Default: "3",
}, {
},
{
Name: "STORJ_GET_ACCESS_RATE_LIMITERS_NUM_LIMITS",
Description: "maximum number of keys/rate-limit pairs stored in the LRU cache",
Default: "1000",
}, {
},
{
Name: "STORJ_GET_ACCESS_RATE_LIMITERS_ENABLED",
Description: "indicates if rate-limiting for GetAccess endpoints is enabled",
Default: "false",
}, {
},
{
Name: "STORJ_KVBACKEND",
Description: "key/value store backend url",
Default: "",
}, {
},
{
Name: "STORJ_MIGRATION",
Description: "create or update the database schema, and then continue service startup",
Default: "false",
}, {
},
{
Name: "STORJ_LISTEN_ADDR",
Description: "public HTTP address to listen on",
Default: ":20000",
}, {
},
{
Name: "STORJ_LISTEN_ADDR_TLS",
Description: "public HTTPS address to listen on",
Default: ":20001",
}, {
},
{
Name: "STORJ_DRPCLISTEN_ADDR",
Description: "public DRPC address to listen on",
Default: ":20002",
}, {
},
{
Name: "STORJ_DRPCLISTEN_ADDR_TLS",
Description: "public DRPC+TLS address to listen on",
Default: ":20003",
}, {
},
{
Name: "STORJ_LETS_ENCRYPT",
Description: "use lets-encrypt to handle TLS certificates",
Default: "false",
}, {
},
{
Name: "STORJ_CERT_FILE",
Description: "server certificate file",
Default: "",
}, {
},
{
Name: "STORJ_KEY_FILE",
Description: "server key file",
Default: "",
}, {
},
{
Name: "STORJ_PUBLIC_URL",
Description: "public url for the server, for the TLS certificate",
Default: "",
}, {
},
{
Name: "STORJ_DELETE_UNUSED_RUN",
Description: "whether to run unused records deletion chore",
Default: "false",
}, {
},
{
Name: "STORJ_DELETE_UNUSED_INTERVAL",
Description: "interval unused records deletion chore waits to start next iteration",
Default: "24h",
}, {
},
{
Name: "STORJ_DELETE_UNUSED_AS_OF_SYSTEM_INTERVAL",
Description: "the interval specified in AS OF SYSTEM in unused records deletion chore query as negative interval",
Default: "5s",
}, {
},
{
Name: "STORJ_DELETE_UNUSED_SELECT_SIZE",
Description: "batch size of records selected for deletion at a time",
Default: "10000",
}, {
},
{
Name: "STORJ_DELETE_UNUSED_DELETE_SIZE",
Description: "batch size of records to delete from selected records at a time",
Default: "1000",
Expand Down
3 changes: 0 additions & 3 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

package config

// Config contains all possible configuration for each known services.
var Config = map[string][]Option{}

// Option represents one possible configuration options for a service.
type Option struct {
Name string
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

package config

//go:generate go run ./gen/.
//go:generate go run ./gen/. ./gen
10 changes: 10 additions & 0 deletions cmd/config/gen/all.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.

package config

func All() map[string][]Option {
return map[string][]Option{ {{ range $name, $def := .Configs }}
"{{ $name }}": {{ $name | goName }}Config(),{{ end }}
}
}
92 changes: 64 additions & 28 deletions cmd/config/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ package main
import (
"fmt"
"os"
"path"
"reflect"
"regexp"
"strings"
"text/template"

"github.com/zeebo/errs"

"storj.io/gateway-mt/pkg/auth"
"storj.io/gateway-mt/pkg/linksharing"
"storj.io/storj-up/cmd/config"
"storj.io/storj/satellite"
"storj.io/storj/storagenode"
)
Expand All @@ -28,16 +31,24 @@ var configTypes = map[string]reflect.Type{
}

func main() {
templateDir := "."
if len(os.Args) > 1 {
templateDir = os.Args[1]
}
for name, t := range configTypes {
err := generate(name, t)
err := generateSingle(templateDir, name, t)
if err != nil {
panic(err)
}
}
err := generateCombiner(templateDir, configTypes)
if err != nil {
panic(err)
}
}

func generate(name string, t reflect.Type) error {
fileName := name + ".go"
func generateCombiner(templateDir string, types map[string]reflect.Type) error {
fileName := "all.go"
fmt.Println("Writing " + fileName)
f, err := os.Create(fileName)
if err != nil {
Expand All @@ -47,56 +58,81 @@ func generate(name string, t reflect.Type) error {
_ = f.Close()
}()

_, err = f.WriteString(`// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package config
func init() {
`)
t, err := template.New("all.tpl").
Funcs(map[string]interface{}{
"goName": goName,
}).
ParseFiles(path.Join(templateDir, "all.tpl"))
if err != nil {
return errs.Wrap(err)
}

fmt.Fprintf(f, "\tConfig[\"%s\"] = []ConfigKey{\n\t\t", name)
err = t.Execute(f, struct {
Configs map[string]reflect.Type
}{
Configs: types,
})
return err
}

func generateSingle(templateDir string, name string, root reflect.Type) error {
fileName := name + ".go"
fmt.Println("Writing " + fileName)
f, err := os.Create(fileName)
if err != nil {
return errs.Wrap(err)
}
defer func() {
_ = f.Close()
}()

err = writeConfigStruct(f, "STORJ", t)
t, err := template.New("single.tpl").
Funcs(map[string]interface{}{
"goName": goName,
}).
ParseFiles(path.Join(templateDir, "single.tpl"))
if err != nil {
return errs.Wrap(err)
}
_, err = f.WriteString(`
}
}
`)

options, err := collectOptions("STORJ", root)
if err != nil {
return errs.Wrap(err)
}
return nil

err = t.Execute(f, struct {
Name string
Options []config.Option
}{
Name: name,
Options: options,
})

return err
}

func goName(name string) string {
return strings.ReplaceAll(name, "-", "")
}

func writeConfigStruct(f *os.File, prefix string, configType reflect.Type) error {
func collectOptions(prefix string, configType reflect.Type) (res []config.Option, err error) {
for i := 0; i < configType.NumField(); i++ {
field := configType.Field(i)
if field.Type.Kind() == reflect.Struct {
err := writeConfigStruct(f, prefix+"_"+camelToUpperCase(field.Name), field.Type)
r, err := collectOptions(prefix+"_"+camelToUpperCase(field.Name), field.Type)
if err != nil {
return errs.Wrap(err)
return res, errs.Wrap(err)
}
res = append(res, r...)
} else {
name := prefix + "_" + camelToUpperCase(field.Name)
_, err := fmt.Fprintf(f, `{
Name: "%s",
Description: "%s",
Default: "%s",
}, `, name, safe(field.Tag.Get("help")), safe(field.Tag.Get("default")))
if err != nil {
return errs.Wrap(err)
}
res = append(res, config.Option{
Name: name,
Description: safe(field.Tag.Get("help")),
Default: safe(field.Tag.Get("default"))})
}
}
return nil
return res, nil
}

func safe(s string) string {
Expand Down
15 changes: 15 additions & 0 deletions cmd/config/gen/single.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.

package config

func {{ .Name | goName }}Config() []Option {
return []Option{
{{ range .Options}}
{
Name: "{{ .Name }}",
Description: "{{ .Description }}",
Default: "{{ .Default }}",
},{{ end }}
}
}
Loading

0 comments on commit f123e22

Please sign in to comment.