forked from drdrew42/renderer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRenderApp.pm
120 lines (98 loc) · 4.32 KB
/
RenderApp.pm
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
use strict;
use warnings;
# use feature 'signatures';
# no warnings qw(experimental::signatures);
package RenderApp;
use Mojo::Base 'Mojolicious';
BEGIN {
use Mojo::File;
$main::dirname = Mojo::File::curfile->dirname;
# RENDER_ROOT is required for initializing conf files.
$ENV{RENDER_ROOT} = $main::dirname->dirname
unless ( defined( $ENV{RENDER_ROOT} ) );
# PG_ROOT is required for PG/lib/PGEnvironment.pm, FormatRenderedProblem.pm, and RenderProblem.pm.
# This is hardcoded to avoid conflict with the environment variable for webwork2.
# There is no need for this to be configurable.
$ENV{PG_ROOT} = $main::dirname . '/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}/render_app.conf") ? "$ENV{RENDER_ROOT}/render_app.conf" : "$ENV{RENDER_ROOT}/render_app.conf.dist";
# $ENV{MOJO_MODE} = 'production';
# $ENV{MOJO_LOG_LEVEL} = 'debug';
}
use lib "$main::dirname";
print "home directory " . $main::dirname . "\n";
use RenderApp::Model::Problem;
use RenderApp::Controller::IO;
use WeBWorK::RenderProblem;
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($_);
};
$ENV{baseURL} = '' if ( $ENV{baseURL} eq '/' );
$ENV{SITE_HOST} =~ s|/$||; # remove trailing slash
# $r needs to be defined before the SITE_HOST is added to the baseURL
my $r = $self->routes->under($ENV{baseURL});
# while iFrame embedded problems are likely to need the baseURL to include SITE_HOST
# convert to absolute URLs
$ENV{baseURL} = $ENV{SITE_HOST} . $ENV{baseURL} unless ( $ENV{baseURL} =~ m|^https?://| );
$ENV{formURL} = $ENV{baseURL} . $ENV{formURL} unless ( $ENV{formURL} =~ m|^https?://| );
# 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?:\/\//);
$self->hook(before_dispatch => sub {
my $c = shift;
$c->res->headers->header( 'Access-Control-Allow-Origin' => $CORS_ORIGIN );
});
}
# Models
$self->helper(newProblem => sub { shift; RenderApp::Model::Problem->new(@_) });
# Helpers
$self->helper(format => sub { WeBWorK::FormatRenderedProblem::formatRenderedProblem(@_) });
$self->helper(validateRequest => sub { RenderApp::Controller::IO::validate(@_) });
$self->helper(parseRequest => sub { RenderApp::Controller::Render::parseRequest(@_) });
$self->helper(croak => sub { RenderApp::Controller::Render::croak(@_) });
$self->helper(logID => sub { shift->req->request_id });
$self->helper(exception => sub { RenderApp::Controller::Render::exception(@_) });
# Routes to controller
$r->any('/render-api')->to('render#problem');
$r->any('/health' => sub {shift->rendered(200)});
if ($self->mode eq 'development') {
$r->any('/')->to('pages#twocolumn');
$r->any('/opl')->to('pages#oplUI');
$r->any('/die' => sub {die "what did you expect, flowers?"});
$r->any('/timeout' => sub {
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);
});
});
$r->any('/render-api/jwt')->to('render#jwtFromRequest');
$r->any('/render-api/jwe')->to('render#jweFromRequest');
$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/sma')->to('IO#findNewVersion');
$r->post('/render-api/unique')->to('IO#findUniqueSeeds');
$r->post('/render-api/tags')->to('IO#setTags');
}
# 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');
}
1;