mentorenwahl/backend/src/backend/config.cr

131 lines
3.1 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
2022-03-07 13:06:02 +00:00
#
2022-02-10 07:43:47 +00:00
# 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.
2022-03-07 13:06:02 +00:00
#
2022-02-10 07:43:47 +00:00
# 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.
2022-03-07 13:06:02 +00:00
#
2022-02-10 07:43:47 +00:00
# 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 "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
2022-10-31 08:47:26 +00:00
Release
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
2022-10-31 08:47:26 +00:00
{{ flag?(:release) }} ? BuildEnv::Release : BuildEnv::Development
end
2022-10-31 08:47:26 +00:00
# Release mode
2022-02-07 17:52:17 +00:00
#
2022-10-31 08:47:26 +00:00
# `true` if the build environment is `BuildEnv::Release`
def release? : Bool
2022-02-07 17:52:17 +00:00
build_env.production?
end
2022-02-07 17:52:17 +00:00
# Development mode
#
2022-10-31 08:47:26 +00:00
# `true` if the build environment is `BuildEnv::Development`
2022-02-07 17:52:17 +00:00
def development? : Bool
build_env.development?
end
# Minimum teacher selection count
2022-03-13 10:54:42 +00:00
getter minimum_teacher_selection_count : Int32
2023-03-05 12:52:30 +00:00
# Assignment max run time for algorithm
getter assignment_run_time : UInt32
2022-07-28 12:05:10 +00:00
2022-02-03 16:28:32 +00:00
@[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: "db")]
2022-02-07 17:52:17 +00:00
# Configuration for `Db`
2022-02-03 16:28:32 +00:00
getter db : DbConfig
2022-03-07 08:34:18 +00:00
@[EnvConfig::Setting(key: "redis")]
getter redis : RedisConfig
2023-01-29 10:49:13 +00:00
@[EnvConfig::Setting(key: "auth")]
# Configuration for authorization provider
getter auth : AuthConfig
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
# JWT signing key
2022-02-03 16:28:32 +00:00
getter jwt_secret : String
# JWT expiration time in minutes
getter jwt_expiration : Int32
2022-10-31 08:47:26 +00:00
# Returns true of `playground` flag was set on compile time
def graphql_playground? : Bool
flag?(:playground)
end
2022-02-03 16:28:32 +00:00
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
# Allow old database migrations to be used
getter allow_old_schema : Bool
2022-02-03 16:28:32 +00:00
end
2022-03-07 08:34:18 +00:00
# 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
2023-01-29 10:49:13 +00:00
# Configuration for authoriuation API
class AuthConfig
include EnvConfig
# Auth API URL
getter url : String
end
2022-02-03 16:28:32 +00:00
end
end