-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
159 lines (157 loc) · 5.75 KB
/
server.php
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
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/local/bin/php
<?php
require 'vendor/autoload.php';
include_once "task.php";
// 初始化配置
$config = include_once "conf/config.php";
$project = task::$project = include_once "conf/project.php";
task::$gitpath = realpath(__DIR__).'/git/';
task::$cos_secretId = $config['cos']['secretid'];
task::$cos_secretKey = $config['cos']['secretkey'];
task::$cos_bucket = $config['cos']['bucket'];
task::$othercos = $config['othercos'];
$server = new \Swoole\Http\Server("0.0.0.0", 9501, \SWOOLE_BASE);
$server->set([
'daemonize' => 0,
// 一般是cpu两倍,但很少会同时构建两个程序
'worker_num' => 2,
'task_worker_num' => 2,
'document_root' => realpath(__DIR__),
// 'enable_static_handler' => true,
'http_autoindex' => false,
// 'http_index_files' => ['index.html'],
// 'user' => 'www-data',
// 设置前端文件
]);
$server->on('Task', 'task::add');
$server->on('Finish', 'task::finish');
$server->on('Request', function ($request, $response) use ($server, $config, $project) {
$uri = $request->server['request_uri'];
$uri = trim($uri, '/');
if($uri == 'favicon.ico')
{
$response->write('ico');
return true;
}
if(!isset($request->header['authorization']))
{
$response->status('401', 'Unauthorized');
$response->header('WWW-Authenticate', 'Basic realm="My passport"');
return true;
}else{
$authorization = $request->header['authorization'];
$authorization = str_replace("Basic ", "" , $authorization);
$authorization = base64_decode($authorization);
$passport = explode(":", $authorization);
if($passport['0'] != $config['account']['user'] && $passport['1'] != $config['account']['pass'])
{
$response->status('401', 'Unauthorized');
$response->header('WWW-Authenticate', 'Basic realm="My passport"');
$response->end('认证失败');
return true;
}
}
$response->header('Content-Type', 'text/html; charset=utf-8');
$uridata = explode('/', $uri);
$action = $uridata['0'];
$id = $uridata['1'] ?? '';
if($action == 'build')
{
$tasks = [
'action' => $action,
'id' => $id,
];
$status = $server->task($tasks);
if($status)
{
$response->write('任务正在运行中');
}
}elseif($action == 'status'){
$statusfile = realpath(__DIR__)."/status/{$id}.txt";
$content = file_get_contents($statusfile);
$response->write("<pre>");
$response->write($content);
}elseif($action == ''){
$content = '
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="canonical" href="https://getbootstrap.com/docs/3.4/examples/justified-nav/">
<title>前端构建系统</title>
<link href="https://cdn.jsdelivr.net/npm/@bootcss/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@bootcss/[email protected]/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@bootcss/[email protected]/examples/justified-nav/justified-nav.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/@bootcss/[email protected]/assets/js/ie-emulation-modes-warning.js"></script>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="masthead">
<h3 class="text-muted">前端构建系统</h3>
<nav>
<ul class="nav nav-justified">
<li class="active"><a href="#">Home</a></li>
</ul>
</nav>
</div>
<div class="jumbotron">
<h1>front CI/CD</h1>
<p class="lead">frontshow,前端构建系统</p>
<p><a class="btn btn-lg btn-success" href="#" role="button">Get started!</a></p>
</div>
';
//闭合开关
$j = 0;
$i = 0;
foreach($project as $proid => $proj){
if($i % 3 == 0){
if($j == 1){
$content .= '</div>
<div class="row">';
$j = 0;
}else{
$content .= '<div class="row">';
$j = 1;
}
}
// 内容正文
$content .= '
<div class="col-lg-4">
<h2>'.$proj['projectname'].'</h2>
<p class="text-danger">'.$proj['url'].'</p>
<p>'.$proj['description'].'</p>
<p>
<a class="btn btn-primary" href="/build/'.$proid.'" role="button">构建 »</a>
<a class="btn btn-success" href="/status/'.$proid.'" role="button">详情 »</a>
</p>
</div>';
$i++;
}
if($j == 1){
$content .= '</div>';
}
$content .= '
<footer class="footer">
<p>© frontshow</p>
</footer>
</div> <!-- /container -->
</body>
</html>';
// $content = file_get_contents(realpath(__DIR__).'/index.html');
$response->write($content);
return true;
}
$response->write(time());
$response->write("<br/><a href='javascript:history.go(-1);'>返回</a><br/>");
// $response->end('<pre>Task Result: '.var_export($result, true)); });
});
echo "前端构建系统 is started at http://0.0.0.0:9501\n";
$server->start();