Skip to content

Commit 57b40f6

Browse files
committed
add scalar html
1 parent 8240991 commit 57b40f6

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ php bin/hyperf.php start
208208
* 访问`/swagger`可以看到swagger页面
209209
* 已安装[knife4j-ui](https://github.com/tw2066/knife4j-ui),访问`/swagger/doc`可以看到knife4j页面
210210
* 访问`/swagger/redoc`,可以看到[redoc](https://github.com/Redocly/redoc)页面
211+
* 访问`/swagger/scalar`,可以看到[scalar](https://github.com/scalar/scalar)页面
211212
* 访问`/swagger/rapidoc`,可以看到[rapidoc](https://github.com/rapi-doc/RapiDoc)页面
212213

213214
## 注解

src/Listener/BootAppRouteListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Hyperf\ApiDocs\Listener;
66

7-
use Hyperf\ApiDocs\Swagger\SwaggerUiController;
87
use Hyperf\ApiDocs\Swagger\SwaggerConfig;
98
use Hyperf\ApiDocs\Swagger\SwaggerController;
9+
use Hyperf\ApiDocs\Swagger\SwaggerUiController;
1010
use Hyperf\Contract\ConfigInterface;
1111
use Hyperf\Contract\StdoutLoggerInterface;
1212
use Hyperf\DTO\DtoValidation;
@@ -75,6 +75,7 @@ public function process(object $event): void
7575
$route->get('', [SwaggerUiController::class, 'swagger']);
7676
$route->get('/redoc', [SwaggerUiController::class, 'redoc']);
7777
$route->get('/rapidoc', [SwaggerUiController::class, 'rapidoc']);
78+
$route->get('/scalar', [SwaggerUiController::class, 'scalar']);
7879
$route->get('/doc', [SwaggerUiController::class, 'knife4j']);
7980
$route->get('/swagger-resources', [SwaggerUiController::class, 'swaggerResources']);
8081
$route->get('/webjars/{file:.*}', [SwaggerUiController::class, 'knife4jFile']);

src/Swagger/SwaggerUiController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public function rapidoc(): PsrResponseInterface
4747
$contents = str_replace('{{$url}}', BootAppRouteListener::$httpServerName . '.' . $this->swaggerConfig->getFormat(), $contents);
4848
return $this->response->withAddedHeader('content-type', 'text/html')->withBody(new SwooleStream($contents));
4949
}
50+
public function scalar(): PsrResponseInterface
51+
{
52+
// https://github.com/scalar/scalar
53+
$filePath = $this->docsWebPath . '/scalar.html';
54+
$contents = file_get_contents($filePath);
55+
$contents = str_replace('{{$url}}', BootAppRouteListener::$httpServerName . '.' . $this->swaggerConfig->getFormat(), $contents);
56+
return $this->response->withAddedHeader('content-type', 'text/html')->withBody(new SwooleStream($contents));
57+
}
5058

5159
public function knife4j()
5260
{

src/web/scalar.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Scalar API Reference</title>
5+
<meta charset="utf-8" />
6+
<meta
7+
name="viewport"
8+
content="width=device-width, initial-scale=1" />
9+
</head>
10+
11+
<body>
12+
<div id="app"></div>
13+
14+
<!-- Load the Script -->
15+
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
16+
17+
<!-- Initialize the Scalar API Reference -->
18+
<script>
19+
Scalar.createApiReference('#app', {
20+
// The URL of the OpenAPI/Swagger document
21+
url: '{{$url}}',
22+
// Avoid CORS issues
23+
// proxyUrl: '',
24+
})
25+
</script>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)