概要
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 で記述します。
UDP サーバーがバインドするためのポート。UDP サーバーは、指定されたポートから始まる利用可能なポートを自動的に探します。
trueの場合、HTTP サーバーは指定されたポートでのみバインドされます
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)
}
JSON を使った HTTP エンドポイント
Diarkis の HTTP サーバーは、リクエストの ContentType が application/json
の時に、リクエストボディを自動的に req.JSONBody
にデコードします。 JSON Body に格納するためには、オブジェクトを記述する必要があることに注意してください。
コピー package httpcmds
import (
"github.com/Diarkis/diarkis/server/http"
)
func Expose(rootpath string) {
http.Post("/echo", handleEcho)
}
func handleEcho(res *http.Response, req *http.Request, params *http.Params, next func(error)) {
if req.JSONBody == nil {
err := errors.New("expect JSON body")
res.Respond(err.Error(), http.Bad)
next(err)
return
}
message, ok := req.JSONBody["message"].(string)
if !ok {
err := errors.New("missing parameter message")
res.Respond(err.Error(), http.Bad)
next(err)
return
}
enc, err := json.Marshal(map[string]any{"echo": message})
if err != nil {
res.Respond(err.Error(), http.Bad)
next(err)
return
}
res.SetHeader("Content-Type", "application/json")
res.SendBytes(enc, http.Ok)
// move on to other handlers
next(nil)
}