Skip to content

Commit

Permalink
Basics
Browse files Browse the repository at this point in the history
  • Loading branch information
kyour-cn committed Jan 8, 2021
1 parent 7b887d6 commit 836f7b0
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 69 deletions.
8 changes: 3 additions & 5 deletions App/HttpController/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,21 @@ public function input(string $type = '')
}

//获取Session对象
public function session(string $key, $data = 'SWORD_GET')
public function session(string $key, $data = SWORD_NULL)
{
$session = Session::getInstance();

if($data == 'SWORD_GET'){
if($data === SWORD_NULL){
$d = $session->get($key);
if($d){
return $d;
// return unserialize($d);
}else{
return null;
}
}elseif($data == null){
$session->del($key);
}else{
$session->set($key, $data);
// $session->set($key,serialize($data));
}

}
Expand Down Expand Up @@ -130,7 +128,7 @@ public function withData(int $code = 0, string $msg = '', $result = [], int $cou
if($count >= 0) $ret['count'] = $count;

$this->response()->withHeader('Content-type','application/json;charset=utf-8');
$this->response()->withHeader('Access-Control-Allow-Origin','*'); //*.kyour.cn
$this->response()->withHeader('Access-Control-Allow-Origin','*');
$this->write(json_encode($ret));

return true;
Expand Down
23 changes: 17 additions & 6 deletions App/HttpController/Index/Index.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
<?php declare(strict_types=1);
namespace App\HttpController\Index;

// use App\Common\Utils;
use App\Model\Identity;
use EasySwoole\Validate\Validate;

class Index extends BaseAuth
use App\HttpController\BaseController;

class Index extends BaseController
{
public $authRule = [];

//项目首页
public function index()
{
//直接输出
$this->response()->write('hello php-sword!');
}

public function jsonData()
{
//读取静态文件返回
$this->withData(0, 'hello world!', [
'tip' => 'json data'
]);
}

public function html()
{
//读取静态文件返回
$this->fetch('index', [], 'raw');
Expand Down
16 changes: 1 addition & 15 deletions App/WebSocket/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@

use EasySwoole\Socket\AbstractInterface\Controller;

use EasySwoole\Session\Session;

use App\WebSocket\Base;

use App\Model\User as UserModel;

/**
* Class Index
*
Expand All @@ -25,15 +19,7 @@ public function heart()
$param = $this->caller()->getArgs();
$base = new Base($param, $this->response());

$s_user = $base->session('user');
//判断是否在别处登录
if($s_user){
$token = UserModel::create()->where('id', $s_user['id'])->val('token');
if($token != $base->sessionId()){
//清空session
$base->session('user', null);
}
}
$base->withData(0, 'success',['data' => 'hello world!']);
}

}
10 changes: 3 additions & 7 deletions App/WebSocket/Index/Index.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?php
namespace App\WebSocket\Index;

use App\Model\Article;
use App\Model\UserFollow;
use EasySwoole\Socket\AbstractInterface\Controller;

use App\WebSocket\Base;
use EasySwoole\Mysqli\QueryBuilder;

use App\Model\User as UserModel;

/**
* 前端帖子相关
* Class User
* @package App\WebSocket\Index
*/
class User extends Controller
{
Expand All @@ -21,7 +17,7 @@ public function getInfo()
$args = $this->caller()->getArgs();
$base = new Base($args, $this->response());

$base->withData(0, 'success',['data' => 'hello wlord!']);
$base->withData(0, 'success',['data' => 'hello world!']);
}

}
22 changes: 22 additions & 0 deletions Config/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* 应用的配置信息
* 通过config('app.xx')获取
*/
return [
// 系统时区
'timezone' => 'Asia/Shanghai',
// 启用调试模式
'debug' => true,

//===========其他配置===========

// 应用名 -保证唯一性,避免在同一机器上运行相同的应用
'app_name' => 'sword',

// 服务器域名
'host' => 'youhost.cn',
// 资源服务器域名
'res_host' => 'res.youhost.cn'

];
14 changes: 14 additions & 0 deletions Config/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* 数据库连接配置
* 通过config('database.xx')获取
*/
return [
'host' => '127.0.0.1', //服务器地址
'port' => 3306, //端口
'user' => 'user', //用户名
'password' => 'pass', //密码
'charset' => 'utf8', //编码
'name' => 'dbname', //数据库名

];
10 changes: 10 additions & 0 deletions Config/redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* redis连接配置
*/
return [
'host' => '127.0.0.1', //服务器地址
'port' => 6379, //端口
'password' => '', //密码
'db' => 0 //默认数据库 0-12
];
26 changes: 13 additions & 13 deletions EasySwooleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ public static function initialize()

// -------------------- HTTP --------------------
//onRequest事件
Di::getInstance()->set(SysConst::HTTP_GLOBAL_ON_REQUEST,function (Request $request,Response $response){

//启用Session会话
$session_conf = config('session');
$cookie = $request->getCookieParams($session_conf['sessionName']);
if(empty($cookie)){
$sid = Session::getInstance()->sessionId();
$response->setCookie($session_conf['sessionName'], $sid, time() + $session_conf['expire']);
}else{
Session::getInstance()->sessionId($cookie);
}
return true;
});
// Di::getInstance()->set(SysConst::HTTP_GLOBAL_ON_REQUEST,function (Request $request,Response $response){
//
// //启用Session会话
// $session_conf = config('session');
// $cookie = $request->getCookieParams($session_conf['sessionName']);
// if(empty($cookie)){
// $sid = Session::getInstance()->sessionId();
// $response->setCookie($session_conf['sessionName'], $sid, time() + $session_conf['expire']);
// }else{
// Session::getInstance()->sessionId($cookie);
// }
// return true;
// });

// 注册异常处理
Di::getInstance()->set(SysConst::HTTP_EXCEPTION_HANDLER, [ExceptionHandler::class,'handle']);
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* 支持更多的`PSR`规范
* 基于Swoole常驻内存
* 协程TCP、UDP、WEB_SOCKET 服务端
* 严格的版本控制

## 安装
安装Composer,请先确保正确安装php并配置了环境变量。
Expand Down
13 changes: 4 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
{
"name": "php-sword/sword",
"description": "The phpSword Framework.",
"description": "The PhpSword Framework.",
"type": "project",
"keywords": [
"php",
"swoole",
"easyswoole",
"sword"
],
"keywords": ["php", "swoole", "easyswoole", "sword"],
"license": "Apache-2.0",
"authors": [
{
Expand All @@ -16,7 +11,7 @@
}
],
"require": {
"easyswoole/easyswoole": "^3.4",
"php-sword/framework": "^0.0",
"ext-redis": "*"
},
"autoload": {
Expand All @@ -25,4 +20,4 @@
"Sword\\": "Sword-Dev/"
}
}
}
}
16 changes: 11 additions & 5 deletions dev.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
<?php
/**
* EasySwoole框架配置信息
*
*/
return [
'SERVER_NAME' => "EasySwoole",
'MAIN_SERVER' => [
'LISTEN_ADDRESS' => '0.0.0.0',
'PORT' => 8105,
'SERVER_TYPE' => EASYSWOOLE_WEB_SOCKET_SERVER, //可选为 EASYSWOOLE_SERVER EASYSWOOLE_WEB_SERVER EASYSWOOLE_WEB_SOCKET_SERVER,EASYSWOOLE_REDIS_SERVER
'PORT' => 8100,
'SERVER_TYPE' => EASYSWOOLE_WEB_SERVER, //可选为 EASYSWOOLE_SERVER EASYSWOOLE_WEB_SERVER EASYSWOOLE_WEB_SOCKET_SERVER,EASYSWOOLE_REDIS_SERVER
'SOCK_TYPE' => SWOOLE_TCP,
'RUN_MODEL' => SWOOLE_PROCESS,

// Swoole配置信息
'SETTING' => [
'worker_num' => 8,
'reload_async' => true,
'max_wait_time'=> 3,

//兼容文件上传最大值
'package_max_length' => 20 * 1024 * 1024
// 兼容文件上传最大值
// 'package_max_length' => 20 * 1024 * 1024
],
'TASK' => [
'workerNum' => 4,
'maxRunningNum' => 128,
'timeout' => 15
]
],
'TEMP_DIR' => null,
'TEMP_DIR' => './Temp',
'LOG_DIR' => './Temp/Log'
];
14 changes: 7 additions & 7 deletions nginx_make.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<?php
/**
* 因为nginx配置代理、ssl、websocket较为复杂 所以编写了这个工具。
* 用法:1.在$config变量中配置你的信息
* 用法: 1.在$config变量中配置你的信息
* 2.在该目录运行命令 ./nginx_make.php
* 3.在该目录会自动生成 nginx.conf,这个文件就是配置文件,将它添加到nginx中
* 4.重启nginx服务器即可
Expand All @@ -18,19 +18,19 @@
//https端口 第一个是外网端口,第二个是内网服务端口
'https_port' => [443, 8105],
//外网访问域名 多个用空格分割
'host_name' => 'www.xoshe.cn xoshe.cn xo.kyour.cn',
'host_name' => 'sword.kyour.cn',
//应用根目录 绝对路径,以/结尾
'root_path' => '/data/www/xoshe/',
'root_path' => __DIR__, //默认当前路径
//静态资源目录名称
'public_path' => 'Public',

//websocket的url,为空则不开启,不支持填写'/'
'ws_url' => '/ws',
//websocket的url,如:'/ws',为空则不开启,不支持填写'/'
'ws_url' => '',

//https的ssl证书文件(绝对路径) -仅开启https有效
'ssl_cer' => '/data/www/xoshe/ssl/fullchain.cer',
'ssl_cer' => '/data/www/sword/ssl/fullchain.cer',
//https的ssl密钥文件(绝对路径) -仅开启https有效
'ssl_key' => '/data/www/xoshe/ssl/mk.kyour.cn.key',
'ssl_key' => '/data/www/sword/ssl/ssl.key',

//图片缓存时间
'img_cache' => '3d',
Expand Down
35 changes: 33 additions & 2 deletions sword
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
#!/usr/bin/env php
<?php

/**
* Easyswoole命令行
* 启动服务:./sword server start
* 常驻内存:./sword server start -d
* 停止服务:./sword server stop
*/
require_once "./vendor/bin/easyswoole";

use EasySwoole\EasySwoole\Command\CommandRunner;
use EasySwoole\Command\Caller;

$file = __DIR__ . '/vendor/autoload.php';

if (file_exists($file)) {
require $file;
}else{
die("include composer autoload.php fail\n Please run: composer install\n");
}

$realCwd = substr(realpath($file),0,-strlen("/vendor/autoload.php"));

defined('IN_PHAR') or define('IN_PHAR', boolval(\Phar::running(false)));
defined('RUNNING_ROOT') or define('RUNNING_ROOT', $realCwd);
defined('EASYSWOOLE_ROOT') or define('EASYSWOOLE_ROOT', IN_PHAR ? \Phar::running() : $realCwd);

if(file_exists(EASYSWOOLE_ROOT.'/bootstrap.php')){
require_once EASYSWOOLE_ROOT.'/bootstrap.php';
}

$caller = new Caller();
$caller->setScript(current($argv));
$caller->setCommand(next($argv));
$caller->setParams($argv);
reset($argv);

$ret = CommandRunner::getInstance()->run($caller);
if($ret && !empty($ret->getMsg())){
echo $ret->getMsg()."\n";
}

0 comments on commit 836f7b0

Please sign in to comment.