Skip to content

Commit c3fe843

Browse files
authored
Merge pull request #105 from drgrice1/home-rework
Clean up the conf file handling.
2 parents 1de2e5f + 27f997c commit c3fe843

30 files changed

+73
-75
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ node_modules
99

1010
# Log files
1111
logs/*
12+
!logs/.gitkeep
1213
npm-debug.log*
1314
yarn-debug.log*
1415
yarn-error.log*
@@ -26,8 +27,7 @@ pnpm-debug.log*
2627

2728
# Project files
2829
sample_db.sqlite
29-
conf/webwork3.yml
30-
conf/ww3-dev.yml
30+
conf/webwork3*.yml
3131
conf/apache2/webwork3-apache2.conf
3232
conf/apache2/webwork3.service
3333
conf/apache2/renderer.service

bin/import_ww2_db.pl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ BEGIN
8181
# Load the webwork3 configuration file.
8282

8383
my $config_file = "$main::ww3_dir/conf/webwork3.yml";
84-
die "The file $config_file does not exist. Did you make a copy of it from ww3-dev.dist.yml ?"
85-
unless (-e $config_file);
84+
$config_file = "$main::ww3_dir/conf/webwork3.yml.dist" unless -e $config_file;
8685

8786
my $config = LoadFile($config_file);
8887

lib/WeBWorK3.pm

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,36 @@ use Mojo::Base 'Mojolicious', -signatures;
44
use Mojo::File qw(curfile path);
55
use YAML::XS qw/LoadFile/;
66

7-
BEGIN {
8-
use Env qw(WW3_ROOT);
9-
$WW3_ROOT = curfile->dirname->dirname->to_string;
10-
}
11-
127
use DB::Schema;
138
use WeBWorK3::Hooks;
149

1510
my $perm_table;
1611

1712
# This method will run once at server start
1813
sub startup ($self) {
19-
# log to file if we're in production mode
14+
# Pick the config file to use and set up logging dependant on the mode being run in.
15+
my $config_file;
2016
if ($ENV{MOJO_MODE} && $ENV{MOJO_MODE} eq 'production') {
21-
my $path = path("$ENV{WW3_ROOT}/logs")->make_path->child('webwork3.log');
22-
$self->log->path($path);
23-
}
17+
$self->log->path($self->home->child('logs', 'webwork3.log'));
2418

25-
my $config;
26-
27-
if ($ENV{MOJO_MODE} && $ENV{MOJO_MODE} eq 'test') {
28-
my $path = path("$ENV{WW3_ROOT}/logs")->make_path->child('webwork3_test.log');
29-
$self->log->path($path);
19+
$config_file = $self->home->child('conf', 'webwork3.yml');
20+
$config_file = $self->home->child('conf', 'webwork3.dist.yml') unless -e $config_file;
21+
} elsif ($ENV{MOJO_MODE} && $ENV{MOJO_MODE} eq 'test') {
22+
$self->log->path($self->home->child('logs', 'webwork3_test.log'));
3023
$self->log->level('trace');
31-
my $test_config_file = "$ENV{WW3_ROOT}/conf/ww3-dev.yml";
32-
$test_config_file = "$ENV{WW3_ROOT}/conf/ww3-dev.dist.yml" unless (-e $test_config_file);
33-
$config = $self->plugin(NotYAMLConfig => { file => $test_config_file });
24+
25+
$config_file = $self->home->child('conf', 'webwork3-test.yml');
26+
$config_file = $self->home->child('conf', 'webwork3-test.dist.yml') unless -e $config_file;
27+
$self->plugin(NotYAMLConfig => { file => $config_file });
3428
} else {
35-
# Load configuration from config file
36-
$config = $self->plugin('NotYAMLConfig');
29+
$config_file = $self->home->child('conf', 'webwork3-dev.yml');
30+
$config_file = $self->home->child('conf', 'webwork3.yml') unless -e $config_file;
31+
$config_file = $self->home->child('conf', 'webwork3.dist.yml') unless -e $config_file;
3732
}
3833

34+
# Load configuration from config file
35+
my $config = $self->plugin(NotYAMLConfig => { file => $config_file });
36+
3937
# Configure the application
4038
$self->secrets($config->{secrets});
4139

@@ -65,7 +63,7 @@ sub startup ($self) {
6563
$self->sessions->secure($config->{cookie_secure});
6664

6765
# Load permissions and set up a helper for dealing with permissions.
68-
$perm_table = LoadFile("$ENV{WW3_ROOT}/conf/permissions.yaml");
66+
$perm_table = LoadFile($self->home->child('conf', 'permissions.yaml'));
6967
$self->helper(perm_table => sub ($c) { return $perm_table; });
7068

7169
# Handle all api route exceptions

lib/WeBWorK3/Controller/Logger.pm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ use warnings;
33
use strict;
44

55
use Mojo::Base 'Mojolicious::Controller', -signatures;
6-
use Mojo::File qw(path);
6+
use Mojo::Home;
77
use Mojo::Log;
88
use JSON qw(decode_json);
99

10-
my $path = path("$ENV{WW3_ROOT}/logs")->make_path->child('clientLog.log');
11-
my $clientLogFile = Mojo::Log->new(path => $path);
10+
my $clientLogFile = Mojo::Log->new(path => Mojo::Home->new->detect->child('logs', 'clientLog.log'));
1211

1312
sub clientLog ($c) {
1413
my $rawJSON = $c->req->body;

lib/WeBWorK3/Utils/Settings.pm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use feature 'signatures';
66
no warnings qw(experimental::signatures);
77

88
use YAML::XS qw/LoadFile/;
9+
use Mojo::Home;
910
use Carp;
1011

1112
require Exporter;
@@ -15,9 +16,11 @@ our @EXPORT_OK = qw/checkSettings getDefaultCourseSettings getDefaultCourseValue
1516
validateSingleCourseSetting validateSettingConfig
1617
isInteger isTimeString isTimeDuration isDecimal/;
1718

18-
use DB::Exception::UndefinedCourseField;
19-
use DB::Exception::InvalidCourseField;
20-
use DB::Exception::InvalidCourseFieldType;
19+
use Exception::Class qw(
20+
DB::Exception::UndefinedCourseField
21+
DB::Exception::InvalidCourseField
22+
DB::Exception::InvalidCourseFieldType
23+
);
2124

2225
use WeBWorK3;
2326

@@ -31,7 +34,7 @@ load the default settings from the conf/course_settings.yaml file
3134
=cut
3235

3336
sub getDefaultCourseSettings () {
34-
return LoadFile("$ENV{WW3_ROOT}/conf/course_defaults.yml");
37+
return LoadFile(Mojo::Home->new->detect->child('conf', 'course_defaults.yml'));
3538
}
3639

3740
my @course_setting_categories = qw/email optional general permissions problem problem_set/;

logs/.gitkeep

Whitespace-only changes.

t/db/001_courses.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use DB::Schema;
2828
use TestUtils qw/loadCSV removeIDs loadSchema/;
2929

3030
# Load the database
31-
my $config_file = "$main::ww3_dir/conf/ww3-dev.yml";
32-
$config_file = "$main::ww3_dir/conf/ww3-dev.dist.yml" unless (-e $config_file);
31+
my $config_file = "$main::ww3_dir/conf/webwork3-test.yml";
32+
$config_file = "$main::ww3_dir/conf/webwork3-test.dist.yml" unless (-e $config_file);
3333
my $config = LoadFile($config_file);
3434
my $schema = DB::Schema->connect($config->{database_dsn}, $config->{database_user}, $config->{database_password});
3535
my $strp = DateTime::Format::Strptime->new(pattern => '%F', on_error => 'croak');

t/db/002_course_settings.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use WeBWorK3::Utils::Settings qw/getDefaultCourseSettings getDefaultCourseValues
2727
use TestUtils qw/removeIDs loadSchema/;
2828

2929
# Load the database
30-
my $config_file = "$main::ww3_dir/conf/ww3-dev.yml";
31-
$config_file = "$main::ww3_dir/conf/ww3-dev.dist.yml" unless (-e $config_file);
30+
my $config_file = "$main::ww3_dir/conf/webwork3-test.yml";
31+
$config_file = "$main::ww3_dir/conf/webwork3-test.dist.yml" unless (-e $config_file);
3232
my $config = LoadFile($config_file);
3333
my $schema = DB::Schema->connect($config->{database_dsn}, $config->{database_user}, $config->{database_password});
3434

t/db/003_users.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use DB::Schema;
2626
use TestUtils qw/loadCSV removeIDs/;
2727

2828
# Load the database
29-
my $config_file = "$main::ww3_dir/conf/ww3-dev.yml";
30-
$config_file = "$main::ww3_dir/conf/ww3-dev.dist.yml" unless (-e $config_file);
29+
my $config_file = "$main::ww3_dir/conf/webwork3-test.yml";
30+
$config_file = "$main::ww3_dir/conf/webwork3-test.dist.yml" unless (-e $config_file);
3131
my $config = LoadFile($config_file);
3232
my $schema = DB::Schema->connect($config->{database_dsn}, $config->{database_user}, $config->{database_password});
3333
my $strp = DateTime::Format::Strptime->new(pattern => '%F', on_error => 'croak');

0 commit comments

Comments
 (0)