瀏覽代碼

refactor: cleanup and fix error handling shit,
optimize autoloader,
add sentry integration for production debugging

CoreyAndres 5 年之前
父節點
當前提交
d6781e3663
共有 8 個文件被更改,包括 83 次插入23 次删除
  1. 18 5
      app/container.php
  2. 23 18
      composer.json
  3. 3 0
      config/.config.example.php
  4. 1 0
      public/index.php
  5. 20 0
      resources/views/material/500.tpl
  6. 15 0
      src/Services/Boot.php
  7. 2 0
      src/Services/Config.php
  8. 1 0
      xcat

+ 18 - 5
app/container.php

@@ -21,20 +21,33 @@ $container = new Container($configuration);
 
 $container['notFoundHandler'] = static function ($c) {
     return static function ($request, $response) use ($c) {
-        return $response->withAddedHeader('Location', '/404');
+        $view = View::getSmarty();
+        return $response->withStatus(404)->write($view->fetch('404.tpl'));
     };
 };
 
 $container['notAllowedHandler'] = static function ($c) {
     return static function ($request, $response, $methods) use ($c) {
-        return $response->withAddedHeader('Location', '/405');
+        $view = View::getSmarty();
+        return $response->withStatus(405)->write($view->fetch('405.tpl'));
     };
 };
 
 if ($_ENV['debug'] === false) {
-    $container['errorHandler'] = static function ($c) {
-        return static function ($request, $response, $exception) use ($c) {
-            return $response->withAddedHeader('Location', '/500');
+    $container['errorHandler'] = function ($c) {
+        return function ($request, $response, $exception) use ($c) {
+            $view = View::getSmarty();
+            $exceptionId = empty($_ENV['sentry_dsn']) ? null : Sentry\captureException($exception);
+            return $response->withStatus(500)
+                ->write($view->assign('exceptionId', $exceptionId)->fetch('500.tpl'));
+        };
+    };
+    $container['phpErrorHandler'] = function ($c) {
+        return function ($request, $response, $exception) use ($c) {
+            $view = View::getSmarty();
+            $exceptionId = empty($_ENV['sentry_dsn']) ? null : Sentry\captureException($exception);
+            return $response->withStatus(500)
+                ->write($view->assign('exceptionId', $exceptionId)->fetch('500.tpl'));
         };
     };
 }

+ 23 - 18
composer.json

@@ -4,33 +4,38 @@
         "ext-curl": "*",
         "ext-mysqli": "*",
         "ext-xml": "*",
-        "slim/slim": "~3.0",
-        "smarty/smarty": "3.*",
-        "mailgun/mailgun-php": "^2.6.0",
-        "predis/predis": "~1.0",
-        "phpmailer/phpmailer": "~6.0",
-        "zeuxisoo/slim-whoops": "0.5.*",
-        "firebase/php-jwt": "~3.0",
         "aws/aws-sdk-php": "3.*",
-        "voku/anti-xss": "^1.2",
-        "paymentwall/paymentwall-php": "^2.1",
-        "telegram-bot/api": "^2.2",
-        "illuminate/pagination": "5.2.*",
-        "illuminate/database": "5.2.*",
-        "sendgrid/sendgrid": "~7",
-        "esdeathlove/datatables": "^1.6",
-        "ramsey/uuid": "^3.0@dev",
-        "cloudflare/sdk": "1.1.1",
         "chen-see/chen-pay": "^1.0@dev",
+        "cloudflare/sdk": "1.1.1",
+        "esdeathlove/datatables": "^1.6",
+        "firebase/php-jwt": "~3.0",
+        "illuminate/database": "5.2.*",
+        "illuminate/pagination": "5.2.*",
+        "irazasyed/telegram-bot-sdk": "^2.0",
+        "khanamiryan/qrcode-detector-decoder": "*",
         "league/omnipay": "^3",
         "lokielse/omnipay-alipay": "*",
-        "khanamiryan/qrcode-detector-decoder": "*",
+        "mailgun/mailgun-php": "^2.6.0",
+        "paymentwall/paymentwall-php": "^2.1",
+        "phpmailer/phpmailer": "~6.0",
+        "predis/predis": "~1.0",
+        "ramsey/uuid": "^3.0@dev",
+        "sendgrid/sendgrid": "~7",
+        "sentry/sdk": "^2.1",
+        "slim/slim": "~3.0",
+        "smarty/smarty": "3.*",
         "symfony/yaml": "^4.4@dev",
-        "irazasyed/telegram-bot-sdk": "^2.0"
+        "telegram-bot/api": "^2.2",
+        "voku/anti-xss": "^1.2",
+        "zeuxisoo/slim-whoops": "0.5.*"
     },
     "autoload": {
         "psr-4": {
             "App\\": "src/"
         }
+    },
+    "config": {
+        "sort-packages": true,
+        "optimize-autoloader": true
     }
 }

+ 3 - 0
config/.config.example.php

@@ -538,3 +538,6 @@ foreach ($_ENV['cdn_forwarded_ip'] as $cdn_forwarded_ip) {
         break;
     }
 }
+
+// https://sentry.io for production debugging
+$_ENV['sentry_dsn'] = '';

+ 1 - 0
public/index.php

@@ -19,6 +19,7 @@ require __DIR__ . '/../app/envload.php';
 use App\Services\Boot;
 
 Boot::setTime();
+Boot::bootSentry();
 Boot::bootDb();
 
 /** @var Slim\Container $container */

+ 20 - 0
resources/views/material/500.tpl

@@ -28,6 +28,9 @@
                 <h1>500 错误</h1>
                 <p>服务娘崩溃了呢... TwT</p>
                 <p>这件事儿不应该发生的...如果反复出现可以提交一下工单联系站主.</p>
+                {if !is_null($exceptionId)}
+                <p>事件 ID: {$exceptionId}</p>
+                {/if}
             </div>
         </div>
         <nav>
@@ -46,6 +49,23 @@
 <script src="https://cdn.jsdelivr.net/gh/ajlkn/[email protected]/dist/skel.min.js"></script>
 <script src="/assets/js/util.js"></script>
 <script src="/assets/js/main.js"></script>
+
+{if !is_null($exceptionId)}
+<script src="https://cdn.jsdelivr.net/npm/@sentry/[email protected]/build/bundle.min.js" integrity="sha256-EIV/iYkbXFgnuIHEdltBOK4eY58n87ADisyDI8/VJPg=" crossorigin="anonymous"></script>
+<script>
+    Sentry.init({
+        dsn: "{$config['sentry_dsn']}"
+    });
+    Sentry.showReportDialog({
+        eventId: '{$exceptionId}',
+        user: {
+            name: '{$user->user_name}',
+            email: '{$user->email}'
+        }
+    });
+</script>
+{/if}
+
 </body>
 
 </html>

+ 15 - 0
src/Services/Boot.php

@@ -3,6 +3,7 @@
 namespace App\Services;
 
 use Illuminate\Database\Capsule\Manager as Capsule;
+use Sentry;
 
 class Boot
 {
@@ -39,4 +40,18 @@ class Boot
         View::$connection = $capsule->getDatabaseManager();
         $capsule->getDatabaseManager()->connection('default')->enableQueryLog();
     }
+
+    public static function bootSentry() {
+        if (empty($_ENV['sentry_dsn'])) {
+            Sentry\init([
+                'dsn' => $_ENV['sentry_dsn'],
+                'prefixes' => [
+                    realpath(__DIR__ . '/../../')
+                ],
+                'in_app_exclude' => [
+                    realpath(__DIR__ . '/../../vendor'),
+                ],
+            ]);
+        }
+    }
 }

+ 2 - 0
src/Services/Config.php

@@ -82,6 +82,8 @@ class Config
             'userCenterClient'        => $_ENV['userCenterClient'],
 
             'old_index_DESC'          => $_ENV['old_index_DESC'],
+
+            'sentry_dsn'              => $_ENV['sentry_dsn'],
         ];
     }
 

+ 1 - 0
xcat

@@ -11,6 +11,7 @@ require __DIR__ . '/config/.config.php';
 require __DIR__ . '/app/envload.php';
 
 Boot::setTime();
+Boot::bootSentry();
 Boot::bootDb();
 
 if (!isset($argv[1])) {