Last active
February 2, 2017 01:40
-
-
Save daotoad/af7ce353b2b66f0939f808e061c49c67 to your computer and use it in GitHub Desktop.
Trying to set up CORS headers for static files in Mojo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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