Skip to content

Commit

Permalink
Merge pull request #107 from vatsimnetwork/development
Browse files Browse the repository at this point in the history
1.5.1
  • Loading branch information
lieselwd authored Oct 22, 2023
2 parents b601dd7 + 276a00b commit a343d52
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 627 deletions.
79 changes: 33 additions & 46 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,42 @@ RUN set -ex \
&& npm ci \
&& VITE_PUSHER_APP_KEY=${VITE_PUSHER_APP_KEY} VITE_PUSHER_HOST=${VITE_PUSHER_HOST} VITE_PUSHER_PORT=${VITE_PUSHER_PORT} npm run build

FROM composer:2.4.2 as vendor
FROM unit:1.31.1-php8.2

WORKDIR /app

COPY database/ database/
COPY app/ app/
COPY composer.json composer.lock ./

RUN composer install \
--prefer-dist \
--no-dev \
--no-scripts \
--no-plugins \
--no-interaction \
--ignore-platform-reqs

FROM php:8.1-fpm-alpine

RUN apk update && apk add --no-cache \
supervisor \
curl \
openssl \
nginx \
libxml2-dev \
oniguruma-dev \
libzip-dev \
libjpeg-turbo-dev \
libpng-dev \
freetype-dev \
pcre-dev $PHPIZE_DEPS \
&& rm -rf /var/cache/apk/*

RUN docker-php-ext-configure gd --with-jpeg --with-freetype
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl dom bcmath gd
RUN pecl install redis && docker-php-ext-enable redis.so
ARG NEW_RELIC_AGENT_VERSION=10.13.0.2

# Install PHP extensions
RUN set -ex \
&& apt-get update \
&& apt-get install --no-install-recommends -y git unzip \
&& docker-php-ext-install bcmath opcache pcntl pdo_mysql \
&& pecl install redis-5.3.7 \
&& apt-get purge -y --auto-remove git \
&& curl -L https://download.newrelic.com/php_agent/archive/${NEW_RELIC_AGENT_VERSION}/newrelic-php5-${NEW_RELIC_AGENT_VERSION}-linux.tar.gz | tar -C /tmp -zx \
&& cp /tmp/newrelic-php5-${NEW_RELIC_AGENT_VERSION}-linux/agent/x64/newrelic-20220829.so /usr/local/lib/php/extensions/no-debug-non-zts-20220829/newrelic.so \
&& rm -rf /tmp/newrelic-php5-* /tmp/pear /var/lib/apt/lists/* \
&& docker-php-ext-enable newrelic redis

# Install Composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# Configure PHP & Unit
COPY docker/php.ini /usr/local/etc/php/php.ini
COPY docker/bootstrap-laravel.sh /docker-entrypoint.d/
COPY docker/unit.json /docker-entrypoint.d/

# Copy application files
WORKDIR /var/www/app
COPY --chown=www-data:www-data . /var/www/app
COPY --chown=www-data:www-data --from=vendor /app/vendor /var/www/app/vendor
COPY --chown=www-data:www-data --from=nodejs /var/www/app/public/build /var/www/app/public/build
COPY --chown=unit:unit . /var/www/app

# Install Composer dependencies
RUN set -ex \
&& php artisan vendor:publish --tag=livewire:assets --force \
&& chown -R www-data:www-data public/vendor/livewire
&& composer install --no-dev --optimize-autoloader \
&& php artisan vendor:publish --tag=livewire:assets \
&& rm -rf /root/.composer \
&& chown -R unit:unit bootstrap/cache public/vendor/livewire vendor

RUN rm /usr/local/etc/php-fpm.d/zz-docker.conf
COPY docker/php-fpm.conf /usr/local/etc/php-fpm.d/www.conf
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY docker/php.ini /etc/php8/conf.d/50-setting.ini
COPY docker/nginx.conf /etc/nginx/nginx.conf
# Copy built frontend files
COPY --from=nodejs --chown=unit:unit /var/www/app/public/build /var/www/app/public/build

EXPOSE 80
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# CMD and ENTRYPOINT are inherited from the Unit image
90 changes: 38 additions & 52 deletions app/Http/Controllers/Api/PluginDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,33 @@
use App\Services\VatsimDataService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;

class PluginDataController extends Controller
{
public VatsimDataService $dataService;

public function __construct(VatsimDataService $dataService)
{
$this->dataService = $dataService;
}

private function formatTime($time)
{
return substr($time, 0, 2).':'.substr($time, 2, 2);
}

private function formatClxData(Collection $data): Collection
{
return $data->map(function (ClxMessage $msg) {
return [
'callsign' => $msg->rclMessage->callsign,
'status' => 'CLEARED',
'nat' => $msg->track ? $msg->track->identifier : 'RR',
'fix' => $msg->entry_fix,
'level' => $msg->flight_level,
'mach' => '0.'.substr($msg->mach, 1),
'estimating_time' => $this->formatTime($msg->rclMessage->entry_time),
'clearance_issued' => $msg->created_at,
'extra_info' => $msg->free_text,
];
});
}

private function formatRclData(Collection $data): Collection
{
return $data->map(function (RclMessage $msg) {
return [
'callsign' => $msg->callsign,
'status' => $msg->clxMessages->count() > 0 ? 'CLEARED' : 'PENDING',
'nat' => $msg->track ? $msg->track->identifier : 'RR',
'fix' => $msg->entry_fix,
'level' => $msg->flight_level,
'mach' => '0.'.substr($msg->mach, 1),
'estimating_time' => $this->formatTime($msg->entry_time),
'clearance_issued' => $msg->clxMessages->count() > 0 ? $msg->clxMessages->first()->created_at : null,
'extra_info' => $msg->free_text,
];
});
public function __construct(
public VatsimDataService $dataService
) {
}

public function allRclMessages(Request $request)
{
$rclMessages = RclMessage::when($request->has('track'), function (Builder $query) use ($request) {
$track = Track::active()->whereIdentifier($request->get('track'))->first();
$track = null;
if ($trackIdent = $request->get('track')) {
$track = Track::active()->whereIdentifier($trackIdent)->first();
if (! $track) {
abort(400, "Track with identifier {$request->get('track')} not active at present time.");
abort(400, "Track with identifier $trackIdent not active at present time.");
}
$query->where('track_id', $track->id);
})->with('clxMessages')->get();
}

return response($this->formatRclData($rclMessages));
return Cache::remember(
'rcl-messages:'.($track->id ?? 'all'),
60,
fn () => RclMessage::with('clxMessages')
->when($track, fn (Builder $q) => $q->whereBelongsTo($track))
->get()
->map(self::serializeRclMessage(...))
);
}

public function detailedClxMessages(Request $request)
Expand Down Expand Up @@ -113,7 +79,7 @@ public function detailedClxMessages(Request $request)
],
'entry' => [
'fix' => $msg->entry_fix,
'estimate' => $this->formatTime($msg->rclMessage->entry_time),
'estimate' => self::formatTime($msg->rclMessage->entry_time),
'restriction' => $msg->entry_time_restriction,
],
'extra_info' => $msg->free_text,
Expand All @@ -131,4 +97,24 @@ public function getTracks(Request $request)

return response($tracks);
}

private static function serializeRclMessage(RclMessage $msg): array
{
return [
'callsign' => $msg->callsign,
'status' => $msg->clxMessages->count() ? 'CLEARED' : 'PENDING',
'nat' => $msg->track->identifier ?? 'RR',
'fix' => $msg->entry_fix,
'level' => $msg->flight_level,
'mach' => '0.'.substr($msg->mach, 1),
'estimating_time' => self::formatTime($msg->entry_time),
'clearance_issued' => $msg->clxMessages->first()?->created_at,
'extra_info' => $msg->free_text,
];
}

private static function formatTime($time)
{
return substr($time, 0, 2).':'.substr($time, 2, 2);
}
}
6 changes: 3 additions & 3 deletions app/Http/Requests/RclMessageRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function withValidator($validator)
$validator->errors()->add('mach.regex', 'Mach must be in format 0xx (e.g. .74 = 074)');
}
/** Entry fix time requirement */
if (config('app.rcl_time_constraints_enabled')) {
if (config('app.rcl_time_constraints_enabled') && strlen($this->entry_time) == 4) {
if (!$this->entryTimeWithinRange($this->entry_time)) {
$validator->errors()->add('entry_time.range', 'You are either too early or too late to submit oceanic clearance. If you are entering the oceanic more than 45 minutes from now, come back when within 45 minutes. If your entry is within 15 minutes, or you have already entered, request clearance via voice.');
}
Expand Down Expand Up @@ -107,7 +107,7 @@ private function entryTimeWithinRange($input): bool
$minutesDifference = $currentDateTime->diffInMinutes($entryTime);

// Check if the difference is within the range [15, 45] minutes and not negative (entry time is in the future)
if ($minutesDifference >= 15 && $minutesDifference <= 45) {
if ($minutesDifference >= 14 && $minutesDifference <= 61) {
return true;
}

Expand All @@ -116,7 +116,7 @@ private function entryTimeWithinRange($input): bool
$minutesToMidnight = $currentDateTime->diffInMinutes($midnight);
$minutesFromMidnight = $entryTime->diffInMinutes($midnight);

if ($minutesToMidnight >= 15 && $minutesFromMidnight >= 0 && $minutesFromMidnight <= 45) {
if ($minutesToMidnight >= 14 && $minutesFromMidnight >= 0 && $minutesFromMidnight <= 61) {
return true;
}

Expand Down
12 changes: 12 additions & 0 deletions docker/bootstrap-laravel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

set -eux

cd /var/www/app

# Cache/compile Laravel structs
/usr/local/bin/php artisan config:cache
/usr/local/bin/php artisan event:cache
/usr/local/bin/php artisan route:cache
/usr/local/bin/php artisan view:cache
/bin/chown -R unit:unit bootstrap/cache storage/framework/views
32 changes: 0 additions & 32 deletions docker/nginx.conf

This file was deleted.

Loading

0 comments on commit a343d52

Please sign in to comment.