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().
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.