nhance/app/Controllers/SwaggerController.php

84 lines
3.0 KiB
PHP
Executable File

<?php namespace App\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\Exceptions\PageNotFoundException;
class SwaggerController extends BaseController
{
public function index()
{
return view('swagger/index', [
'specUrl' => base_url('assets/api.yaml'),
'pageTitle' => 'Swagger UI',
'swaggerPreauthorize' => [],
]);
}
/**
* Swagger UI for TPA external integrations OpenAPI spec (openapi/tpa-external-integrations.yaml).
* Pre-fills Authorize from the same `.env` keys the API controllers use (`getenv`).
*/
public function tpa()
{
return view('swagger/index', [
'specUrl' => base_url('swagger/tpa-spec'),
'pageTitle' => 'TPA external APIs — Swagger UI',
'swaggerPreauthorize' => $this->buildTpaSwaggerPreauthorize(),
]);
}
/**
* Values for Swagger UI Authorize (apiKey / basic). Uses runtime env — keep `/swagger/tpa` access-controlled.
*
* @return list<array<string, string>>
*/
protected function buildTpaSwaggerPreauthorize(): array
{
return [
['kind' => 'apiKey', 'scheme' => 'MediAssistUsername', 'value' => $this->readEnvString('MEDI_ASSIST_API_USERNAME')],
['kind' => 'apiKey', 'scheme' => 'MediAssistPassword', 'value' => $this->readEnvString('MEDI_ASSIST_API_PASSWORD')],
['kind' => 'apiKey', 'scheme' => 'VidalSubscriptionKey', 'value' => $this->readEnvString('VIDAL_API_SUBSCRIPTION_KEY')],
['kind' => 'apiKey', 'scheme' => 'VidalIrSubscriptionKey', 'value' => $this->readEnvString('VIDAL_SUBSCRIPTION_KEY')],
['kind' => 'apiKey', 'scheme' => 'VidalWellnessSubscriptionKey', 'value' => $this->readEnvString('VIDAL_WELLNESS_SUBSCRIPTION_KEY')],
[
'kind' => 'basic',
'scheme' => 'HealthIndiaTokenBasic',
'username' => $this->readEnvString('HEALTH_INDIA_USERNAME'),
'password' => $this->readEnvString('HEALTH_INDIA_PASSWORD'),
],
];
}
/**
* Read `.env` the same way as the rest of CodeIgniter (`env()` then `getenv()`).
*/
protected function readEnvString(string $key): string
{
if (function_exists('env')) {
$v = \env($key);
if ($v !== null) {
return \is_bool($v) ? '' : (string) $v;
}
}
$g = \getenv($key);
return $g === false ? '' : (string) $g;
}
/**
* Serves the OpenAPI YAML from project root /openapi (not under public/).
*/
public function tpaSpec()
{
$path = ROOTPATH . 'openapi/tpa-external-integrations.yaml';
if (! is_readable($path)) {
throw PageNotFoundException::forPageNotFound('OpenAPI spec not found.');
}
return $this->response
->setHeader('Content-Type', 'application/yaml; charset=UTF-8')
->setHeader('Cache-Control', 'public, max-age=300')
->setBody(file_get_contents($path));
}
}