From e8456e63920058f2e4dfc4dfb6124505120f5396 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 2 Jul 2024 12:51:13 +0200 Subject: [PATCH] feat: refs #7022 Added cdn --- cdn/Dockerfile | 20 ++++++++++++++++++++ cdn/apache2.conf | 18 ++++++++++++++++++ cdn/docker-compose.yml | 8 ++++++++ cdn/rwmap.pl | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 cdn/Dockerfile create mode 100644 cdn/apache2.conf create mode 100644 cdn/docker-compose.yml create mode 100755 cdn/rwmap.pl diff --git a/cdn/Dockerfile b/cdn/Dockerfile new file mode 100644 index 0000000..055ca85 --- /dev/null +++ b/cdn/Dockerfile @@ -0,0 +1,20 @@ +FROM httpd:2.4.60 + +RUN apt-get update \ + && apt-get install -y curl libcgi-pm-perl \ + && rm -rf /var/lib/apt/lists/* + +RUN echo "ServerName localhost\n" \ + "Include conf/extra/httpd-vhosts.conf\n" \ + "LoadModule rewrite_module modules/mod_rewrite.so\n" \ + "LoadModule cgid_module modules/mod_cgid.so\n" \ + "LoadModule proxy_module modules/mod_proxy.so\n" \ + "LoadModule proxy_http_module modules/mod_proxy_http.so" \ + >> "/usr/local/apache2/conf/httpd.conf" + +COPY apache2.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf + +COPY rwmap.pl /usr/local/apache2/htdocs/ +RUN chmod +x /usr/local/apache2/htdocs/rwmap.pl + +CMD ["httpd-foreground"] diff --git a/cdn/apache2.conf b/cdn/apache2.conf new file mode 100644 index 0000000..c685548 --- /dev/null +++ b/cdn/apache2.conf @@ -0,0 +1,18 @@ + + DocumentRoot /usr/local/apache2/htdocs + + RewriteEngine On + RewriteMap rwmap prg:/usr/local/apache2/htdocs/rwmap.pl + RewriteRule ^/image/catalog/(.+)$ ${rwmap:$1} [PT,QSA] + + + Require all granted + + + + Options Indexes FollowSymLinks ExecCGI + AllowOverride All + Require all granted + AddHandler cgi-script .pl + + \ No newline at end of file diff --git a/cdn/docker-compose.yml b/cdn/docker-compose.yml new file mode 100644 index 0000000..ac8896a --- /dev/null +++ b/cdn/docker-compose.yml @@ -0,0 +1,8 @@ +version: '3' +services: + apache: + build: + context: . + dockerfile: Dockerfile + ports: + - "80:80" diff --git a/cdn/rwmap.pl b/cdn/rwmap.pl new file mode 100755 index 0000000..c150734 --- /dev/null +++ b/cdn/rwmap.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Digest::SHA qw(sha1_hex); +# Desactiva el buffering de salida para que Apache pueda obtener la respuesta inmediatamente +$| = 1; +my $serverName = $ENV{'SERVER_NAME'} || 'localhost'; +my $serverPort = $ENV{'SERVER_PORT'} || '80'; +my $scheme = ($serverPort == 443) ? 'https' : 'http'; +my $baseUrl = "$scheme://$serverName"; +$baseUrl .= ":$serverPort" if ($serverPort != 80 && $serverPort != 443); + +while () { + chomp; + my $input = $_; + my ($path, $filename) = split('/', $input); + + if (defined $filename && $filename =~ /^(.+)\.(\w+)$/) { + my $name = $1; + my $extension = $2; + + my $sha1 = sha1_hex($name); + my $first = substr($sha1, 0, 2); + my $last = substr($sha1, 1, 2); + + if (-e "images/catalog/$path/$first/$last/$name.$extension") { + print "$baseUrl/images/catalog/$path/$first/$last/$name.$extension\n"; + } + else { + print "$baseUrl/images/catalog/$path/$name.$extension\n"; + } + } else { + print "Status: 400 Bad Request\n"; + } +}