initial commit
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/caarlos0/env/v11"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Conf struct {
|
||||
Server Server
|
||||
Postgres Postgres
|
||||
Redis Redis
|
||||
Client Client
|
||||
JWT JWT
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Addr string `env:"SERVER_ADDR" envDefault:":8080"`
|
||||
FrontendURL string `env:"FRONTEND_URL,required"`
|
||||
}
|
||||
|
||||
type Postgres struct {
|
||||
URL string `env:"POSTGRES_URL,required"`
|
||||
MigrationFilePath string `env:"POSTGRES_MIGRATION_FILE_PATH,required"`
|
||||
}
|
||||
|
||||
type Redis struct {
|
||||
URL string `env:"REDIS_URL,required"`
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
ID string `env:"CLIENT_ID,required"`
|
||||
Secret string `env:"CLIENT_SECRET,required"`
|
||||
Name string `env:"CLIENT_NAME" envDefault:"Basic Client"`
|
||||
RedirectURIs []string `env:"CLIENT_REDIRECT_URIS" envSeparator:","`
|
||||
GrantTypes []string `env:"CLIENT_GRANT_TYPES" envSeparator:"," envDefault:"refresh_token"`
|
||||
Scopes []string `env:"CLIENT_SCOPES" envSeparator:"," envDefault:"profile:read,profile:edit,profile:consent:read,profile:consent:delete,auth:register"`
|
||||
}
|
||||
|
||||
type JWT struct {
|
||||
Secret string `env:"JWT_SECRET,required"`
|
||||
TTL time.Duration `env:"JWT_TTL" envDefault:"24h"`
|
||||
}
|
||||
|
||||
func New() (*Conf, error) {
|
||||
_ = godotenv.Load()
|
||||
|
||||
var conf Conf
|
||||
if err := env.Parse(&conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &conf, nil
|
||||
}
|
||||
|
||||
func (c *Client) RedirectURIsJSON() string {
|
||||
if len(c.RedirectURIs) == 0 {
|
||||
return "[]"
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("[\"")
|
||||
b.WriteString(strings.Join(c.RedirectURIs, "\",\""))
|
||||
b.WriteString("\"]")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (c *Client) GrantTypesJSON() string {
|
||||
if len(c.GrantTypes) == 0 {
|
||||
return "[]"
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("[\"")
|
||||
b.WriteString(strings.Join(c.GrantTypes, "\",\""))
|
||||
b.WriteString("\"]")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (c *Client) ScopesJSON() string {
|
||||
if len(c.Scopes) == 0 {
|
||||
return "[]"
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("[\"")
|
||||
b.WriteString(strings.Join(c.Scopes, "\",\""))
|
||||
b.WriteString("\"]")
|
||||
return b.String()
|
||||
}
|
||||
Reference in New Issue
Block a user