Skip to content

Commit ef4271a

Browse files
committed
refactor: enhance CORS and Gzip middleware header handling, improve Comment model structure, and update Dockerfile permissions
1 parent 12ff589 commit ef4271a

6 files changed

Lines changed: 35 additions & 42 deletions

File tree

app/Middleware/CorsMiddleware.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ public function handle(Request $request, Closure $next)
1515
$header->set('Access-Control-Allow-Origin', '*');
1616
$header->set('Access-Control-Expose-Headers', 'Content-Length, Content-Disposition');
1717

18-
$vary = $header->has('Vary') ? explode(', ', $header->get('Vary')) : [];
19-
$vary = array_unique([...$vary, 'Accept', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Origin']);
20-
$header->set('Vary', join(', ', $vary));
18+
$varyList = ['Accept', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Origin'];
19+
20+
if ($header->has('Vary')) {
21+
$existing = preg_split('/\s*,\s*/', $header->get('Vary'), -1, PREG_SPLIT_NO_EMPTY);
22+
$varyList = array_merge($existing, $varyList);
23+
}
24+
25+
$varyList = array_unique(array_map('trim', $varyList));
26+
$header->set('Vary', implode(', ', $varyList));
2127

2228
if (!$request->method(Request::OPTIONS)) {
2329
return $next($request);

app/Middleware/GzipMiddleware.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ public function handle(Request $request, Closure $next): Stream|Respond
4040

4141
$response->setContent($compressed);
4242

43-
$vary = (!$response->headers->has('Vary')) ? [] : explode(', ', $response->headers->get('Vary'));
44-
$vary = array_unique([...$vary, 'Accept-Encoding']);
43+
$varyList = ['Accept-Encoding'];
4544

46-
$response->headers
47-
->set('Vary', join(', ', $vary))
48-
->set('Content-Encoding', 'gzip');
45+
if ($response->headers->has('Vary')) {
46+
$existing = preg_split('/\s*,\s*/', $response->headers->get('Vary'), -1, PREG_SPLIT_NO_EMPTY);
47+
$varyList = array_merge($existing, $varyList);
48+
}
49+
50+
$varyList = array_unique(array_map('trim', $varyList));
51+
$response->headers->set('Vary', implode(', ', $varyList));
52+
53+
$response->headers->set('Content-Encoding', 'gzip');
4954

5055
return $response;
5156
}

app/Models/Comment.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace App\Models;
44

55
use Core\Model\Model;
6-
use Core\Model\Query;
7-
use Core\Model\Relational;
86

97
final class Comment extends Model
108
{
@@ -29,28 +27,4 @@ final class Comment extends Model
2927
'is_admin' => 'bool',
3028
'created_at' => 'datetime:diff'
3129
];
32-
33-
public function comments(): Relational
34-
{
35-
return $this->hasMany(
36-
Comment::class,
37-
'parent_id',
38-
'uuid',
39-
function (Query $query): Query {
40-
return $query->select(['uuid', 'name', 'presence', 'comment', 'is_admin', 'gif_url', 'created_at', ...(auth()->user()->isAdmin() ? ['ip', 'own', 'user_agent'] : [])])->orderBy('id');
41-
}
42-
)->as('comments')->with($this->likes())->recursive();
43-
}
44-
45-
public function likes(): Relational
46-
{
47-
return $this->belongsTo(
48-
Like::class,
49-
'uuid',
50-
'comment_id',
51-
function (Query $query): Query {
52-
return $query->count('uuid', 'love');
53-
}
54-
)->as('like');
55-
}
5630
}

docker/nginx/default.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
server {
22
listen 80;
3-
server_name localhost;
3+
server_name localhost 127.0.0.1;
44
index index.php;
55

66
error_log /var/log/nginx/error.log;
@@ -9,11 +9,11 @@ server {
99
root /var/www/public;
1010

1111
charset utf-8;
12-
12+
1313
location / {
1414
try_files $uri $uri/ /index.php?$query_string;
1515
}
16-
16+
1717
error_page 404 /index.php;
1818

1919
location ~ \.php$ {

docker/php/Dockerfile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ RUN apt-get install -y libzip-dev libpq-dev
66
RUN apt-get install -y build-essential libssl-dev zlib1g-dev
77
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
88

9-
RUN docker-php-ext-install pdo zip pdo_pgsql pgsql opcache
10-
RUN docker-php-ext-enable pdo zip pdo_pgsql pgsql opcache
9+
RUN docker-php-ext-install pdo zip pdo_pgsql pgsql opcache intl bcmath mbstring
10+
RUN docker-php-ext-enable pdo zip pdo_pgsql pgsql opcache intl bcmath mbstring
1111

1212
COPY /api/php.ini /usr/local/etc/php/conf.d/opcache.ini
1313

@@ -16,11 +16,16 @@ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local
1616
COPY . /var/www
1717
WORKDIR /var/www
1818

19-
RUN chmod 777 -R /var/www/cache
19+
RUN chown -R www-data:www-data /var/www && \
20+
chmod -R 755 /var/www && \
21+
chmod -R 777 /var/www/cache
22+
2023
RUN composer install --prefer-dist --no-dev --optimize-autoloader --no-interaction
2124

2225
RUN php saya key
2326
RUN php saya view:cache
2427

28+
USER www-data
29+
2530
EXPOSE 9000
2631
CMD ["php-fpm"]

public/.htaccess

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
RewriteCond %{REQUEST_URI} (.+)/$
1515
RewriteRule ^ %1 [L,R=301]
1616

17+
# Prevent directory listing
18+
Options -Indexes
19+
1720
# Send Requests To Front Controller...
1821
RewriteCond %{REQUEST_FILENAME} !-d
1922
RewriteCond %{REQUEST_FILENAME} !-f
20-
RewriteRule ^(.+)$ index.php/$1 [L]
21-
</IfModule>
23+
RewriteRule ^ index.php [L]
24+
</IfModule>

0 commit comments

Comments
 (0)