Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit 832f448

Browse files
committed
init
0 parents  commit 832f448

33 files changed

+3172
-0
lines changed

.env.example

Whitespace-only changes.

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.idea
2+
/.vscode
3+
/vendor
4+
*.log
5+
.env
6+
/tests/tmp
7+
/tests/.phpunit.result.cache

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
MIT License
2+
3+
Webman:
4+
Copyright (c) 2021 walkor<[email protected]> and contributors (see https://github.com/walkor/webman/contributors)
5+
6+
Zhblogs-back-end:
7+
Copyright (c) 2022 zhblogs and contributors (see https://github.com/zh-blogs/zhblogs-back-end)
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.

app/functions.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Webman\Http\Request;
4+
5+
/**
6+
* 处理API返回值
7+
* @param int code
8+
* @param string msg
9+
* @param array data
10+
* @return \support\Response
11+
*/
12+
function api(int $code = 0, string $msg = 'success', array|object $data = []): \support\Response
13+
{
14+
return json([
15+
'code' => $code,
16+
'msg' => $msg,
17+
'data' => $data,
18+
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
19+
}
20+
21+
function getIp(Request $request): String
22+
{
23+
$ip = $request->header('cf-connecting-ip') ?? $request->header('x-forwarded-for');
24+
if (!$ip) $ip = $request->header('x-real-ip');
25+
return $ip;
26+
}

app/middleware/AccessControl.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace app\middleware;
4+
5+
use Webman\MiddlewareInterface;
6+
use Webman\Http\Response;
7+
use Webman\Http\Request;
8+
9+
/**
10+
* 设置跨域请求
11+
*/
12+
class AccessControl implements MiddlewareInterface
13+
{
14+
public function process(Request $request, callable $next): Response
15+
{
16+
$response = $request->method() == 'OPTIONS' ? api(-1, 'permission deniend') : $next($request);
17+
18+
$response->withHeaders([
19+
'Access-Control-Allow-Origin' => '*',
20+
'Content-Type' => "Application/json; charset=utf-8",
21+
'Server' => 'xcsoft'
22+
]);
23+
24+
return $response;
25+
}
26+
}

app/middleware/RateLimit.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace app\middleware;
4+
5+
use common\Ip;
6+
use Webman\MiddlewareInterface;
7+
use Webman\Http\{Response, Request};
8+
9+
use RateLimit\{Rate, RateLimiter};
10+
11+
/**
12+
* 速率限制
13+
*/
14+
class RateLimit implements MiddlewareInterface
15+
{
16+
public function process(Request $request, callable $next): Response
17+
{
18+
$redis = new \Redis();
19+
$redis->connect(config('redis.default.host'), config('redis.default.port'));
20+
$redis->select(config('redis.default.database'));
21+
22+
$user_agent = $request->header('user-agent');
23+
24+
$limit = Rate::perMinute(120);
25+
$rateLimiter = new RateLimiter($limit, $redis, 'zh:rate_limit:');
26+
$status = $rateLimiter->limitSilently(md5(getIp($request) . $user_agent));
27+
28+
if ($status->left() === 0) return api(-1, 'Exceeded rate limit');
29+
30+
return $next($request);
31+
}
32+
}

composer.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "workerman/webman",
3+
"type": "project",
4+
"keywords": [
5+
"high performance",
6+
"http service"
7+
],
8+
"homepage": "http://www.workerman.net",
9+
"license": "MIT",
10+
"description": "High performance HTTP Service Framework.",
11+
"authors": [
12+
{
13+
"name": "walkor",
14+
"email": "[email protected]",
15+
"homepage": "http://www.workerman.net",
16+
"role": "Developer"
17+
}
18+
],
19+
"support": {
20+
"email": "[email protected]",
21+
"issues": "https://github.com/walkor/webman/issues",
22+
"forum": "http://wenda.workerman.net/",
23+
"wiki": "http://workerman.net/doc/webman",
24+
"source": "https://github.com/walkor/webman"
25+
},
26+
"require": {
27+
"php": ">=7.2",
28+
"workerman/webman-framework": "^1.3.0",
29+
"monolog/monolog": "^2.0",
30+
"vlucas/phpdotenv": "^5.4",
31+
"soxft/rate-limit": "^1.0",
32+
"illuminate/redis": "^9.5"
33+
},
34+
"suggest": {
35+
"ext-event": "For better performance. "
36+
},
37+
"autoload": {
38+
"psr-4": {
39+
"": "./",
40+
"App\\": "./app"
41+
},
42+
"files": [
43+
"./support/helpers.php"
44+
]
45+
},
46+
"scripts": {
47+
"post-package-install": [
48+
"support\\Plugin::install"
49+
],
50+
"post-package-update": [
51+
"support\\Plugin::install"
52+
],
53+
"pre-package-uninstall": [
54+
"support\\Plugin::uninstall"
55+
]
56+
}
57+
}

0 commit comments

Comments
 (0)