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, you can define custom endpoints.
Setting Up the HTTP Server
To expose built-in commands to the client, you can set it up using the diarkisexec package.
You can expose built-in commands by specifying modules with diarkisexec.SetupDiarkis(). By setting ConfigPath for each module, you can customize the configuration.
You can expose custom commands using diarkisexec.SetServerCommandHandler().
You can set up a UDP server with diarkisexec.SetupDiarkisHTTPServer(). You can customize the server settings using a JSON file as an argument.
The above functions must be executed before calling diarkisexec.StartDiarkis().
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)
}
Using JSON in HTTP Endpoints
Diarkis's HTTP server automatically decodes the request body into req.JSONBody when the ContentType of the request is application/json. Note that an object must be described to store in the JSON Body.