From eeedc57cd3ec73f7bd8687c261eeec92829e71d4 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Tue, 12 Aug 2025 23:47:38 -0400 Subject: [PATCH] Overhauled configuration so that it's a bit more useful in more spots; configuration now properly loads an environment config as well as defaults; updated some woodpecker config --- .gitignore | 3 +++ .woodpecker.yml | 14 +++++++++++++- Rakefile | 13 ++++++++----- config/defaults.yaml | 6 +++--- config/development.yaml | 3 +++ config/puma.rb | 6 ++++-- config/testing.yaml | 3 +++ src/config.rb | 4 +++- src/server.rb | 3 ++- 9 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 config/development.yaml create mode 100644 config/testing.yaml diff --git a/.gitignore b/.gitignore index 7a6beec..aafef6a 100644 --- a/.gitignore +++ b/.gitignore @@ -65,3 +65,6 @@ node_modules/ # Compiled assets public/css/ public/js/ + +# Ignore production configuration files to protect against credential leaks +config/production.yaml diff --git a/.woodpecker.yml b/.woodpecker.yml index ba40cdd..e8e2ce7 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -1,16 +1,28 @@ steps: - test_ruby34: + setup: image: ruby:3.4 + env: + RACK_ENV: testing commands: - gem install rake - bundle config set --local path "vendor/bundle" - bundle install - rake db:migrate + + test_ruby34: + image: ruby:3.4 + env: + RACK_ENV: testing + commands: + - gem install rake + - bundle config set --local path "vendor/bundle" - rake test:unit group: tests style: image: ruby:3.4 + env: + RACK_ENV: testing commands: - gem install rake - bundle config set --local path "vendor/bundle" diff --git a/Rakefile b/Rakefile index 40a3c9f..c086e9c 100644 --- a/Rakefile +++ b/Rakefile @@ -3,18 +3,22 @@ require 'bundler/setup' namespace :db do desc 'Run migrations' task :migrate, [:version] do |t, args| - require "sequel/core" + require 'sequel/core' + # load configuration + require_relative 'src/config' + conf = Config.new() + Sequel.extension :migration version = args[:version].to_i if args[:version] - Sequel.connect('sqlite://data/gamedata.db') do |db| - Sequel::Migrator.run(db, "db/migrations", target: version) + Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database')) do |db| + Sequel::Migrator.run(db, 'db/migrations', target: version) end end end namespace :server do task :start do - ENV['APP_ENV'] = 'production' + ENV['RACK_ENV'] = 'production' system("puma") end @@ -32,4 +36,3 @@ namespace :test do system("rubocop src/ spec/") end end - diff --git a/config/defaults.yaml b/config/defaults.yaml index baff6cc..284de80 100644 --- a/config/defaults.yaml +++ b/config/defaults.yaml @@ -1,6 +1,6 @@ -database: - adapter: 'sqlite' - database: 'data/gamedata.db' +server: + host: '0.0.0.0' + port: '9292' testing: minimum_results_required: 3 diff --git a/config/development.yaml b/config/development.yaml new file mode 100644 index 0000000..8f173a4 --- /dev/null +++ b/config/development.yaml @@ -0,0 +1,3 @@ +database: + adapter: 'sqlite' + database: 'data/gamedata.db' diff --git a/config/puma.rb b/config/puma.rb index aebe0b6..f071a4f 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -3,7 +3,9 @@ directory app_dir environment ENV.fetch('RACK_ENV', 'development') -bind 'tcp://0.0.0.0:9292' +require_relative '../src/config' +conf = Config.new() + +bind "tcp://#{conf.get('server.host')}:#{conf.get('server.port')}" workers 2 threads 1, 5 - diff --git a/config/testing.yaml b/config/testing.yaml new file mode 100644 index 0000000..e7f9f26 --- /dev/null +++ b/config/testing.yaml @@ -0,0 +1,3 @@ +database: + adapter: 'sqlite' + database: 'data/gamedata_testing.db' diff --git a/src/config.rb b/src/config.rb index 84d7491..3027d98 100644 --- a/src/config.rb +++ b/src/config.rb @@ -6,11 +6,13 @@ require 'yaml' class Config DEFAULT_CONFIG = 'config/defaults.yaml' + ENVIRONMENT_CONFIG = ENV.fetch('RACK_ENV', 'development') - def initialize(config_path) + def initialize(config_path = "config/#{ENVIRONMENT_CONFIG}.yaml") @data = YAML.load_file(DEFAULT_CONFIG) # merge in user-defined configuration if it exists + puts "Merging #{config_path} with #{DEFAULT_CONFIG}." @data.merge!(YAML.load_file(config_path)) if File.exist?(config_path) end diff --git a/src/server.rb b/src/server.rb index 10d196e..6ccd1ec 100644 --- a/src/server.rb +++ b/src/server.rb @@ -6,7 +6,8 @@ require 'sqlite3' require_relative 'config' -$conf = Config.new(File.join(__dir__, 'config/defaults.yaml')) +# Load configuration from environment config file +$conf = Config.new() # Load the Sequel timestamps plugin Sequel::Model.plugin(:timestamps)