|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "log/slog" |
| 6 | + "reflect" |
| 7 | + |
| 8 | + "github.com/caarlos0/env/v11" |
| 9 | + "github.com/jackc/pgx/v5/pgxpool" |
| 10 | +) |
| 11 | + |
| 12 | +type Config struct { |
| 13 | + Host string `env:"HOST" envDefault:"localhost"` |
| 14 | + Port string `env:"PORT" envDefault:"80"` |
| 15 | + LogLevel slog.Level `env:"LOG_LEVEL" envDefault:"error"` |
| 16 | + |
| 17 | + RedirectDIDTemplate URLTemplate `env:"REDIRECT_DID_TEMPLATE" envDefault:"https://bsky.app/profile/{did}"` |
| 18 | + RedirectHandleTemplate URLTemplate `env:"REDIRECT_HANDLE_TEMPLATE" envDefault:"https://{handle.domain}?handle={handle}"` |
| 19 | + |
| 20 | + Postgres *pgxpool.Config `env:"DATABASE_URL"` |
| 21 | + PostgresDidsTable string `env:"DATABASE_TABLE_DIDS" envDefault:"dids"` |
| 22 | + PostgresDomainsTable string `env:"DATABASE_TABLE_DOMAINS" envDefault:"domains"` |
| 23 | + |
| 24 | + MemoryDids map[string]string `env:"MEMORY_DIDS" envKeyValSeparator:"@"` |
| 25 | + MemoryDomains []string `env:"MEMORY_DOMAINS"` |
| 26 | + |
| 27 | + Provider ProvidesDecentralizedIDs `env:"DID_PROVIDER,required"` |
| 28 | +} |
| 29 | + |
| 30 | +func ConfigFromEnvironment() (Config, error) { |
| 31 | + config := Config{} |
| 32 | + |
| 33 | + err := env.ParseWithOptions(&config, env.Options{ |
| 34 | + FuncMap: map[reflect.Type]env.ParserFunc{ |
| 35 | + reflect.TypeFor[slog.Level](): func(v string) (interface{}, error) { |
| 36 | + var level slog.Level |
| 37 | + return level, level.UnmarshalText([]byte(v)) |
| 38 | + }, |
| 39 | + reflect.TypeFor[pgxpool.Config](): func(v string) (interface{}, error) { |
| 40 | + config, err := pgxpool.ParseConfig(v) |
| 41 | + return *config, err |
| 42 | + }, |
| 43 | + reflect.TypeFor[ProvidesDecentralizedIDs](): func(v string) (interface{}, error) { |
| 44 | + switch v { |
| 45 | + case "postgres": |
| 46 | + if config.Postgres == nil { |
| 47 | + return &PostgresHandles{}, errors.New("a database connection (`DATABASE_URL`) is required to use the postgres provider") |
| 48 | + } |
| 49 | + |
| 50 | + return NewPostgresHandlesProvider( |
| 51 | + config.Postgres, |
| 52 | + config.PostgresDidsTable, |
| 53 | + config.PostgresDomainsTable, |
| 54 | + ) |
| 55 | + case "memory": |
| 56 | + if config.MemoryDids == nil || config.MemoryDomains == nil { |
| 57 | + return nil, errors.New("a map of Decentralized IDs (`MEMORY_DIDS`) and domains (`MEMORY_DOMAINS`) is required to use the memory provider") |
| 58 | + } |
| 59 | + |
| 60 | + dids := make(MapOfDids) |
| 61 | + |
| 62 | + for handle, did := range config.MemoryDids { |
| 63 | + dids[Hostname(handle)] = DecentralizedID(did) |
| 64 | + } |
| 65 | + |
| 66 | + domains := make(MapOfDomains) |
| 67 | + |
| 68 | + for _, domain := range config.MemoryDomains { |
| 69 | + domains[Domain(domain)] = true |
| 70 | + } |
| 71 | + |
| 72 | + provider := NewInMemoryProvider(dids, domains) |
| 73 | + return provider, nil |
| 74 | + default: |
| 75 | + return nil, errors.New("no valid provider of decentralized IDs specified") |
| 76 | + } |
| 77 | + }, |
| 78 | + }, |
| 79 | + }) |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + return Config{}, err |
| 83 | + } |
| 84 | + |
| 85 | + return config, nil |
| 86 | +} |
0 commit comments