Added env_config and removed senf
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Dominic Grimm 2022-02-03 17:28:32 +01:00
parent 0922ebe2e4
commit 1b3bdbd389
15 changed files with 97 additions and 57 deletions

View file

@ -1,13 +1,23 @@
# General
URL=
# Db
POSTGRES_USER="mw"
POSTGRES_PASSWORD=
URL=
BACKEND_GRAPHQL_PLAYGROUND=0
# Backend
BACKEND_URL=URL
BACKEND_ADMIN_EMAIL=
BACKEND_ADMIN_PASSWORD=
# Backend - API
BACKEND_API_GRAPHQL_PLAYGROUND=false
BACKEND_JWT_SECRET=
# Backend - Worker
# Backend - SMTP
BACKEND_SMTP_HELO=
BACKEND_SMTP_ADDRESS=
BACKEND_SMTP_PORT=
BACKEND_SMTP_NAME=
BACKEND_SMTP_USERNAME=
BACKEND_SMTP_PASSWORD=
# Backend - Db

View file

@ -58,10 +58,9 @@ services:
- db
- redis
environment:
URL: ${URL}
BACKEND_GRAPHQL_PLAYGROUND: ${BACKEND_GRAPHQL_PLAYGROUND}
BACKEND_DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_USER}
BACKEND_JWT_SECRET: ${BACKEND_JWT_SECRET}
BACKEND_URL: ${URL}
BACKEND_API_GRAPHQL_PLAYGROUND: ${BACKEND_API_GRAPHQL_PLAYGROUND}
BACKEND_API_JWT_SECRET: ${BACKEND_JWT_SECRET}
BACKEND_WORKER_REDIS_URL: redis://redis:6379
BACKEND_SMTP_HELO: ${BACKEND_SMTP_HELO}
BACKEND_SMTP_ADDRESS: ${BACKEND_SMTP_ADDRESS}
@ -69,6 +68,7 @@ services:
BACKEND_SMTP_NAME: ${BACKEND_SMTP_NAME}
BACKEND_SMTP_USERNAME: ${BACKEND_SMTP_USERNAME}
BACKEND_SMTP_PASSWORD: ${BACKEND_SMTP_PASSWORD}
BACKEND_DB_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_USER}
frontend:
build:

View file

@ -32,6 +32,10 @@ shards:
git: https://github.com/arcage/crystal-email.git
version: 0.6.3
env_config:
git: https://github.com/jreinert/env_config.cr.git
version: 0.1.0+git.commit.a3ef5b955f27e2c65de2fe0ff41718e2eea7c06f
fancyline:
git: https://github.com/papierkorb/fancyline.git
version: 0.4.1
@ -104,10 +108,6 @@ shards:
git: https://github.com/spider-gazelle/secrets-env.git
version: 1.3.1
senf:
git: https://git.dergrimm.net/dergrimm/senf.git
version: 0.1.0
version_from_shard:
git: https://github.com/hugopl/version_from_shard.git
version: 1.2.5

View file

@ -35,8 +35,6 @@ dependencies:
github: Papierkorb/fancyline
micrate:
github: juanedi/micrate
senf:
git: https://git.dergrimm.net/dergrimm/senf.git
mosquito:
github: mosquito-cr/mosquito
secrets-env:
@ -53,3 +51,5 @@ dependencies:
github: tbrand/router.cr
html-minifier:
github: sam0x17/html-minifier
env_config:
github: jreinert/env_config.cr

View file

@ -1,5 +1,3 @@
require "secrets-env"
require "./backend/*"
module Backend

View file

@ -19,7 +19,7 @@ module Backend
end
private def create_jwt(data, expiration : Int) : String
JWT.encode({"data" => data.to_h, "exp" => expiration}, SAFE_ENV["BACKEND_JWT_SECRET"], JWT::Algorithm::HS256)
JWT.encode({"data" => data.to_h, "exp" => expiration}, Backend.config.api.jwt_secret, JWT::Algorithm::HS256)
end
def create_user_jwt(user_id : Int, expiration : Int = (Time.utc + Time::Span.new(days: 1)).to_unix) : String
@ -27,7 +27,7 @@ module Backend
end
def decode_jwt(jwt : String) : JSON::Any
JWT.decode(jwt, SAFE_ENV["BACKEND_JWT_SECRET"], JWT::Algorithm::HS256)[0]
JWT.decode(jwt, Backend.config.api.jwt_secret, JWT::Algorithm::HS256)[0]
end
def decode_jwt?(jwt : String) : JSON::Any?

View file

@ -7,12 +7,11 @@ module Backend
class WebServer
include Router
GRAPHQL_PLAYGROUND = {{ run("./macros/minify_html.cr", read_file("#{__DIR__}/playground.html")).stringify }}
GRAPHQL_PLAYGROUND_ENABLE = SAFE_ENV["BACKEND_GRAPHQL_PLAYGROUND"].to_i == 1 || {{ flag?(:development) }}
GRAPHQL_PLAYGROUND = {{ run("./macros/minify_html.cr", read_file("#{__DIR__}/playground.html")).stringify }}
def draw_routes : Nil
# enable graphql playground when in development mode or explicitly enabled
if GRAPHQL_PLAYGROUND_ENABLE
if Backend.config.api.graphql_playground || {{ flag?(:development) }}
Log.info { "GraphQL playground enabled" }
get "/" do |context|

View file

@ -0,0 +1,60 @@
require "secrets-env"
require "env_config"
module Backend
extend self
@@config = Config.new(ENV, prefix: "BACKEND")
def config : Config
@@config
end
class Config
include EnvConfig
getter url : String
@[EnvConfig::Setting(key: "api")]
getter api : APIConfig
@[EnvConfig::Setting(key: "worker")]
getter worker : WorkerConfig
@[EnvConfig::Setting(key: "smtp")]
getter smtp : SMTPConfig
@[EnvConfig::Setting(key: "db")]
getter db : DbConfig
class APIConfig
include EnvConfig
getter graphql_playground : Bool
getter jwt_secret : String
end
class WorkerConfig
include EnvConfig
getter redis_url : String
end
class SMTPConfig
include EnvConfig
getter helo : String
getter address : String
getter port : Int32
getter name : String
getter username : String
getter password : String
end
class DbConfig
include EnvConfig
getter url : String
end
end
end

View file

@ -5,6 +5,6 @@ require "./db/*"
module Backend
module Db
Granite::Connections << Granite::Adapter::Pg.new(name: "pg", url: SAFE_ENV["BACKEND_DATABASE_URL"])
Granite::Connections << Granite::Adapter::Pg.new(name: "pg", url: Backend.config.db.url)
end
end

View file

@ -6,17 +6,14 @@ require "./mailers/*"
module Backend
module Mailers
NAME = SAFE_ENV["BACKEND_SMTP_NAME"]
EMAIL = SAFE_ENV["BACKEND_SMTP_USERNAME"]
Quartz.config do |config|
config.smtp_enabled = true
config.smtp_address = SAFE_ENV["BACKEND_SMTP_ADDRESS"]
config.smtp_port = SAFE_ENV["BACKEND_SMTP_PORT"]
config.helo_domain = SAFE_ENV["BACKEND_SMTP_HELO"]
config.smtp_address = Backend.config.smtp.address
config.smtp_port = Backend.config.smtp.port
config.helo_domain = Backend.config.smtp.helo
config.use_tls = EMail::Client::TLSMode::STARTTLS
config.username = EMAIL
config.password = SAFE_ENV["BACKEND_SMTP_PASSWORD"]
config.username = Backend.config.smtp.username
config.password = Backend.config.smtp.password
config.use_authentication = true
end
end

View file

@ -2,7 +2,7 @@ module Backend
module Mailers
class TeacherRegistrationMailer < Quartz::Composer
def sender : Quartz::Message::Address
address email: EMAIL, name: NAME
address email: Backend.config.smtp.username, name: Backend.config.smtp.name
end
def initialize(user : Db::User)

View file

@ -2,4 +2,4 @@ Hey, <%= user.name %>!
Du wurdest erfolgreich als Lehrer registriert.
Initialisiere deinen Account, indem du auf den folgenden Link klickst und deine Daten eingibst:
<%= Path[SAFE_ENV["URL"], "register/teacher?jwt=TEST"] %>
<%= Backend.config.url %>

View file

@ -1,19 +0,0 @@
require "senf"
module Backend
SAFE_ENV = Senf::SafeEnv.new(%w(
URL
BACKEND_GRAPHQL_PLAYGROUND
BACKEND_DATABASE_URL
BACKEND_ADMIN_EMAIL
BACKEND_ADMIN_PASSWORD
BACKEND_JWT_SECRET
BACKEND_WORKER_REDIS_URL
BACKEND_SMTP_HELO
BACKEND_SMTP_ADDRESS
BACKEND_SMTP_PORT
BACKEND_SMTP_NAME
BACKEND_SMTP_USERNAME
BACKEND_SMTP_PASSWORD
))
end

View file

@ -23,7 +23,7 @@ require "./worker/*"
module Backend
module Worker
Mosquito.configure do |settings|
settings.redis_url = SAFE_ENV["BACKEND_WORKER_REDIS_URL"]
settings.redis_url = Backend.config.worker.redis_url
end
end
end

View file

@ -1,11 +1,6 @@
require "secrets-env"
require "senf"
require "micrate"
require "pg"
SAFE_ENV = Senf::SafeEnv.new([
"BACKEND_DATABASE_URL",
])
Micrate::DB.connection_url = SAFE_ENV["BACKEND_DATABASE_URL"]?
Micrate::DB.connection_url = ENV["BACKEND_DB_URL"]?
Micrate::Cli.run