Skip to content

Commit

Permalink
Use configurable issuer for test servers
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Rusakov committed Oct 10, 2024
1 parent 0410628 commit 11c53ac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
5 changes: 5 additions & 0 deletions example/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
Port string
RedirectURIs []string
UsersFile string
Issuer string
}

// FromEnvVars loads configuration parameters from environment variables.
Expand All @@ -26,6 +27,7 @@ func FromEnvVars(defaults *Config) *Config {
Port: defaults.Port,
RedirectURIs: defaults.RedirectURIs,
UsersFile: defaults.UsersFile,
Issuer: defaults.Issuer,
}
if value, ok := os.LookupEnv("PORT"); ok {
cfg.Port = value
Expand All @@ -36,5 +38,8 @@ func FromEnvVars(defaults *Config) *Config {
if value, ok := os.LookupEnv("REDIRECT_URIS"); ok {
cfg.RedirectURIs = strings.Split(value, ",")
}
if value, ok := os.LookupEnv("ISSUER"); ok {
cfg.Issuer = value
}
return cfg
}
5 changes: 5 additions & 0 deletions example/server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ func TestFromEnvVars(t *testing.T) {
Port: "6666",
UsersFile: "/default/user/path",
RedirectURIs: []string{"re", "direct", "uris"},
Issuer: "123",
},
want: &Config{
Port: "6666",
UsersFile: "/default/user/path",
RedirectURIs: []string{"re", "direct", "uris"},
Issuer: "123",
},
},
{
Expand All @@ -39,16 +41,19 @@ func TestFromEnvVars(t *testing.T) {
"PORT": "1234",
"USERS_FILE": "/path/to/users",
"REDIRECT_URIS": "http://redirect/redirect",
"ISSUER": "someissuer",
},
defaults: &Config{
Port: "6666",
UsersFile: "/default/user/path",
RedirectURIs: []string{"re", "direct", "uris"},
Issuer: "someissuer",
},
want: &Config{
Port: "1234",
UsersFile: "/path/to/users",
RedirectURIs: []string{"http://redirect/redirect"},
Issuer: "someissuer",
},
},
{
Expand Down
11 changes: 4 additions & 7 deletions example/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@ import (

func getUserStore(cfg *config.Config) (storage.UserStore, error) {
if cfg.UsersFile == "" {
return storage.NewUserStore(fmt.Sprintf("http://localhost:%s/", cfg.Port)), nil
return storage.NewUserStore(fmt.Sprintf("http://zitadel:%s/", cfg.Port)), nil
}
return storage.StoreFromFile(cfg.UsersFile)
}

func main() {
cfg := config.FromEnvVars(&config.Config{Port: "9998"})
cfg := config.FromEnvVars(&config.Config{Port: "9998", Issuer: "http://localhost:9998"})
logger := slog.New(
slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}),
)

//which gives us the issuer: http://localhost:9998/
issuer := fmt.Sprintf("http://localhost:%s/", cfg.Port)

storage.RegisterClients(
storage.NativeClient("native", cfg.RedirectURIs...),
storage.WebClient("web", "secret"),
Expand All @@ -45,13 +42,13 @@ func main() {
os.Exit(1)
}
storage := storage.NewStorage(store)
router := exampleop.SetupServer(issuer, storage, logger, false)
router := exampleop.SetupServer(cfg.Issuer, storage, logger, false)

server := &http.Server{
Addr: ":" + cfg.Port,
Handler: router,
}
logger.Info("server listening, press ctrl+c to stop", "addr", issuer)
logger.Info("server listening, press ctrl+c to stop", "addr", cfg.Issuer)
if server.ListenAndServe() != http.ErrServerClosed {
logger.Error("server terminated", "error", err)
os.Exit(1)
Expand Down

0 comments on commit 11c53ac

Please sign in to comment.