forked from openwebwork/renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.pm
More file actions
201 lines (163 loc) · 6.39 KB
/
Renderer.pm
File metadata and controls
201 lines (163 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package Renderer;
use Mojo::Base 'Mojolicious';
BEGIN {
use Mojo::File;
$main::libname = Mojo::File::curfile->dirname;
# RENDER_ROOT is required for initializing conf files.
$ENV{RENDER_ROOT} = $main::libname->dirname
unless (defined($ENV{RENDER_ROOT}));
# PG_ROOT is required for PG/lib/PGEnvironment.pm
$ENV{PG_ROOT} = $main::libname . '/PG';
# Used for reconstructing library paths from sym-links.
$ENV{OPL_DIRECTORY} = "$ENV{RENDER_ROOT}/webwork-open-problem-library";
$ENV{MOJO_CONFIG} =
(-r "$ENV{RENDER_ROOT}/renderer.conf")
? "$ENV{RENDER_ROOT}/renderer.conf"
: "$ENV{RENDER_ROOT}/renderer.conf.dist";
$ENV{MOJO_LOG_LEVEL} = 'debug';
}
use lib "$main::libname";
print "using root directory: $ENV{RENDER_ROOT}\n";
use Renderer::Model::Problem;
use Renderer::Controller::IO;
use WeBWorK::FormatRenderedProblem;
sub startup {
my $self = shift;
# Merge environment variables with config file
$self->plugin('Config');
$self->plugin('TagHelpers');
$self->secrets($self->config('secrets'));
for (qw(problemJWTsecret webworkJWTsecret baseURL formURL SITE_HOST STRICT_JWT)) {
$ENV{$_} //= $self->config($_);
}
sanitizeHostURLs();
print "Renderer is based at $main::basehref\n";
print "Problem attempts will be sent to $main::formURL\n";
# Handle optional CORS settings
if (my $CORS_ORIGIN = $self->config('CORS_ORIGIN')) {
die "CORS_ORIGIN ($CORS_ORIGIN) must be an absolute URL or '*'"
unless ($CORS_ORIGIN eq '*' || $CORS_ORIGIN =~ /^https?:\/\//);
warn "*** [CONFIG] Using '*' for CORS_ORIGIN is insecure\n"
if ($CORS_ORIGIN eq '*');
$self->hook(
before_dispatch => sub {
my $c = shift;
$c->res->headers->header('Access-Control-Allow-Origin' => $CORS_ORIGIN);
}
);
}
# Logging
if ($ENV{MOJO_MODE} && $ENV{MOJO_MODE} eq 'production') {
my $logPath = "$ENV{RENDER_ROOT}/logs/error.log";
print "[LOGS] Running in production mode, logging to $logPath\n";
$self->log(Mojo::Log->new(
path => $logPath,
level => ($ENV{MOJO_LOG_LEVEL} || 'warn')
));
}
if ($self->config('INTERACTION_LOG')) {
my $interactionLogPath = "$ENV{RENDER_ROOT}/logs/interactions.log";
print "[LOGS] Saving interactions to $interactionLogPath\n";
my $resultsLog = Mojo::Log->new(path => $interactionLogPath, level => 'info');
$resultsLog->format(sub {
my ($time, $level, @lines) = @_;
my $start = shift(@lines);
my $msg = join ", ", @lines;
return sprintf "%s, %s, %s\n", $start, $time - $start, $msg;
});
$self->helper(logAttempt => sub { shift; $resultsLog->info(@_); });
}
# Models
$self->helper(newProblem => sub { shift; Renderer::Model::Problem->new(@_) });
# Helpers
$self->helper(format => sub { WeBWorK::FormatRenderedProblem::formatRenderedProblem(@_) });
$self->helper(validateRequest => sub { Renderer::Controller::IO::validate(@_) });
$self->helper(parseRequest => sub { Renderer::Controller::Render::parseRequest(@_) });
$self->helper(croak => sub { Renderer::Controller::Render::croak(@_) });
$self->helper(logID => sub { shift->req->request_id });
$self->helper(exception => sub { Renderer(@_) });
# Routes
# baseURL sets the root at which the renderer is listening,
# and is used in Environment for pg_root_url
my $r = $self->routes->under($ENV{baseURL});
$r->any('/render-api')->to('render#problem');
$r->any('/render-ptx')->to('render#render_ptx');
$r->any('/health' => sub { shift->rendered(200) });
# Enable problem editor & OPL browser -- NOT recommended for production environment!
supplementalRoutes($r) if ($self->mode eq 'development' || $self->config('FULL_APP_INSECURE'));
# Static file routes
$r->any('/pg_files/CAPA_Graphics/*static')->to('StaticFiles#CAPA_graphics_file');
$r->any('/pg_files/tmp/*static')->to('StaticFiles#temp_file');
$r->any('/pg_files/*static')->to('StaticFiles#pg_file');
$r->any('/*static')->to('StaticFiles#public_file');
}
sub supplementalRoutes {
my $r = shift;
# UI
$r->any('/')->to('pages#twocolumn');
$r->any('/opl')->to('pages#oplUI');
# Testing
$r->any('/die' => sub { die "what did you expect, flowers?" });
$r->any('/timeout' => sub { timeout(@_) });
# JWT Convenience
$r->any('/render-api/jwt')->to('render#jwtFromRequest');
$r->any('/render-api/jwe')->to('render#jweFromRequest');
# Library Actions
$r->any('/render-api/tap')->to('IO#raw');
$r->post('/render-api/can')->to('IO#writer');
$r->any('/render-api/cat')->to('IO#catalog');
$r->any('/render-api/find')->to('IO#search');
$r->post('/render-api/upload')->to('IO#upload');
$r->delete('/render-api/remove')->to('IO#remove');
$r->post('/render-api/clone')->to('IO#clone');
$r->post('/render-api/tags')->to('IO#setTags');
# ShowMeAnother Support Functions
$r->post('/render-api/sma')->to('IO#findNewVersion');
$r->post('/render-api/unique')->to('IO#findUniqueSeeds');
}
sub timeout {
my $c = shift;
my $tx = $c->render_later->tx;
Mojo::IOLoop->timer(
2 => sub {
$tx = $tx; # prevent $tx from going out of scope
$c->rendered(200);
}
);
}
sub sanitizeHostURLs {
$ENV{SITE_HOST} =~ s!/$!!;
# set an absolute base href for asset urls under iframe embedding
if ($ENV{baseURL} =~ m!^https?://!) {
# this should only be used by MITM sites when proxying renderer assets
my $baseURL = $ENV{baseURL} =~ m!/$! ? $ENV{baseURL} : "$ENV{baseURL}/";
$main::basehref = Mojo::URL->new($baseURL);
# do NOT use the proxy address in our router!
$ENV{baseURL} = '';
} elsif ($ENV{baseURL} =~ m!\S!) {
# ENV{baseURL} is used to build routes, so configure as "/extension"
$ENV{baseURL} = "/$ENV{baseURL}";
warn "*** [CONFIG] baseURL should not end in a slash\n"
if $ENV{baseURL} =~ s!/$!!;
warn "*** [CONFIG] baseURL should begin with a slash\n"
unless $ENV{baseURL} =~ s!^//!/!;
# base href must end in a slash when not hosting at the root
$main::basehref =
Mojo::URL->new($ENV{SITE_HOST})->path("$ENV{baseURL}/");
} else {
# no proxy and service is hosted at the root of SITE_HOST
$main::basehref = Mojo::URL->new($ENV{SITE_HOST});
}
if ($ENV{formURL} =~ m!\S!) {
# this should only be used by MITM
$main::formURL = Mojo::URL->new($ENV{formURL});
die '*** [CONFIG] if provided, formURL must be absolute'
unless $main::formURL->is_abs;
} else {
# if using MITM proxy base href + renderer api not at SITE_HOST root
# provide form url as absolute SITE_HOST/extension/render-api
$main::formURL =
Mojo::URL->new($ENV{SITE_HOST})->path("$ENV{baseURL}/render-api");
}
}
1;