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

167 lines
3.8 KiB
Crystal
Raw Normal View History

2022-02-10 07:43:47 +00:00
# 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/>.
2022-02-03 16:28:32 +00:00
require "secrets-env"
require "env_config"
module Backend
extend self
@@config = Config.new(ENV, prefix: "BACKEND")
2022-02-07 17:52:17 +00:00
# Global configuration
2022-02-03 16:28:32 +00:00
def config : Config
@@config
end
2022-02-07 17:52:17 +00:00
# Environment based configuration class
2022-02-03 16:28:32 +00:00
class Config
include EnvConfig
2022-02-07 17:52:17 +00:00
# Types of environments program can compiled for / with
enum BuildEnv
Production
2022-02-07 17:52:17 +00:00
Development
end
2022-02-07 17:52:17 +00:00
# Type of environment program is running in
def build_env : BuildEnv
{{ flag?(:development) }} ? BuildEnv::Development : BuildEnv::Production
end
2022-02-07 17:52:17 +00:00
# Production mode
#
# `true` if the build environment is `BuildEnv::Development`
def production? : Bool
build_env.production?
end
2022-02-07 17:52:17 +00:00
# Development mode
#
# `true` if the build environment is `BuildEnv::Production`
def development? : Bool
build_env.development?
end
2022-02-07 17:52:17 +00:00
# Base URL of application
2022-02-03 16:28:32 +00:00
getter url : String
@[EnvConfig::Setting(key: "api")]
2022-02-07 17:52:17 +00:00
# Configuration for `Api`
getter api : ApiConfig
2022-02-03 16:28:32 +00:00
@[EnvConfig::Setting(key: "worker")]
2022-02-07 17:52:17 +00:00
# Configuration for `Worker`
2022-02-03 16:28:32 +00:00
getter worker : WorkerConfig
@[EnvConfig::Setting(key: "smtp")]
2022-02-07 17:52:17 +00:00
# Configuration for `Mailers`
getter smtp : SmtpConfig
2022-02-03 16:28:32 +00:00
@[EnvConfig::Setting(key: "db")]
2022-02-07 17:52:17 +00:00
# Configuration for `Db`
2022-02-03 16:28:32 +00:00
getter db : DbConfig
@[EnvConfig::Setting(key: "ldap")]
2022-02-07 17:52:17 +00:00
# Configuration for `Ldap`
getter ldap : LdapConfig
2022-02-07 17:52:17 +00:00
# Configuration for `Api`
class ApiConfig
2022-02-03 16:28:32 +00:00
include EnvConfig
2022-02-07 17:52:17 +00:00
# GraphQL playground enable
2022-02-03 16:28:32 +00:00
getter graphql_playground : Bool
2022-02-07 17:52:17 +00:00
# JWT signing key
2022-02-03 16:28:32 +00:00
getter jwt_secret : String
2022-02-07 17:52:17 +00:00
# Helper method for enabling GraphQL playground
#
2022-02-07 19:26:29 +00:00
# Returns `true` if `Config#development?` or `#graphql_playground` are
2022-02-07 17:52:17 +00:00
def graphql_playground_fully_enabled? : Bool
Backend.config.development? || graphql_playground
end
2022-02-03 16:28:32 +00:00
end
2022-02-07 17:52:17 +00:00
# Configuration for `Worker`
2022-02-03 16:28:32 +00:00
class WorkerConfig
include EnvConfig
2022-02-07 17:52:17 +00:00
# Redis URL
2022-02-03 16:28:32 +00:00
getter redis_url : String
end
2022-02-07 17:52:17 +00:00
# Configuration for `Mailers`
class SmtpConfig
2022-02-03 16:28:32 +00:00
include EnvConfig
2022-02-07 17:52:17 +00:00
# SMTP host HELO
#
# NOTE: HELOs are [FQDNs](https://en.wikipedia.org/wiki/Fully_qualified_domain_name), so this should be a domain name
2022-02-03 16:28:32 +00:00
getter helo : String
2022-02-07 17:52:17 +00:00
# SMTP hostname
getter host : String
2022-02-07 17:52:17 +00:00
# SMTP port
2022-02-03 16:28:32 +00:00
getter port : Int32
2022-02-07 17:52:17 +00:00
# Name to send from
2022-02-03 16:28:32 +00:00
getter name : String
2022-02-07 17:52:17 +00:00
# SMTP username
2022-02-03 16:28:32 +00:00
getter username : String
2022-02-07 17:52:17 +00:00
# SMTP password
2022-02-03 16:28:32 +00:00
getter password : String
end
2022-02-07 17:52:17 +00:00
# Configuration for `Db`
2022-02-03 16:28:32 +00:00
class DbConfig
include EnvConfig
2022-02-07 17:52:17 +00:00
# Database URL
2022-02-03 16:28:32 +00:00
getter url : String
end
2022-02-07 17:52:17 +00:00
# Configuration for `Ldap`
class LdapConfig
include EnvConfig
2022-02-07 17:52:17 +00:00
# LDAP hostname
getter host : String
2022-02-07 17:52:17 +00:00
# LDAP port
getter port : Int32
2022-02-07 17:52:17 +00:00
# LDAP base DN
getter base_dn : String
2022-02-07 17:52:17 +00:00
# LDAP user base DN
getter base_user_dn : String
# LDAP bind DN
#
# NOTE: This is the DN to search with
2022-02-06 15:42:08 +00:00
getter bind_dn : String
2022-02-07 17:52:17 +00:00
# LDAP bind password
2022-02-06 15:42:08 +00:00
getter bind_password : String
end
2022-02-03 16:28:32 +00:00
end
end