11package Renderer ;
22use Mojo::Base ' Mojolicious' ;
33
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;
147
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' );
2312}
2413
25- use lib " $main::libname " ;
26- print " using root directory: $ENV {RENDER_ROOT}\n " ;
27-
2814use Renderer::Model::Problem;
2915use Renderer::Controller::IO;
3016use WeBWorK::FormatRenderedProblem;
3117
3218sub startup {
3319 my $self = shift ;
3420
35- # Merge environment variables with config file
3621 $self -> plugin(' Config' );
37- $self -> plugin(' TagHelpers' );
3822 $self -> secrets($self -> config(' secrets' ));
39- for (qw( problemJWTsecret webworkJWTsecret baseURL formURL SITE_HOST STRICT_JWT) ) {
40- $ENV {$_ } //= $self -> config($_ );
41- }
4223
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 };
4428
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' );
4733
4834 # Handle optional CORS settings
4935 if (my $CORS_ORIGIN = $self -> config(' CORS_ORIGIN' )) {
@@ -63,42 +49,62 @@ sub startup {
6349
6450 # Logging
6551 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 " ;
6854 $self -> log (Mojo::Log-> new(
6955 path => $logPath ,
7056 level => ($ENV {MOJO_LOG_LEVEL } || ' warn' )
7157 ));
7258 }
7359
7460 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 " ;
7763 my $resultsLog = Mojo::Log-> new(path => $interactionLogPath , level => ' info' );
7864 $resultsLog -> format(sub {
7965 my ($time , $level , @lines ) = @_ ;
8066 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 );
8368 });
8469 $self -> helper(logAttempt => sub { shift ; $resultsLog -> info(@_ ); });
8570 }
8671
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+
8779 # 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 ) });
8981
9082 # 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+ );
97104
98105 # 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 });
102108
103109 $r -> any(' /render-api' )-> to(' render#problem' );
104110 $r -> any(' /render-ptx' )-> to(' render#render_ptx' );
@@ -108,10 +114,12 @@ sub startup {
108114 supplementalRoutes($r ) if ($self -> mode eq ' development' || $self -> config(' FULL_APP_INSECURE' ));
109115
110116 # 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 ;
115123}
116124
117125sub supplementalRoutes {
@@ -156,45 +164,40 @@ sub timeout {
156164}
157165
158166sub 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 ;
163168
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 ! /$!! ;
167170
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 );
171176
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 !/$ ! ! ;
178183
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 } . ' /' );
182186 } else {
183187 # 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 });
185189 }
186190
187- if ($ENV {formURL } =~ m !\S ! ) {
188-
191+ if ($self -> config-> {formURL } =~ m !\S ! ) {
189192 # 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 });
191194 die ' *** [CONFIG] if provided, formURL must be absolute'
192- unless $main:: formURL -> is_abs;
195+ unless $self -> defaults -> { formURL } -> is_abs;
193196 } else {
194197 # if using MITM proxy base href + renderer api not at SITE_HOST root
195198 # 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' );
198201 }
199202}
200203
0 commit comments