Skip to content

Commit 0e45641

Browse files
authored
Add files via upload
1 parent 8d751c9 commit 0e45641

90 files changed

Lines changed: 15179 additions & 2 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 223 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,223 @@
1-
# BreadNote
2-
BreadNote是一款在线轻量笔记编辑平台,在浏览器访问即可使用不需要本地安装,方便快捷。
1+
# API development skeleton
2+
3+
帮助你快速搭建 API 项目骨架,并指导你如何使用该骨架的细节,骨架默认开启了 SQL、Redis 日志,压测前请先关闭 `.env``APP_DEBUG`
4+
5+
## 安装
6+
7+
> 需要先安装 [Swoole](https://wiki.swoole.com/#/environment) 或者 [WorkerMan](http://doc.workerman.net/install/requirement.html)
8+
9+
```
10+
composer create-project --prefer-dist mix/api-skeleton api
11+
```
12+
13+
## 快速开始
14+
15+
启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务 (零依赖)
16+
17+
```
18+
composer run-script --timeout=0 cliserver:start
19+
```
20+
21+
启动 Swoole 多进程服务
22+
23+
```
24+
composer run-script --timeout=0 swoole:start
25+
```
26+
27+
启动 Swoole 协程服务
28+
29+
```
30+
composer run-script --timeout=0 swooleco:start
31+
```
32+
33+
启动 WorkerMan 多进程服务
34+
35+
```
36+
composer run-script --timeout=0 workerman:start
37+
```
38+
39+
## 执行脚本
40+
41+
- `composer run-script` 命令中的 `--timeout=0` 参数是防止 composer [执行超时](https://getcomposer.org/doc/06-config.md#process-timeout)
42+
- `composer.json` 定义了命令执行脚本,对应上面的执行命令
43+
44+
```json
45+
"scripts": {
46+
"cliserver:start": "php -S localhost:8000 public/index.php",
47+
"swoole:start": "php bin/swoole.php",
48+
"swooleco:start": "php bin/swooleco.php",
49+
"workerman:start": "php bin/workerman.php start",
50+
"cli:clearcache": "php bin/cli.php clearcache"
51+
}
52+
```
53+
54+
当然也可以直接下面这样启动,效果是一样的,但是 `scripts` 能帮你记录到底有哪些可用的命令,同时在IDE中调试更加方便。
55+
56+
```
57+
php bin/swoole.php start
58+
```
59+
60+
## 编写一个 API 接口
61+
62+
首先修改根目录 `.env` 文件的数据库信息
63+
64+
然后在 `routes/index.php` 定义一个新的路由
65+
66+
```php
67+
$vega->handle('/users/{id}', [new Users(), 'index'])->methods('GET');
68+
```
69+
70+
路由里使用了 `Users` 控制器,我们需要创建他
71+
72+
- 如何配置路由:[mix/vega](https://github.com/mix-php/vega#readme)
73+
- 如何调用数据库:[mix/database](https://github.com/mix-php/database#readme)
74+
75+
```php
76+
<?php
77+
78+
namespace App\Controller;
79+
80+
use App\Container\DB;
81+
use Mix\Vega\Context;
82+
83+
class Users
84+
{
85+
86+
/**
87+
* @param Context $ctx
88+
* @throws \Exception
89+
*/
90+
public function index(Context $ctx)
91+
{
92+
$row = DB::instance()->table('users')->where('id = ?', $ctx->param('id'))->first();
93+
if (!$row) {
94+
throw new \Exception('User not found');
95+
}
96+
$ctx->JSON(200, [
97+
'code' => 0,
98+
'message' => 'ok',
99+
'data' => $row
100+
]);
101+
}
102+
103+
}
104+
```
105+
106+
重新启动服务器后方可测试新开发的接口
107+
108+
> 实际开发中使用 PhpStorm 的 Run 功能,只需要点击一下重启按钮即可
109+
110+
```
111+
// 查找进程 PID
112+
ps -ef | grep swoole
113+
114+
// 通过 PID 停止进程
115+
kill PID
116+
117+
// 重新启动进程
118+
composer run-script swoole:start
119+
120+
// curl 测试
121+
curl http://127.0.0.1:9501/users/1
122+
```
123+
124+
## 使用容器中的对象
125+
126+
容器采用了一个简单的单例模式,你可以修改为更加适合自己的方式。
127+
128+
- 数据库:[mix/database](https://github.com/mix-php/database#readme)
129+
130+
```
131+
DB::instance()
132+
```
133+
134+
- Redis:[mix/redis](https://github.com/mix-php/redis#readme)
135+
136+
```
137+
RDS::instance()
138+
```
139+
140+
- 日志:[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html)
141+
142+
```
143+
Logger::instance()
144+
```
145+
146+
- 配置:[hassankhan/config](https://github.com/hassankhan/config#getting-values)
147+
148+
```
149+
Config::instance()
150+
```
151+
152+
## 部署
153+
154+
- CLI
155+
156+
线上部署启动时,修改 `shell/server.sh` 脚本中的绝对路径和参数
157+
158+
```
159+
php=/usr/local/bin/php
160+
file=/project/bin/swoole.php
161+
cmd=start
162+
numprocs=1
163+
```
164+
165+
启动管理
166+
167+
```
168+
sh shell/server.sh start
169+
sh shell/server.sh stop
170+
sh shell/server.sh restart
171+
```
172+
173+
使用 `nginx` 或者 `SLB` 代理到服务器端口即可
174+
175+
```
176+
server {
177+
server_name www.domain.com;
178+
listen 80;
179+
180+
location / {
181+
proxy_http_version 1.1;
182+
proxy_set_header Connection "keep-alive";
183+
proxy_set_header Host $http_host;
184+
proxy_set_header Scheme $scheme;
185+
proxy_set_header X-Real-IP $remote_addr;
186+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
187+
if (!-f $request_filename) {
188+
proxy_pass http://127.0.0.1:9501;
189+
}
190+
}
191+
}
192+
```
193+
194+
- PHP-FPM
195+
196+
和 Laravel、ThinkPHP 部署方法完全一致,将 `public/index.php``nginx` 配置 `rewrite` 重写即可
197+
198+
```
199+
server {
200+
server_name www.domain.com;
201+
listen 80;
202+
root /data/project/public;
203+
index index.html index.php;
204+
205+
location / {
206+
if (!-e $request_filename) {
207+
rewrite ^/(.*)$ /index.php/$1 last;
208+
}
209+
}
210+
211+
location ~ ^(.+\.php)(.*)$ {
212+
fastcgi_pass 127.0.0.1:9000;
213+
fastcgi_split_path_info ^(.+\.php)(.*)$;
214+
fastcgi_param PATH_INFO $fastcgi_path_info;
215+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
216+
include fastcgi_params;
217+
}
218+
}
219+
```
220+
221+
## License
222+
223+
Apache License Version 2.0, http://www.apache.org/licenses/

bin/cli.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env php
2+
<?php
3+
ini_set('display_errors', 'on');
4+
ini_set('display_startup_errors', 'on');
5+
ini_set('error_reporting', E_ALL ^ E_NOTICE);
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
use App\Error;
10+
use Dotenv\Dotenv;
11+
use Mix\Cli\Cli;
12+
13+
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
14+
define("APP_DEBUG", env('APP_DEBUG'));
15+
16+
Error::register();
17+
18+
Cli::setName('app')->setVersion('0.0.0-alpha');
19+
$cmds = [
20+
new Mix\Cli\Command([
21+
'name' => 'clearcache',
22+
'short' => 'Clear cache',
23+
'options' => [
24+
new Mix\Cli\Option([
25+
'names' => ['k', 'key'],
26+
'usage' => 'Key name'
27+
]),
28+
],
29+
'run' => new App\Command\ClearCache(),
30+
])
31+
];
32+
Cli::addCommand(...$cmds)->run();

bin/swoole.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env php
2+
<?php
3+
ini_set('display_errors', 'on');
4+
ini_set('display_startup_errors', 'on');
5+
ini_set('error_reporting', E_ALL ^ E_NOTICE);
6+
ini_set('memory_limit', '1G');
7+
8+
require __DIR__ . '/../vendor/autoload.php';
9+
10+
use App\Error;
11+
use App\Container\Logger;
12+
use App\Vega;
13+
use Dotenv\Dotenv;
14+
use Mix\Init\StaticInit;
15+
16+
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
17+
define("APP_DEBUG", env('APP_DEBUG'));
18+
19+
Error::register();
20+
21+
/**
22+
* 多进程默认开启了协程
23+
* 关闭协程只需关闭 `enable_coroutine` 配置并注释数据库的 `::enableCoroutine()` 即可退化为多进程同步模式
24+
*/
25+
26+
$vega = Vega::new();
27+
$host = '0.0.0.0';
28+
$port = 9501;
29+
$http = new Swoole\Http\Server($host, $port);
30+
$http->on('Request', $vega->handler());
31+
$http->on('WorkerStart', function ($server, $workerId) {
32+
// swoole 协程不支持 set_exception_handler 需要手动捕获异常
33+
try {
34+
StaticInit::finder(__DIR__ . '/../src/Container')->exec('init');
35+
App\Container\DB::enableCoroutine();
36+
App\Container\RDS::enableCoroutine();
37+
} catch (\Throwable $ex) {
38+
Error::handle($ex);
39+
}
40+
});
41+
$http->set([
42+
'enable_coroutine' => true,
43+
'worker_num' => 4,
44+
]);
45+
46+
echo <<<EOL
47+
____
48+
______ ___ _____ ___ _____ / /_ _____
49+
/ __ `__ \/ /\ \/ /__ / __ \/ __ \/ __ \
50+
/ / / / / / / /\ \/ _ / /_/ / / / / /_/ /
51+
/_/ /_/ /_/_/ /_/\_\ / .___/_/ /_/ .___/
52+
/_/ /_/
53+
54+
55+
EOL;
56+
printf("System Name: %s\n", strtolower(PHP_OS));
57+
printf("PHP Version: %s\n", PHP_VERSION);
58+
printf("Swoole Version: %s\n", swoole_version());
59+
printf("Listen Addr: http://%s:%d\n", $host, $port);
60+
Logger::instance()->info('Start swoole server');
61+
62+
$http->start();

bin/swooleco.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env php
2+
<?php
3+
ini_set('display_errors', 'on');
4+
ini_set('display_startup_errors', 'on');
5+
ini_set('error_reporting', E_ALL ^ E_NOTICE);
6+
ini_set('memory_limit', '1G');
7+
8+
require __DIR__ . '/../vendor/autoload.php';
9+
10+
use App\Error;
11+
use App\Container\Logger;
12+
use App\Vega;
13+
use Dotenv\Dotenv;
14+
use Mix\Init\StaticInit;
15+
16+
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
17+
define("APP_DEBUG", env('APP_DEBUG'));
18+
19+
Error::register();
20+
21+
Swoole\Coroutine\run(function () {
22+
StaticInit::finder(__DIR__ . '/../src/Container')->exec('init');
23+
App\Container\DB::enableCoroutine();
24+
App\Container\RDS::enableCoroutine();
25+
26+
$vega = Vega::new();
27+
$host = '0.0.0.0';
28+
$port = 9502;
29+
$server = new Swoole\Coroutine\Http\Server($host, $port, false, false);
30+
$server->handle('/', $vega->handler());
31+
32+
foreach ([SIGHUP, SIGINT, SIGTERM] as $signal) {
33+
Swoole\Process::signal($signal, function () use ($server) {
34+
Logger::instance()->info('Shutdown swoole coroutine server');
35+
$server->shutdown();
36+
App\Container\Shutdown::trigger();
37+
});
38+
}
39+
40+
echo <<<EOL
41+
____
42+
______ ___ _____ ___ _____ / /_ _____
43+
/ __ `__ \/ /\ \/ /__ / __ \/ __ \/ __ \
44+
/ / / / / / / /\ \/ _ / /_/ / / / / /_/ /
45+
/_/ /_/ /_/_/ /_/\_\ / .___/_/ /_/ .___/
46+
/_/ /_/
47+
48+
49+
EOL;
50+
printf("System Name: %s\n", strtolower(PHP_OS));
51+
printf("PHP Version: %s\n", PHP_VERSION);
52+
printf("Swoole Version: %s\n", swoole_version());
53+
printf("Listen Addr: http://%s:%d\n", $host, $port);
54+
Logger::instance()->info('Start swoole coroutine server');
55+
56+
$server->start();
57+
});

0 commit comments

Comments
 (0)