HTTP Server

Overview

The HTTP server serves as the entry point for Diarkis. The application server connects to the HTTP server to obtain real-time connection endpoints and 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 set up using the diarkisexec package.

  • Use diarkisexec.SetupDiarkis() to specify modules and expose built-in commands. You can customize settings by specifying the ConfigPath for each module.

  • Use diarkisexec.SetServerCommandHandler() to expose custom commands.

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

⚠️ The above functions must be executed before calling diarkisexec.StartDiarkis().

For more details, refer to the API reference of diarkisexec.

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

Specify the path to the JSON file in meshConfigPath. For further details, please refer to #mesh-she-ding.

HTTP Server Configuration

The configuration is written in JSON format.

Key
Default
Description

address

"127.0.0.1"

Address for binding the UDP server

port

"7000"

Port for the UDP server to bind. The UDP server will automatically find an available port starting from the specified port.

useFixedPort

false

If true, the HTTP server will only bind to the specified port

timeout

5

HTTP response timeout (in seconds)

enableEncryption

true

If set to false, packet encryption and decryption will be disabled. Other servers must also be configured similarly.

For more details, please refer to the API reference of HTTP.

Creating Custom HTTP Endpoints

The HTTP server in Diarkis 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)
}

HTTP endpoints with JSON

Diarkis's HTTP server will automatically decode the request body into req.JSONBody if the request content type is application/json. Note that the JSON body must describe an object in order to work.

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

Last updated