config

package
v0.0.0-...-7a2aee4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 7 Imported by: 0

README

Application configurations

Introduction

We use Koanf for managing configurations in our project. Koanf allows us to define default configuration values in the default.go file. If the necessary environment variables are not found, Koanf will fall back to these default values, ensuring that the application has sensible defaults in place.

Setting up configurations

Server Configuration
  • SERVER_PORT: The port number where the server listens for incoming requests.

    • Default: 8080
  • SERVER_PRODUCTION: Indicates the environment in which the application is running (true for production, false for development).

    • Default: false
  • SERVER_READ_TIMEOUT: Duration for reading requests.

    • Default: 5s
  • SERVER_WRITE_TIMEOUT: Duration for writing responses.

    • Default: 10s
  • SERVER_GRACEFUL_SHUTDOWN: Time to wait before forcefully terminating ongoing requests during shutdown.

    • Default: 30s
  • SERVER_DOMAIN: The domain on which the server is accessible.

    • Default: http://localhost:4000

OAuth Configuration

Google OAuth
  • OAUTH_GOOGLE_CLIENT_ID: Client ID for Google OAuth.

    • Default: "client-id"
  • OAUTH_GOOGLE_CLIENT_SECRET: Client Secret for Google OAuth.

    • Default: "secret"
  • OAUTH_GOOGLE_REDIRECT_URL: URL for redirecting users after successful authentication with Google.

    • Default: http://localhost:4000/api/v1/oauth/google/callback
  • OAUTH_GOOGLE_SCOPES: Scopes for Google OAuth permissions.

    • Default: "email,profile"
Microsoft OAuth
  • OAUTH_MICROSOFT_CLIENT_ID: Client ID for Microsoft OAuth.

    • Default: "client-id"
  • OAUTH_MICROSOFT_CLIENT_SECRET: Client Secret for Microsoft OAuth.

    • Default: "secret"
  • OAUTH_MICROSOFT_REDIRECT_URL: URL for redirecting users after successful authentication with Microsoft.

    • Default: http://localhost:4000/api/v1/oauth/microsoft/callback
  • OAUTH_MICROSOFT_SCOPES: Scopes for Microsoft OAuth permissions.

    • Default: "User.Read,openid"

Database Configuration

  • DB_HOST: Hostname or IP address of the database server.

    • Default: localhost
  • DB_PORT: Port number on which the database server is listening.

    • Default: 5432
  • DB_USER: Username for database authentication.

    • Default: root
  • DB_PASSWORD: Password for database authentication.

    • Default: root
  • DB_NAME: Name of the database.

    • Default: test
  • DB_MIGRATIONS: Whether to apply database migrations automatically on startup.

    • Default: false
  • DB_LOG_LEVEL: Log level for database operations.

    • Default: 2
  • DB_POOL_MAX_OPEN: Maximum number of open connections to the database.

    • Default: 10
  • DB_POOL_MAX_IDLE: Maximum number of idle connections in the pool.

    • Default: 5
  • DB_POOL_MAX_LIFETIME: Maximum time a connection may remain open.

    • Default: 5m

JWT Configuration

  • JWT_SECRET: Secret key for signing and verifying JSON Web Tokens (JWT).

    • Default: "secret"
  • JWT_ACCESS_TOKEN_EXP: Expiration time for access tokens.

    • Default: 900s (15 minutes)
  • JWT_REFRESH_TOKEN_EXP: Expiration time for refresh tokens.

    • Default: 604800s (7 days)

Logging Configuration

  • LOGGING_LEVEL: Verbosity level for logging.

    • Default: -1
  • LOGGING_ENCODING: Format of log output.

    • Default: console

AWS Configuration

  • AWS_REGION: AWS region for cloud resources.

    • Default: eu-west-2
  • AWS_SES_FROM_EMAIL: Default sender email address for AWS Simple Email Service (SES).

    • Default: "example.com"

Setting Environment Variables

To configure the application, set the environment variables as described above. You can set these variables in your environment or by using a .env file.

Example using .env file:
SERVER_PORT=8080
SERVER_PRODUCTION=false
OAUTH_GOOGLE_CLIENT_ID="your-google-client-id"
OAUTH_GOOGLE_CLIENT_SECRET="your-google-client-secret"
DB_HOST="localhost"
DB_PORT=5432
JWT_SECRET="your-secret-key"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AWSConfig

type AWSConfig struct {
	Region string `json:"region"`
}

AWSConfig represents the configuration for AWS services

type Config

type Config struct {
	Server  ServerConfig  `json:"server"`
	OAuth   OAuthConfig   `json:"oauth"`
	DB      DBConfig      `json:"db"`
	JWT     JWTConfig     `json:"jwt"`
	Logging LoggingConfig `json:"logging"`
	AWS     AWSConfig     `json:"aws"`
	Mail    MailConfig    `json:"mail"`
}

Config represents the configuration for the application It includes settings for the server, database, JWT, logging, and AWS services.

func LoadConfig

func LoadConfig() (*Config, error)

LoadConfig loads the application configuration from environment variables and default settings. It initializes configuration using a default set of values and overrides them with environment variables.

type DBConfig

type DBConfig struct {
	Host       string `json:"host"`
	Port       string `json:"port"`
	User       string `json:"user"`
	Password   string `json:"password"`
	Name       string `json:"name"`
	SSLMode    string `json:"ssl_mode"`
	LogLevel   int    `json:"log_level"`
	Migrations bool   `json:"migrations"`
	Pool       struct {
		MaxOpen     int           `json:"max_open"`
		MaxIdle     int           `json:"max_idle"`
		MaxLifetime time.Duration `json:"max_lifetime"`
	} `json:"pool"`
}

DBConfig represents the configuration for the database

type JWTConfig

type JWTConfig struct {
	Secret             string        `json:"secret"`
	RefreshTokenExpiry time.Duration `json:"refresh_token_exp"`
	AccessTokenExpiry  time.Duration `json:"access_token_exp"`
}

JWTConfig represents the configuration for the JWT

type LoggingConfig

type LoggingConfig struct {
	Level    int    `json:"level"`
	Encoding string `json:"encoding"`
}

LoggingConfig represents the configuration for logging

type MailConfig

type MailConfig struct {
	SMTP struct {
		Server   string `json:"server"`
		Port     int    `json:"port"`
		Username string `json:"username"`
		Password string `json:"password"`
	} `json:"smtp"`
	FromEmail string `json:"from_email"`
	Provider  string `json:"provider"`
}

MailConfig represents the email settings.

type OAuthConfig

type OAuthConfig struct {
	Google    ProviderConfig `json:"google"`
	Microsoft ProviderConfig `json:"microsoft"`
}

OAuthConfig holds the configuration for multiple OAuth providers.

type ProviderConfig

type ProviderConfig struct {
	ClientID     string `json:"client_id"`
	ClientSecret string `json:"client_secret"`
	RedirectURL  string `json:"redirect_url"`
	Scopes       string `json:"scopes"`
}

ProviderConfig represents the common OAuth settings required by each provider.

func (*ProviderConfig) GetScopes

func (oauth *ProviderConfig) GetScopes() []string

GetScopes splits the Scopes string into a slice of individual scope strings. The Scopes field is expected to be a comma-separated string, and this method returns each scope as an element in a slice of strings.

type ServerConfig

type ServerConfig struct {
	Port             uint          `json:"port"`
	Production       bool          `json:"production"`
	ReadTimeout      time.Duration `json:"read_timeout"`
	WriteTimeout     time.Duration `json:"write_timeout"`
	GracefulShutdown time.Duration `json:"graceful_shutdown"`
	Domain           string        `json:"domain"`
}

ServerConfig represents the configuration for the server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL