mentorenwahl/docker/backend/src/backend/config.cr
Dominic Grimm 3a19d1d8db
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
Fixed file license headers
2022-03-07 14:06:02 +01:00

179 lines
4.1 KiB
Crystal

# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
# Copyright (C) 2022 Dominic Grimm
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
require "env_config"
module Backend
extend self
@@config = Config.new(ENV, prefix: "BACKEND")
# Global configuration
def config : Config
@@config
end
# Environment based configuration class
class Config
include EnvConfig
# Types of environments program can compiled for / with
enum BuildEnv
Production
Development
end
# Type of environment program is running in
def build_env : BuildEnv
{{ flag?(:development) }} ? BuildEnv::Development : BuildEnv::Production
end
# Production mode
#
# `true` if the build environment is `BuildEnv::Development`
def production? : Bool
build_env.production?
end
# Development mode
#
# `true` if the build environment is `BuildEnv::Production`
def development? : Bool
build_env.development?
end
# Base URL of application
getter url : String
@[EnvConfig::Setting(key: "api")]
# Configuration for `Api`
getter api : ApiConfig
@[EnvConfig::Setting(key: "smtp")]
# Configuration for `Mailers`
getter smtp : SmtpConfig
@[EnvConfig::Setting(key: "db")]
# Configuration for `Db`
getter db : DbConfig
@[EnvConfig::Setting(key: "redis")]
getter redis : RedisConfig
@[EnvConfig::Setting(key: "ldap")]
# Configuration for `Ldap`
getter ldap : LdapConfig
# Configuration for `Api`
class ApiConfig
include EnvConfig
# GraphQL playground enable
getter graphql_playground : Bool
# JWT signing key
getter jwt_secret : String
# JWT expiration time in minutes
getter jwt_expiration : Int32
# Helper method for enabling GraphQL playground
#
# Returns `true` if `Config#development?` or `#graphql_playground` are
def graphql_playground_fully_enabled? : Bool
Backend.config.development? || graphql_playground
end
end
# Configuration for `Mailers`
class SmtpConfig
include EnvConfig
# SMTP host HELO
#
# NOTE: HELOs are [FQDNs](https://en.wikipedia.org/wiki/Fully_qualified_domain_name), so this should be a domain name
getter helo : String
# SMTP hostname
getter host : String
# SMTP port
getter port : Int32
# Name to send from
getter name : String
# SMTP username
getter username : String
# SMTP password
getter password : String
end
# Configuration for `Db`
class DbConfig
include EnvConfig
# Database URL
getter url : String
end
# Configuration for `REDIS`
class RedisConfig
include EnvConfig
# Redis host
getter host : String
# Redis port
getter port : Int32
# Redis URL
def url : String
"redis://#{host}:#{port}"
end
end
# Configuration for `Ldap`
class LdapConfig
include EnvConfig
# LDAP hostname
getter host : String
# LDAP port
getter port : Int32
# LDAP base DN
getter base_dn : String
# LDAP user base DN
getter base_user_dn : String
# LDAP bind DN
#
# NOTE: This is the DN to search with
getter bind_dn : String
# LDAP bind password
getter bind_password : String
# Periodical cache refresh interval
getter cache_refresh_interval : Int32
end
end
end