0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SOCKET-MANAGERを使ったREST-APIサーバー開発の手引き<新規でサーバーを作る場合(QUICK-START)>

0
Posted at

はじめに

このページでは、REST-API サーバーを 新規に構築するための最短手順 を紹介します。
ルーティング設定・メイン処理クラス・イベント処理クラス(イベントハンドラ型 / ステートマシン型)の 3 つを用意するだけで、REST-API サーバーを起動できます。

SOCKET-MANAGER Framework は PSR-7 準拠 の HTTP 処理と CUEI アーキテクチャ に基づく統一モデルを採用しており、 REST-API を 1 から構築する際もシンプルで一貫した開発体験を提供します。

まだ REST-API 開発環境を作成していない場合は、 composer create-project によるプロジェクト生成が必要です。
詳細は次のセクション「REST-API 開発環境の準備」を参照してください。

まずは動くサンプルから学びたい場合は、以下のページを参照してください。

REST-API 開発環境の準備

このページでは、REST-API サーバーを 最短手順で新規作成 する方法を紹介しますが、 その前に REST-API 開発環境そのものを用意する必要があります。

まだプロジェクトを作成していない場合は、次のコマンドで REST-API サーバー開発環境を生成してください。

REST-API 開発環境の生成

プロジェクト作成
composer create-project socket-manager/rest-api my-api

これにより、REST-API サーバーに必要なディレクトリ構成と設定ファイルが自動生成されます。

すでに環境を作成済みの場合は、このまま次のステップへ進んでください。

必要最小限のファイルを生成

ルーティング設定ファイルの生成

custom:setting-routing
php worker custom:setting-routing hello

生成されるファイル: setting/hello.php

メイン処理クラスの生成

custom:main
php worker custom:main Hello

生成されるファイル: MainClass/Hello.php

イベント処理クラスの生成(どちらか一方)

custom:event-handler
php worker custom:event-handler HelloEventhandler

生成されるファイル: EventClass/HelloEventhandler.php

custom:state-machine
php worker custom:state-machine HelloStateMachine

生成されるファイル: EventClass/HelloStateMachine.php

最小ルーティング設定を書く

setting/hello.php の内容

setting/hello.php(追記例)
return [
    'routes' => [
        ['GET', '/hello', 'onRequest'], // ←追加分
    ],
    ...既存のコード
];

イベント処理クラスの最小実装(比較)

イベントハンドラ型(最小)

HelloEventhandler.php
<?php

class HelloEventhandler extends CommandForHandler
{
    protected function onRequest($ctx)
    {
        $ctx->json(['message' => 'hello']);
    }
}

ステートマシン型(最小)

HelloStateMachine.php
<?php

class HelloStateMachine extends CommandForStateMachine
{
    protected function onStart($ctx)
    {
        return [
            [
                'status' => 'start',
                'unit' => function($ctx): ?string
                {
                    $ctx->json(['message' => 'hello']);
                    return null;
                }
            ]
        ];
    }
}

イベント処理クラス名の登録

使用するイベント処理クラス名をメイン処理クラスへ登録します。

イベントハンドラ型の場合

MainClass.php(event-handler)
protected array $classes = [
    'context'  => null,
    'event'    => HelloEventhandler::class,
    'parallel' => null
];

ステートマシン型の場合

MainClass.php(state-machine)
protected array $classes = [
    'context'  => null,
    'event'    => HelloStateMachine::class,
    'parallel' => null
];

サーバーを起動する

メイン処理クラス生成時の Hello の名称が identifier にもセットされます。

identifer
protected string $identifer = 'app:Hello {port?} {keep_alive?}';

そのため、次のコマンドで起動できます。

サーバー起動
php worker app:Hello

動作確認

ブラウザまたは curl で確認

curl
curl http://localhost:8080/hello
レスポンス
{"message":"hello"}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?