Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.x #41

Merged
merged 4 commits into from
Sep 10, 2022
Merged

1.x #41

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# usermod -aG docker gitlab-runner

stages:
- build
- deploy

variables:
PROJECT_NAME: maxphp
REGISTRY_URL: registry-docker.org

build_test_docker:
stage: build
before_script:
# - git submodule sync --recursive
# - git submodule update --init --recursive
script:
- docker build . -t $PROJECT_NAME
- docker tag $PROJECT_NAME $REGISTRY_URL/$PROJECT_NAME:test
- docker push $REGISTRY_URL/$PROJECT_NAME:test
only:
- test
tags:
- builder

deploy_test_docker:
stage: deploy
script:
- docker stack deploy -c deploy.test.yml --with-registry-auth $PROJECT_NAME
only:
- test
tags:
- test

build_docker:
stage: build
before_script:
# - git submodule sync --recursive
# - git submodule update --init --recursive
script:
- docker build . -t $PROJECT_NAME
- docker tag $PROJECT_NAME $REGISTRY_URL/$PROJECT_NAME:$CI_COMMIT_REF_NAME
- docker tag $PROJECT_NAME $REGISTRY_URL/$PROJECT_NAME:latest
- docker push $REGISTRY_URL/$PROJECT_NAME:$CI_COMMIT_REF_NAME
- docker push $REGISTRY_URL/$PROJECT_NAME:latest
only:
- tags
tags:
- builder

deploy_docker:
stage: deploy
script:
- echo SUCCESS
only:
- tags
tags:
- builder
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM php:latest
LABEL maintainer="ChengYao" version="1.0" license="Apache-2.0" app.name="MaxPHP"

##
# ---------- env settings ----------
##
# --build-arg timezone=Asia/Shanghai
ARG timezone

ENV TIMEZONE=${timezone:-"Asia/Shanghai"}

# update
RUN set -ex \
# show php version and extensions
&& php -v \
&& php -m \
&& pecl install swoole \
&& echo -e "\033[42;37m Build Completed :).\033[0m\n"

WORKDIR /www

# Composer Cache
# COPY ./composer.* /opt/www/
# RUN composer install --no-dev --no-scripts

COPY . /www
RUN composer install --no-dev -o && php bin/swoole.php

EXPOSE 8989

ENTRYPOINT ["php", "/www/bin/swoole.php"]
34 changes: 17 additions & 17 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ class Kernel extends HttpKernel
* Web middlewares.
*/
protected array $webMiddlewares = [
\Max\Http\Server\Middleware\SessionMiddleware::class,
// \Max\Http\Server\Middleware\SessionMiddleware::class,
\App\Http\Middleware\VerifyCSRFToken::class,
];

/**
* Api middlewares.
*/
protected array $apiMiddlewares = [
\App\Http\Middleware\AllowCrossDomain::class,
\App\Http\Middleware\ParseBodyMiddleware::class,
// \App\Http\Middleware\AllowCrossDomain::class,
// \App\Http\Middleware\ParseBodyMiddleware::class,
];

/**
Expand All @@ -47,20 +47,20 @@ class Kernel extends HttpKernel
protected function map(Router $router): void
{
$router->middleware(...$this->webMiddlewares)
->group(function (Router $router) {
$router->request('/', [\App\Http\Controller\IndexController::class, 'index']);
});
->group(function(Router $router) {
$router->request('/', [\App\Http\Controller\IndexController::class, 'index']);
});
$router->middleware(...$this->apiMiddlewares)
->prefix('api')
->group(function (Router $router) {
$router->get('/', function (ServerRequestInterface $request) {
return Response::JSON([
'statue' => true,
'code' => 0,
'message' => sprintf('Hello, %s.', $request->query('name', 'world')),
'data' => [],
]);
});
});
->prefix('api')
->group(function(Router $router) {
$router->get('/', function(ServerRequestInterface $request) {
return Response::JSON([
'statue' => true,
'code' => 0,
'message' => sprintf('Hello, %s.', $request->query('name', 'world')),
'data' => [],
]);
});
});
}
}
3 changes: 1 addition & 2 deletions app/Http/Middleware/DownloadCacheMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
if ($modifiedSince = $request->getHeaderLine('if-modified-since')) {
$body = $response->getBody();
if ($body instanceof FileStream) {
$path = $body->getMetadata('uri');
$lastModified = date('D, d M Y H:i:s', filemtime($path)) . ' ' . date_default_timezone_get();
$lastModified = date('D, d M Y H:i:s', filemtime($body->getFilename())) . ' ' . date_default_timezone_get();
if ($lastModified === $modifiedSince) {
return new Response(304);
}
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Middleware/ExceptionHandleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace App\Http\Middleware;

use App\Http\Response;
use ErrorException;
use Max\Exception\Handler\WhoopsExceptionHandler;
use Max\Http\Message\Exception\HttpException;
use Max\Http\Server\Middleware\ExceptionHandleMiddleware as Middleware;
Expand All @@ -34,10 +33,10 @@ public function __construct(

protected function render(Throwable $throwable, ServerRequestInterface $request): ResponseInterface
{
$code = $this->getStatusCode($throwable);
if ($throwable instanceof Abort) {
return Response::HTML($this->convertToHtml($throwable));
}
$code = $this->getStatusCode($throwable);
if (env('APP_DEBUG')) {
$response = (new WhoopsExceptionHandler())->handle($throwable, $request);
if ($throwable instanceof HttpException) {
Expand Down
17 changes: 9 additions & 8 deletions app/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
namespace App\Http;

use Max\Http\Message\Contract\HeaderInterface;
use Max\Http\Message\Stream\StandardStream;
use Max\Http\Message\Stream\FileStream;
use Max\Http\Server\Response as PsrResponse;
use Max\View\ViewFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;

class Response extends PsrResponse
{
Expand All @@ -44,16 +43,18 @@ public static function view(string $view, array $arguments = [], ?ServerRequestI
/**
* Create a file download response.
*
* @param string|StreamInterface|resource $file
* @param string $name 文件名(留空则自动生成文件名)
* @param array $headers
* @param string $file
* @param string $name 文件名(留空则自动生成文件名)
* @param int $offset 偏移量
* @param int $length 长度
*
* @return ResponseInterface
*/
public static function download($file, string $name, array $headers = []): ResponseInterface
public static function download(string $file, string $name = '', int $offset = 0, int $length = 0): ResponseInterface
{
$name = $name ?: pathinfo($file, PATHINFO_BASENAME);
$headers = static::DEFAULT_DOWNLOAD_HEADERS;
$headers[HeaderInterface::HEADER_CONTENT_DISPOSITION] = sprintf('attachment;filename="%s"', htmlspecialchars($name, ENT_COMPAT));
$headers = array_merge(static::DEFAULT_DOWNLOAD_HEADERS, $headers);
return new static(200, $headers, StandardStream::create($file));
return new static(200, $headers, new FileStream($file, $offset, $length));
}
}
30 changes: 30 additions & 0 deletions deploy.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3.7'
services:
maxphp:
image: $REGISTRY_URL/$PROJECT_NAME:test
environment:
- "APP_PROJECT=maxphp"
- "APP_ENV=test"
ports:
- 8989:8989
deploy:
replicas: 1
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 5
update_config:
parallelism: 2
delay: 5s
order: start-first
networks:
- maxphp_net
configs:
- source: maxphp_1.0
target: /www/.env
configs:
maxphp_1.0:
external: true
networks:
maxphp_net:
external: true