HTTP Server

Overview

The HTTP server serves as the entry point for Diarkis. The application server connects to the HTTP server to obtain the endpoint for real-time connections and the encryption keys.

Additionally, it is possible to define custom endpoints.

Setting Up the HTTP Server

To expose built-in commands to the client, you can use the diarkisexec package for setup.

  • You can expose built-in commands by specifying the module through diarkisexec.SetupDiarkis(). By specifying ConfigPath for each module, you can customize the settings.

  • You can expose custom commands using diarkisexec.SetServerCommandHandler().

  • You can set up a UDP server using diarkisexec.SetupDiarkisHTTPServer(). You can customize server settings using a JSON file as an argument.

For more details, refer to the diarkisexec API reference.

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 Configuration

For meshConfigPath, specify the path to the JSON file. For details, refer to Mesh Configuration.

HTTP Server Configuration

Settings are specified in JSON.

For more details, refer to the HTTP API reference.

Creating Custom HTTP Endpoints

Diarkis's HTTP server allows you to write custom endpoints.

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)
}

最終更新