feat(example): Allow configuring some parameters with env variables
This commit is contained in:
parent
5ae555e191
commit
73d827bf88
6 changed files with 262 additions and 21 deletions
|
@ -5,20 +5,33 @@ import (
|
|||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/zitadel/oidc/v3/example/server/config"
|
||||
"github.com/zitadel/oidc/v3/example/server/exampleop"
|
||||
"github.com/zitadel/oidc/v3/example/server/storage"
|
||||
)
|
||||
|
||||
func getUserStore(cfg *config.Config) (storage.UserStore, error) {
|
||||
if cfg.UsersFile == "" {
|
||||
return storage.NewUserStore(fmt.Sprintf("http://localhost:%s/", cfg.Port)), nil
|
||||
}
|
||||
return storage.StoreFromFile(cfg.UsersFile)
|
||||
}
|
||||
|
||||
func main() {
|
||||
//we will run on :9998
|
||||
port := "9998"
|
||||
cfg := config.FromEnvVars(&config.Config{Port: "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/", port)
|
||||
issuer := fmt.Sprintf("http://localhost:%s/", cfg.Port)
|
||||
|
||||
storage.RegisterClients(
|
||||
storage.NativeClient("native", strings.Split(os.Getenv("REDIRECT_URI"), ",")...),
|
||||
storage.NativeClient("native", cfg.RedirectURI...),
|
||||
storage.WebClient("web", "secret"),
|
||||
storage.WebClient("api", "secret"),
|
||||
)
|
||||
|
@ -26,23 +39,20 @@ func main() {
|
|||
// the OpenIDProvider interface needs a Storage interface handling various checks and state manipulations
|
||||
// this might be the layer for accessing your database
|
||||
// in this example it will be handled in-memory
|
||||
storage := storage.NewStorage(storage.NewUserStore(issuer))
|
||||
|
||||
logger := slog.New(
|
||||
slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
|
||||
AddSource: true,
|
||||
Level: slog.LevelDebug,
|
||||
}),
|
||||
)
|
||||
store, err := getUserStore(cfg)
|
||||
if err != nil {
|
||||
logger.Error("cannot create UserStore", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
storage := storage.NewStorage(store)
|
||||
router := exampleop.SetupServer(issuer, storage, logger, false)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: ":" + port,
|
||||
Addr: ":" + cfg.Port,
|
||||
Handler: router,
|
||||
}
|
||||
logger.Info("server listening, press ctrl+c to stop", "addr", fmt.Sprintf("http://localhost:%s/", port))
|
||||
err := server.ListenAndServe()
|
||||
if err != http.ErrServerClosed {
|
||||
logger.Info("server listening, press ctrl+c to stop", "addr", issuer)
|
||||
if server.ListenAndServe() != http.ErrServerClosed {
|
||||
logger.Error("server terminated", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue