mentorenwahl/docker/backend/src/backend/config.cr

167 lines
3.8 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 "secrets-env"
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: "worker")]
# Configuration for `Worker`
getter worker : WorkerConfig
@[EnvConfig::Setting(key: "smtp")]
# Configuration for `Mailers`
getter smtp : SmtpConfig
@[EnvConfig::Setting(key: "db")]
# Configuration for `Db`
getter db : DbConfig
@[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
# 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 `Worker`
class WorkerConfig
include EnvConfig
# Redis URL
getter redis_url : String
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 `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
end
end
end