Skip to content

Instantly share code, notes, and snippets.

@daotoad
Last active February 2, 2017 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daotoad/af7ce353b2b66f0939f808e061c49c67 to your computer and use it in GitHub Desktop.
Save daotoad/af7ce353b2b66f0939f808e061c49c67 to your computer and use it in GitHub Desktop.
Trying to set up CORS headers for static files in Mojo
sub startup {
my $self = shift;
# Router
my $r = $self->routes;
my $log = $self->log;
# Normal route to controller
$r->get('/')->to('example#welcome');
$self->hook( after_static => sub {
my $c = shift;
my $req_headers = $c->req->headers;
$log->append("Processing after static hook.\n");
$log->append(Dumper $req_headers );
my $origin = $req_headers->origin;
$log->append("Origin header is $origin.\n");
return 1 unless defined $origin;
if ( $origin =~ /tutchy/ ) {
$log->append("Origin matches adding CORS\n");
$c->res->headers->vary('Origin');
$c->res->headers->append('Access-Control-Allow-Origin', $origin );
return 1;
}
$c->res->content( Mojo::Content::Single->new );
$c->rendered(400);
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment