# 1. Diarkis サーバーをローカル環境で起動する

## **はじめに**

本ページでは [Diarkis サーバーテンプレート](/getting-started/diarkis-server-template.md) を利用して Diarkis サーバーをローカル環境で起動する手順を解説します。Diarkis での開発を始める前にこちらの手順を一通り試すことをおすすめいたします。

## **環境準備**

サーバー・テンプレートを導入するには Go 1.22 以降が必要です。

インストール方法はプラットフォーム毎に異なりますので、公式ドキュメントに従ってインストールします。 <https://go.dev/doc/install>

Diarkis のサーバー開発は、macOS, Linux, Windows で行うことができます。

Windows 環境でサーバーを起動する場合は [Diarkis サーバーを Windows 環境で起動する](/diarkis-server/setup-windows.md) をご覧ください。

## サーバー・テンプレートからプロジェクトを生成する

1. まずは任意の PATH で `git clone https://github.com/Diarkis/diarkis-server-template.git` として、repository をクローンします。
2. 以下のコマンドを実行して、プロジェクトを生成します。出力先は `output` に指定した絶対パスとなります。

{% code overflow="wrap" %}

```bash
make init project_id={project ID} builder_token={builder token} output={absolute path to install}

# 例
make init project_id=00000000000 builder_token=xxxx-yyyy-zzzz output=../server_bin
```

{% endcode %}

* `project_id`: 弊社が発行したプロジェクトID
* `builder_token`: 弊社が発行したBuilder Token
* `output`: 生成したプロジェクトの出力先。ここでは `../server_bin` として説明します

過去バージョンが必要な場合は、任意のバージョンの tag をチェックアウトするか、[リリース一覧](https://github.com/Diarkis/diarkis-server-template/releases)からダウンロードすることでご利用いただけます。

プロジェクト生成の詳細については、以下も参照してください。

🔗 <https://github.com/Diarkis/diarkis-server-template/blob/develop/README.md>

## セットアップ

生成したプロジェクトにて以下コマンドを実行します。

```bash
cd path/to/project
make init
```

これにより、開発に必要な各種リソース （コード補完を利用するためのリファレンス など）がダウンロードされます。

## サーバー・バイナリの生成

ローカル向けにバイナリを生成する際は、以下の make タスクを実行します。

```bash
make build-local
```

上記を実行すると、 `remote_bin` 配下に、diarkis のバイナリが生成されます。

```bash
% ls remote_bin
health-check http         mars         ms           tcp          testcli      udp
```

## サーバーの起動

MARS, HTTP, UDP の各サーバーを起動します。

```bash
make server target=mars
make server target=http
make server target=udp
# 本チュートリアルでは利用しませんが、TCP サーバーは以下コマンドで起動できます。
# make server target=tcp

# make コマンドを利用せずに以下のコマンドでも同様に起動可能です
./remote_bin/mars ./configs/mars/main.json
./remote_bin/http
./remote_bin/udp
```

## **接続情報の取得**

mars, http, udp が起動していれば、curl で以下のように接続情報を取得することができます。これでサーバーの起動は完了です。

{% code overflow="wrap" %}

```bash
% curl -X POST http://127.0.0.1:7000/endpoint/type/UDP/user/test
{"encryptionMacKey":"xxxxxxxxxx","serverType":"UDP","serverHost":"127.0.0.1","serverPort":7100,"sid":"xxxxxxxxxx","encryptionKey":"xxxxxxxxxx","encryptionIV":"xxxxxxxxxx"}%
```

{% endcode %}

上記の HTTP エンドポイントは UDP サーバーに対して uid `test` で認証したことを意味します。ここから返却された情報を各種クライアント・ライブラリから利用して、パケットのやり取りが可能となります。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.diarkis.io/getting-started/tutorial/setup-local-server.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
