1
1
package Renderer ;
2
2
use Mojo::Base ' Mojolicious' ;
3
3
4
- BEGIN {
5
- use Mojo::File;
6
- $main::libname = Mojo::File::curfile-> dirname;
7
-
8
- # RENDER_ROOT is required for initializing conf files.
9
- $ENV {RENDER_ROOT } = $main::libname -> dirname
10
- unless (defined ($ENV {RENDER_ROOT }));
11
-
12
- # PG_ROOT is required for PG/lib/PGEnvironment.pm
13
- $ENV {PG_ROOT } = $main::libname . ' /PG' ;
4
+ use Mojo::File;
5
+ use Env qw( RENDER_ROOT PG_ROOT baseURL) ;
6
+ use Date::Format;
14
7
15
- # Used for reconstructing library paths from sym-links.
16
- $ENV {OPL_DIRECTORY } = " $ENV {RENDER_ROOT}/webwork-open-problem-library" ;
17
-
18
- $ENV {MOJO_CONFIG } =
19
- (-r " $ENV {RENDER_ROOT}/renderer.conf" )
20
- ? " $ENV {RENDER_ROOT}/renderer.conf"
21
- : " $ENV {RENDER_ROOT}/renderer.conf.dist" ;
22
- $ENV {MOJO_LOG_LEVEL } = ' debug' ;
8
+ BEGIN {
9
+ # RENDER_ROOT and PG_ROOT are required for the WeBWorK::PG::Environment.
10
+ $RENDER_ROOT = Mojo::File::curfile-> dirname-> dirname;
11
+ $PG_ROOT = Mojo::File::curfile-> dirname-> child(' PG' );
23
12
}
24
13
25
- use lib " $main::libname " ;
26
- print " using root directory: $ENV {RENDER_ROOT}\n " ;
27
-
28
14
use Renderer::Model::Problem;
29
15
use Renderer::Controller::IO;
30
16
use WeBWorK::FormatRenderedProblem;
31
17
32
18
sub startup {
33
19
my $self = shift ;
34
20
35
- # Merge environment variables with config file
36
21
$self -> plugin(' Config' );
37
- $self -> plugin(' TagHelpers' );
38
22
$self -> secrets($self -> config(' secrets' ));
39
- for (qw( problemJWTsecret webworkJWTsecret baseURL formURL SITE_HOST STRICT_JWT) ) {
40
- $ENV {$_ } //= $self -> config($_ );
41
- }
42
23
43
- sanitizeHostURLs();
24
+ $self -> sanitizeHostURLs;
25
+
26
+ # This is also required for the WeBWorK::PG::Environment, but is not needed at compile time.
27
+ $baseURL = $self -> config-> {baseURL };
44
28
45
- print " Renderer is based at $main::basehref \n " ;
46
- print " Problem attempts will be sent to $main::formURL \n " ;
29
+ say ' Renderer is based at ' . $self -> defaults-> {baseHREF };
30
+ say ' Problem attempts will be sent to ' . $self -> defaults-> {formURL };
31
+
32
+ $self -> plugin(' Renderer::Plugin::Assets' );
47
33
48
34
# Handle optional CORS settings
49
35
if (my $CORS_ORIGIN = $self -> config(' CORS_ORIGIN' )) {
@@ -63,42 +49,62 @@ sub startup {
63
49
64
50
# Logging
65
51
if ($ENV {MOJO_MODE } && $ENV {MOJO_MODE } eq ' production' ) {
66
- my $logPath = " $ENV {RENDER_ROOT}/ logs/ error.log" ;
67
- print " [LOGS] Running in production mode, logging to $logPath \n " ;
52
+ my $logPath = $self -> home -> child( ' logs' , ' error.log' ) ;
53
+ say " [LOGS] Running in production mode, logging to $logPath " ;
68
54
$self -> log (Mojo::Log-> new(
69
55
path => $logPath ,
70
56
level => ($ENV {MOJO_LOG_LEVEL } || ' warn' )
71
57
));
72
58
}
73
59
74
60
if ($self -> config(' INTERACTION_LOG' )) {
75
- my $interactionLogPath = " $ENV {RENDER_ROOT}/ logs/ interactions.log" ;
76
- print " [LOGS] Saving interactions to $interactionLogPath \n " ;
61
+ my $interactionLogPath = $self -> home -> child( ' logs' , ' interactions.log' ) ;
62
+ say " [LOGS] Saving interactions to $interactionLogPath " ;
77
63
my $resultsLog = Mojo::Log-> new(path => $interactionLogPath , level => ' info' );
78
64
$resultsLog -> format(sub {
79
65
my ($time , $level , @lines ) = @_ ;
80
66
my $start = shift (@lines );
81
- my $msg = join " , " , @lines ;
82
- return sprintf " %s , %s , %s \n " , $start , $time - $start , $msg ;
67
+ return sprintf " %s , %s , %s \n " , $start , $time - $start , join (' , ' , @lines );
83
68
});
84
69
$self -> helper(logAttempt => sub { shift ; $resultsLog -> info(@_ ); });
85
70
}
86
71
72
+ my $resourceUsageLog = Mojo::Log-> new(path => $self -> home-> child(' logs' , ' resource_usage.log' ));
73
+ $resourceUsageLog -> format(sub {
74
+ my ($time , $level , @lines ) = @_ ;
75
+ return ' [' . time2str(' %a %b %d %H:%M:%S %Y' , time ) . ' ] ' . join (' , ' , @lines ) . " \n " ;
76
+ });
77
+ $self -> helper(resourceUsageLog => sub { shift ; return $resourceUsageLog -> info(@_ ); });
78
+
87
79
# Models
88
- $self -> helper(newProblem => sub { shift ; Renderer::Model::Problem-> new(@_ ) });
80
+ $self -> helper(newProblem => sub { my ( $c , $args ) = @_ ; Renderer::Model::Problem-> new($c , $args ) });
89
81
90
82
# Helpers
91
- $self -> helper(format => sub { WeBWorK::FormatRenderedProblem::formatRenderedProblem(@_ ) });
92
- $self -> helper(validateRequest => sub { Renderer::Controller::IO::validate(@_ ) });
93
- $self -> helper(parseRequest => sub { Renderer::Controller::Render::parseRequest(@_ ) });
94
- $self -> helper(croak => sub { Renderer::Controller::Render::croak(@_ ) });
95
- $self -> helper(logID => sub { shift -> req-> request_id });
96
- $self -> helper(exception => sub { Renderer(@_ ) });
83
+ $self -> helper(
84
+ format => sub {
85
+ my ($c , $rh_result ) = @_ ;
86
+ WeBWorK::FormatRenderedProblem::formatRenderedProblem($c , $rh_result );
87
+ }
88
+ );
89
+ $self -> helper(validateRequest => sub { my ($c , $options ) = @_ ; Renderer::Controller::IO::validate($c , $options ) });
90
+ $self -> helper(parseRequest => sub { my $c = shift ; Renderer::Controller::Render::parseRequest($c ) });
91
+ $self -> helper(
92
+ croak => sub {
93
+ my ($c , $exception , $depth ) = @_ ;
94
+ Renderer::Controller::Render::croak($c , $exception , $depth );
95
+ }
96
+ );
97
+ $self -> helper(logID => sub { my $c = shift ; $c -> req-> request_id });
98
+ $self -> helper(
99
+ exception => sub {
100
+ my ($c , $message , $status , %data ) = @_ ;
101
+ Renderer::Controller::Render::exception($c , $message , $status , %data );
102
+ }
103
+ );
97
104
98
105
# Routes
99
- # baseURL sets the root at which the renderer is listening,
100
- # and is used in Environment for pg_root_url
101
- my $r = $self -> routes-> under($ENV {baseURL });
106
+ # baseURL is the root at which the renderer is listening.
107
+ my $r = $self -> routes-> under($self -> config-> {baseURL });
102
108
103
109
$r -> any(' /render-api' )-> to(' render#problem' );
104
110
$r -> any(' /render-ptx' )-> to(' render#render_ptx' );
@@ -108,10 +114,12 @@ sub startup {
108
114
supplementalRoutes($r ) if ($self -> mode eq ' development' || $self -> config(' FULL_APP_INSECURE' ));
109
115
110
116
# Static file routes
111
- $r -> any(' /pg_files/CAPA_Graphics/*static' )-> to(' StaticFiles#CAPA_graphics_file' );
112
- $r -> any(' /pg_files/tmp/*static' )-> to(' StaticFiles#temp_file' );
113
- $r -> any(' /pg_files/*static' )-> to(' StaticFiles#pg_file' );
114
- $r -> any(' /*static' )-> to(' StaticFiles#public_file' );
117
+ $r -> any(' /pg_files/CAPA_Graphics/*static' )-> to(' StaticFiles#CAPA_graphics_file' )-> name(' capaFile' );
118
+ $r -> any(' /pg_files/tmp/*static' )-> to(' StaticFiles#temp_file' )-> name(' pgTempFile' );
119
+ $r -> any(' /pg_files/*static' )-> to(' StaticFiles#pg_file' )-> name(' pgFile' );
120
+ $r -> any(' /*static' )-> to(' StaticFiles#public_file' )-> name(' publicFile' );
121
+
122
+ return ;
115
123
}
116
124
117
125
sub supplementalRoutes {
@@ -156,45 +164,40 @@ sub timeout {
156
164
}
157
165
158
166
sub sanitizeHostURLs {
159
- $ENV {SITE_HOST } =~ s ! /$!! ;
160
-
161
- # set an absolute base href for asset urls under iframe embedding
162
- if ($ENV {baseURL } =~ m ! ^https?://! ) {
167
+ my $self = shift ;
163
168
164
- # this should only be used by MITM sites when proxying renderer assets
165
- my $baseURL = $ENV {baseURL } =~ m ! /$ ! ? $ENV {baseURL } : " $ENV {baseURL}/" ;
166
- $main::basehref = Mojo::URL-> new($baseURL );
169
+ $self -> config-> {SITE_HOST } =~ s ! /$!! ;
167
170
168
- # do NOT use the proxy address in our router!
169
- $ENV {baseURL } = ' ' ;
170
- } elsif ($ENV {baseURL } =~ m !\S ! ) {
171
+ # Set an absolute base href for asset urls under iframe embedding.
172
+ if ($self -> config-> {baseURL } =~ m ! ^https?://! ) {
173
+ # This should only be used by MITM sites when proxying renderer assets.
174
+ my $baseURL = $self -> config-> {baseURL } =~ m ! /$ ! ? $self -> config-> {baseURL } : $self -> config-> {baseURL } . ' /' ;
175
+ $self -> defaults-> {baseHREF } = Mojo::URL-> new($baseURL );
171
176
172
- # ENV{baseURL} is used to build routes, so configure as "/extension"
173
- $ENV {baseURL } = " / $ENV {baseURL} " ;
174
- warn " *** [CONFIG] baseURL should not end in a slash \n "
175
- if $ENV { baseURL } =~ s ! /$ !! ;
176
- warn " *** [CONFIG] baseURL should begin with a slash \n "
177
- unless $ENV {baseURL } =~ s !^// ! / ! ;
177
+ # Do NOT use the proxy address for the router!
178
+ $self -> config -> {baseURL } = ' ' ;
179
+ } elsif ( $self -> config -> { baseURL } =~ m ! \S ! ) {
180
+ # Ensure baseURL starts with a slash but doesn't end with a slash.
181
+ $self -> config -> { baseURL } = ' / ' . $self -> config -> { baseURL } unless $self -> config -> { baseURL } =~ m ! ^/ ! ;
182
+ $self -> config -> {baseURL } =~ s !/$ ! ! ;
178
183
179
- # base href must end in a slash when not hosting at the root
180
- $main::basehref =
181
- Mojo::URL-> new($ENV {SITE_HOST })-> path(" $ENV {baseURL}/" );
184
+ # base href must end in a slash when not hosting at the root.
185
+ $self -> defaults-> {baseHREF } = Mojo::URL-> new($self -> config-> {SITE_HOST })-> path($self -> config-> {baseURL } . ' /' );
182
186
} else {
183
187
# no proxy and service is hosted at the root of SITE_HOST
184
- $main::basehref = Mojo::URL-> new($ENV {SITE_HOST });
188
+ $self -> defaults -> { baseHREF } = Mojo::URL-> new($self -> config -> {SITE_HOST });
185
189
}
186
190
187
- if ($ENV {formURL } =~ m !\S ! ) {
188
-
191
+ if ($self -> config-> {formURL } =~ m !\S ! ) {
189
192
# this should only be used by MITM
190
- $main:: formURL = Mojo::URL-> new($ENV {formURL });
193
+ $self -> defaults -> { formURL } = Mojo::URL-> new($self -> config -> {formURL });
191
194
die ' *** [CONFIG] if provided, formURL must be absolute'
192
- unless $main:: formURL -> is_abs;
195
+ unless $self -> defaults -> { formURL } -> is_abs;
193
196
} else {
194
197
# if using MITM proxy base href + renderer api not at SITE_HOST root
195
198
# provide form url as absolute SITE_HOST/extension/render-api
196
- $main:: formURL =
197
- Mojo::URL-> new($ENV {SITE_HOST })-> path(" $ENV {baseURL}/render-api" );
199
+ $self -> defaults -> { formURL } =
200
+ Mojo::URL-> new($self -> config -> {SITE_HOST })-> path($self -> config -> {baseURL } . ' /render-api' );
198
201
}
199
202
}
200
203
0 commit comments