Repository navigation
Expand file tree
/
Copy pathHttpServer.php
More file actions
147 lines (132 loc) · 4.38 KB
/
Copy pathHttpServer.php
File metadata and controls
147 lines (132 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Swagger;
use Hyperf\Codec\Json;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\OnRequestInterface;
use Hyperf\Engine\Http\Stream;
use Hyperf\HttpMessage\Server\Request as Psr7Request;
use Hyperf\HttpMessage\Server\Response;
use Hyperf\HttpServer\ResponseEmitter;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;
class HttpServer implements OnRequestInterface
{
protected array $metadata = [];
protected array $config = [
'enable' => false,
'port' => 9500,
'json_dir' => BASE_PATH . '/storage/swagger',
'html' => null,
'url' => '/swagger',
'auto_generate' => false,
'scan' => [
'paths' => null,
],
];
public function __construct(protected ContainerInterface $container, protected ResponseEmitter $emitter)
{
$this->config = array_merge($this->config, $this->container->get(ConfigInterface::class)->get('swagger', []));
}
public function onRequest($request, $response): void
{
try {
if ($request instanceof ServerRequestInterface) {
$psr7Request = $request;
} else {
$psr7Request = Psr7Request::loadFromSwooleRequest($request);
}
$path = $psr7Request->getUri()->getPath();
if ($path === $this->config['url']) {
$stream = new Stream($this->getHtml());
$contentType = 'text/html;charset=utf-8';
} else {
$stream = new Stream($this->getMetadata($path));
$contentType = 'application/json;charset=utf-8';
}
$psrResponse = (new Response())->setBody($stream)->setHeader('content-type', $contentType);
$this->emitter->emit($psrResponse, $response);
} catch (Throwable) {
$this->emitter->emit(
(new Response())
->setBody(new Stream('Server Error'))
->setHeader('content-type', 'text/html;charset=utf-8'),
$response
);
}
}
protected function getMetadata(string $path): string
{
$path = rtrim($this->config['json_dir'], '/') . $path;
$id = md5($path);
if (isset($this->metadata[$id])) {
return $this->metadata[$id];
}
if (is_file($path) && file_exists($path)) {
$metadata = file_get_contents($path);
} else {
$metadata = Json::encode([
'openapi' => '3.0.0',
]);
}
return $this->metadata[$id] = $metadata;
}
protected function getHtml(): string
{
if (! empty($this->config['html'])) {
return $this->config['html'];
}
return <<<'HTML'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
name="description"
content="SwaggerUI"
/>
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.hyperf.wiki/[email protected]/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.hyperf.wiki/[email protected]/swagger-ui-bundle.js" crossorigin></script>
<script src="https://unpkg.hyperf.wiki/[email protected]/swagger-ui-standalone-preset.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: GetQueryString("search"),
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout",
});
};
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
var context = "";
if (r != null)
context = decodeURIComponent(r[2]);
reg = null;
r = null;
return context == null || context == "" || context == "undefined" ? "/http.json" : context;
}
</script>
</body>
</html>
HTML;
}
}