Skip to content

Commit

Permalink
Merge pull request #15 from teamhanko/use-koanf
Browse files Browse the repository at this point in the history
use konaf instead of viper
  • Loading branch information
FreddyDevelop authored Apr 29, 2022
2 parents 336e1c3 + 22c8f68 commit ca0fcfa
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 260 deletions.
4 changes: 2 additions & 2 deletions cmd/jwk/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/teamhanko/hanko/config"
"github.com/teamhanko/hanko/crypto/jwk"
"github.com/teamhanko/hanko/persistence"
"log"
)

func NewCreateCommand(cfg *config.Config, persister persistence.Persister) *cobra.Command {
Expand All @@ -18,8 +19,7 @@ func NewCreateCommand(cfg *config.Config, persister persistence.Persister) *cobr
jwkPersister := persister.GetJwkPersister()
_, err := jwk.NewDefaultManager(cfg.Secrets.Keys, jwkPersister)
if err != nil {
fmt.Println(err)
return
log.Fatal(err)
}
},
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/migrate/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/spf13/cobra"
"github.com/teamhanko/hanko/persistence"
"os"
"log"
"strconv"
)

Expand All @@ -26,8 +26,7 @@ func NewMigrateDownCommand(persister persistence.Migrator) *cobra.Command {
fmt.Println("migrate down called")
err := persister.MigrateDown(steps)
if err != nil {
fmt.Println(err)
os.Exit(1)
log.Fatal(err)
}
},
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/migrate/up.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package migrate

import (
"fmt"
"github.com/spf13/cobra"
"github.com/teamhanko/hanko/persistence"
"os"
"log"
)

func NewMigrateUpCommand(persister persistence.Migrator) *cobra.Command {
Expand All @@ -15,8 +14,7 @@ func NewMigrateUpCommand(persister persistence.Migrator) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
err := persister.MigrateUp()
if err != nil {
fmt.Println(err)
os.Exit(1)
log.Fatal(err)
}
},
}
Expand Down
15 changes: 9 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/teamhanko/hanko/cmd/serve"
"github.com/teamhanko/hanko/config"
"github.com/teamhanko/hanko/persistence"
"os"
"log"
)

var (
Expand All @@ -28,12 +28,11 @@ func NewRootCmd() *cobra.Command {
cmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file")
err := initConfig()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
log.Fatal(err)
}
err = initPersister()
if err != nil {
os.Exit(1)
log.Fatal(err)
}
migrate.RegisterCommands(cmd, persister)
serve.RegisterCommands(cmd, cfg, persister)
Expand All @@ -49,12 +48,16 @@ func Execute() {

err := cmd.Execute()
if err != nil {
os.Exit(1)
log.Fatal(err)
}
}

func initConfig() error {
cfg = config.Load(&cfgFile)
var err error
cfg, err = config.Load(&cfgFile)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
return cfg.Validate()
}

Expand Down
66 changes: 33 additions & 33 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package config
import (
"errors"
"fmt"
"github.com/spf13/viper"
"path/filepath"
"runtime"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"log"
"strings"
)

Expand All @@ -20,39 +22,37 @@ type Config struct {
Cookies Cookie
}

// Load loads config from given file or default places
func Load(cfgFile *string) *Config {
func Load(cfgFile *string) (*Config, error) {
k := koanf.New(".")
var err error
if cfgFile != nil && *cfgFile != "" {
// Use given config file
viper.SetConfigFile(*cfgFile)
if err = k.Load(file.Provider(*cfgFile), yaml.Parser()); err == nil {
log.Println("Using config file:", *cfgFile)
} else {
log.Println("Failed to load config from:", *cfgFile)
}
} else {
// Use config file from default places
// Get base path of binary call
_, b, _, _ := runtime.Caller(0)
basePath := filepath.Dir(b)

viper.SetConfigType("yaml")
viper.AddConfigPath(basePath)
viper.AddConfigPath("/etc/config")
viper.AddConfigPath("/etc/secrets")
viper.AddConfigPath("./config")
viper.SetConfigName("hanko-config")
if err = k.Load(file.Provider("./config/config.yaml"), yaml.Parser()); err == nil {
log.Println("Using config file:", "./config/config.yaml")
} else {
log.Println("failed to load config from:", "./config/config.yaml")
}
}

viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

var err error
if err = viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
err = k.Load(env.Provider("", ".", func(s string) string {
return strings.Replace(strings.ToLower(s), "_", ".", -1)
}), nil)
if err != nil {
log.Println("failed to load config from env vars")
}

c := defaultConfig()
err = viper.Unmarshal(c)
err = k.Unmarshal("", c)
if err != nil {
panic(fmt.Sprintf("unable to decode config into struct, %v", err))
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}

return c
return c, nil
}

func defaultConfig() *Config {
Expand Down Expand Up @@ -136,8 +136,8 @@ type Password struct {

type Cookie struct {
Domain string
HttpOnly bool `mapstructure:"http_only"`
SameSite string `mapstructure:"same_site"`
HttpOnly bool `koanf:"http_only"`
SameSite string `koanf:"same_site"`
}

type ServerSettings struct {
Expand All @@ -155,7 +155,7 @@ func (s *ServerSettings) Validate() error {

// WebauthnSettings defines the settings for the webauthn authentication mechanism
type WebauthnSettings struct {
RelyingParty RelyingParty `mapstructure:"relying_party"`
RelyingParty RelyingParty `koanf:"relying_party"`
Timeout int
}

Expand All @@ -167,7 +167,7 @@ func (r *WebauthnSettings) Validate() error {
// RelyingParty webauthn settings for your application using hanko.
type RelyingParty struct {
Id string
DisplayName string `mapstructure:"display_name"`
DisplayName string `koanf:"display_name"`
Icon string
Origin string
}
Expand Down Expand Up @@ -197,8 +197,8 @@ func (s *SMTP) Validate() error {
}

type Email struct {
FromAddress string `mapstructure:"from_address"`
FromName string `mapstructure:"from_name"`
FromAddress string `koanf:"from_address"`
FromName string `koanf:"from_name"`
}

func (e *Email) Validate() error {
Expand Down
16 changes: 16 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
database:
user: hanko
password: hanko
host: localhost
port: 5432
dialect: postgres
passcode:
email:
from_address: [email protected]
smtp:
host: smtp.example.com
user: example
password: example
secrets:
keys:
- abcedfghijklmnopqrstuvwxyz
5 changes: 0 additions & 5 deletions config/quickstart.yaml

This file was deleted.

14 changes: 7 additions & 7 deletions deploy/docker-compose/quickstart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ services:
image: ghcr.io/teamhanko/hanko:main
volumes:
- type: bind
source: ../../config/quickstart.yaml
target: /etc/config/hanko-config.yaml
command: --config /etc/config/hanko-config.yaml migrate up
source: ../../config/config.yaml
target: /etc/config/config.yaml
command: --config /etc/config/config.yaml migrate up
restart: on-failure
depends_on:
- postgresd
Expand All @@ -20,11 +20,11 @@ services:
- '8000:8000' # public
- '8001:8001' # private
restart: unless-stopped
command: serve --config /etc/config/hanko-config.yaml
command: serve --config /etc/config/config.yaml
volumes:
- type: bind
source: ../../config/quickstart.yaml
target: /etc/config/hanko-config.yaml
source: ../../config/config.yaml
target: /etc/config/config.yaml
networks:
- intranet
postgresd:
Expand All @@ -45,4 +45,4 @@ services:
# networks:
# - intranet
networks:
intranet:
intranet:
11 changes: 3 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ require (
github.com/gobuffalo/pop/v6 v6.0.1
github.com/gobuffalo/validate/v3 v3.3.1
github.com/gofrs/uuid v4.2.0+incompatible
github.com/knadh/koanf v1.4.1
github.com/labstack/echo/v4 v4.7.2
github.com/lestrrat-go/jwx/v2 v2.0.0-beta2
github.com/nicksnyder/go-i18n/v2 v2.2.0
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.1
golang.org/x/text v0.3.7
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
Expand Down Expand Up @@ -42,7 +42,6 @@ require (
github.com/golang-jwt/jwt/v4 v4.4.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.1 // indirect
Expand All @@ -63,23 +62,20 @@ require (
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/option v1.0.0 // indirect
github.com/luna-duclos/instrumentedsql v1.1.3 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/microcosm-cc/bluemonday v1.0.16 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d // indirect
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand All @@ -90,6 +86,5 @@ require (
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit ca0fcfa

Please sign in to comment.