HTTP サーバー

概要

HTTP サーバーは Diarkis のエントリーポイントとなります。アプリケーションサーバーは、HTTP サーバーに接続して、リアルタイム接続のエンドポイントと暗号化キーを取得します。

また、カスタム・エンドポイントを記述することも可能です。

HTTP サーバーのセットアップ

クライアントにビルトイン・コマンドを公開するには、diarkisexec パッケージを利用してセットアップできます。

  • diarkisexec.SetupDiarkis() でモジュールを指定してビルトインコマンドを公開できます。各モジュールの ConfigPath を指定することで、設定をカスタマイズできます。

  • diarkisexec.SetServerCommandHandler() でカスタムコマンドを公開できます。

  • diarkisexec.SetupDiarkisHTTPServer() で UDP サーバーのセットアップができます。引数の JSON ファイルでサーバーの設定をカスタマイズできます。

⚠️ 上記の関数は、 diarkisexec.StartDiarkis() を呼ぶ前に実行する必要があります。

詳細は diarkisexec の API リファレンスを参照して下さい。

package main

import (
	"github.com/Diarkis/diarkis/diarkisexec"

	"github.com/Diarkis/diarkis-server-template/cmds"
)

func main() {
	logConfigPath := "/configs/shared/log.json"
	meshConfigPath := "/configs/shared/mesh.json"

	diarkisexec.SetupDiarkis(logConfigPath, meshConfigPath, &diarkisexec.Modules{
		Dive:       &diarkisexec.Options{ConfigPath: "/configs/shared/dive.json", ExposeCommands: true},
		Field:      &diarkisexec.Options{ConfigPath: "/configs/shared/field.json", ExposeCommands: true},
		DM:         &diarkisexec.Options{ConfigPath: "/configs/shared/dm.json", ExposeCommands: true},
		MatchMaker: &diarkisexec.Options{ConfigPath: "/configs/shared/matching.json", ExposeCommands: true},
	})

	httpcmds.SetupHTTP()

	diarkisexec.SetupDiarkisHTTPServer("/configs/http/main.json")

	diarkisexec.StartDiarkis()
}

Mesh 設定

meshConfigPath には JSON ファイルのパスを指定します。詳細は Mesh 設定 を参照してください。

HTTP サーバーの設定

設定は JSON で記述します。 

キーデフォルト

address

"127.0.0.1"

バインドする UDP サーバーのアドレス

port

"7000"

UDP サーバーがバインドするためのポート。UDP サーバーは、指定されたポートから始まる利用可能なポートを自動的に探します。

useFixedPort

false

trueの場合、HTTP サーバーは指定されたポートでのみバインドされます

timeout

5

HTTP レスポンス・タイムアウト(秒)

enableEncryption

true

falseに設定すると、パケットの暗号化と復号化が無効になります。他のサーバーも同様の設定をする必要があります。

詳細は http の API リファレンス を参照して下さい。

カスタム HTTP エンドポイントの作成方法

DiarkisのHTTP サーバーでは、カスタムエンドポイントを書くことができます。

package httpcmds

import (
      "github.com/Diarkis/diarkis/server/http"
)

func Expose(rootpath string) {
      // :message is treated as a parameter and the value can be accessed from *http.Params

      http.Get("/hello/:message", handleHello)
}

func handleHello(res *http.Response, req *http.Request, params *http.Params, next func(error)) {
      message := params.GetAsString("message")
      res.Respond(message, http.Ok)
      // move on to other handlers
      next(nil)
}

最終更新