exaroton

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2021 License: MIT Imports: 6 Imported by: 0

README

Go Reference Go Report Card

Golang

This is the unofficial Go implementation of this I'm still learning so expect badly design code PR are appreciated.

Code Inspiration: https://github.com/bwmarrin/discordgo

About

The exaroton API allows automated access to some basic functionalities of your game servers, such as starting or stopping the server. You can read the API documentation here: https://developers.exaroton.com

Installation

go get github.com/Jiternos/exaroton

Usage

To use the API and this client you have to get your API key, which you can generate in your exaroton account settings: https://exaroton.com/account

Import the package into your project.

import "github.com/Jiternos/exaroton"

Construct a new client.

exaroton := exaroton.New(envToken)

Remember to keep your token secret and don't add it to any private or public code repositories.

REST API
Get account info
client := exaroton.New(envToken)

account, err := client.Account()
if err != nil {
	log.Error(err)
	return
}

fmt.Printf("My account is %s and I have %d credits", account.Name, account.Credits)

The account object contains the fields and information as listed in the documentation.

List servers
servers, err := client.Servers()
if err != nil {
	log.Error(err)
	return
}

for _, s := range servers {
	fmt.Println(s.Name + ": " + s.ID)
}

Each server object contains the fields and information as listed in the documentation.

Get server information
server, err := client.Server(id)
if err != nil {
	log.Error(err)
	return
}

fmt.Println(server.Name)
Get the server status
server, err := client.Server(id)
if err != nil {
	log.Error(err)
	return
}

fmt.Prinln(server.GetStatus())
Start/stop/restart the server
server, err := client.Server(id)
if err != nil {
	log.Error(err)
	return
}

err = server.Start(client)
if err != nil {
	log.Error(err)
	return
}

err = server.Stop(client)
if err != nil {
	log.Error(err)
	return
}

err = server.Restart(client)
if err != nil {
	log.Error(err)
	return
}

It's important to catch errors, because incorrect calls, e.g. a server.Stop() when the server is offline will result in an error.

Execute a server command
server, err := client.Server(id)
if err != nil {
	log.Error(err)
	return
}

err := server.ExecuteCommand(client, "say hello")
if err != nil {
	log.Error(err)
	return
}
Get the server logs
client, err := exaroton.New(envToken)
if err != nil {
	log.Error(err)
	return
}

server, err := client.Server(id)
if err != nil {
	log.Error(err)
	return
}

logs, err := server.GetLogs(client)
if err != nil {
	log.Error(err)
	return
}

fmt.Println(logs)

This is cached and will not return the latest updates immediately. It's also not possible to get the server logs while the server is loading, stopping or saving.

Share the server logs via mclo.gs
server, err := client.Server(id)
if err != nil {
	log.Error(err)
	return
}

logs, err := server.ShareLogs(client)
if err != nil {
	log.Error(err)
	return
}

fmt.Println(logs.ID)
fmt.Println(logs.Raw)
fmt.Println(logs.URL)

This is cached and will not return the latest updates immediately. It's also not possible to share the server logs while the server is loading, stopping or saving.

Set the server MOTD
server, err := client.Server(id)
if err != nil {
	log.Error(err)
	return
}

err = server.SetMOTD(client, "Hello World!")
if err != nil {
	log.Error(err)
	return
}

Get the server RAM
server, err := client.Server(id)
if err != nil {
	log.Error(err)
	return
}

ram, err := server.GetRam(client)
if err != nil {
	log.Error(err)
	return
}

fmt.Println(ram)

The amount of RAM is returned in full GiB.

Set the server RAM
server, err := client.Server(id)
if err != nil {
	log.Error(err)
	return
}

err = server.SetRam(client, 2)
if err != nil {
	log.Error(err)
	return
}

The RAM is set in full GiB and has to be between 2 and 16.

Player lists

A player list is a list of players such as the whitelist, ops or bans. Player list entries are usually usernames, but might be something else, e.g. IPs in the banned-ips list. All player list operations are storage operations that might take a while, so try to reduce the amount of requests and combine actions when possible (e.g. adding/deleting multiple entries at once). Player lists are also cached any might not immediately return new results when changed through other methods e.g. in-game.

Get a player list object

You can list all available player lists...

playerList, err := server.GetPlayerList(client)
if err != nil {
	log.Error(err)
	return
}

fmt.Println(playerList.List)
Get all player list entries
playerList, err := server.GetPlayerList(client, "whitelist")
if err != nil {
	log.Error(err)
	return
}
Add player list entries

We handle all the heavy work of adding player list entries for you, e.g. automatically adding UUIDs depending on the online mode or executing the necessary commands while the server is running.

playerList, err := server.GetPlayerList(client, "whitelist")
if err != nil {
	log.Error(err)
	return
}

username := []string{"a", "b", "c", "d"}

playerList.AddEntry(client, username)
Delete player list entries
playerList, err := server.GetPlayerList(client, "whitelist")
if err != nil {
	log.Error(err)
	return
}

username := []string{"a", "b", "c", "d"}

playerList.RemoveEntry(client, username)
Websocket API

The websocket API allows a constant connection to our websocket service to receive events in real time without polling (e.g. trying to get the server status every few seconds).

Work in progress

Documentation

Index

Constants

View Source
const VERSION = "1.0.2"

Exaroton API Wrapper Version

Variables

View Source
var (
	EndpointExaroton       = "https://api.exaroton.com/"
	EndpointAPI            = EndpointExaroton + "v" + APIVersion + "/"
	EndpointAccount        = EndpointAPI + "account/"
	EndpointServers        = EndpointAPI + "servers/"
	EndpointServer         = func(serverID string) string { return EndpointServers + serverID }
	EndpointLogs           = func(serverID string) string { return EndpointServer(serverID) + "/logs/" }
	EndpointShareLogs      = func(serverID string) string { return EndpointLogs(serverID) + "share/" }
	EndpointMOTD           = func(serverID string) string { return EndpointServer(serverID) + "/options/" + "motd/" }
	EndpointRam            = func(serverID string) string { return EndpointServer(serverID) + "/options" + "/ram/" }
	EndpointStart          = func(serverID string) string { return EndpointServer(serverID) + "/start/" }
	EndpointStop           = func(serverID string) string { return EndpointServer(serverID) + "/stop/" }
	EndpointRestart        = func(serverID string) string { return EndpointServer(serverID) + "/restart/" }
	EndpointCommand        = func(serverID string) string { return EndpointServer(serverID) + "/command/" }
	EndpointGetPlayerLists = func(serverID string) string { return EndpointServer(serverID) + "/playerlists/" }
	EndpointPlayerLists    = func(serverID string, playerList string) string {
		return EndpointGetPlayerLists(serverID) + playerList + "/"
	}
)

Exaroton API Endpoints

View Source
var APIVersion = "1"

Exaroton API Version

Functions

This section is empty.

Types

type Account

type Account struct {
	// The user's username.
	Name string `json:"name"`

	// The user's email.
	Email string `json:"email"`

	// The user's credits.
	Credits int64 `json:"credits"`

	// Whether the user is verified.
	Verified bool `json:"verified"`
}

type Logs

type Logs struct {
	// The log content
	Content string `json:"content"`
}

Logs struct to represent the logs content Gonna remove this (just realize how bad it is)

type PlayerList

type PlayerList struct {
	// The server unique ID
	ID string

	// The playerlist type
	// Whitelist/Ops/Banned-Players/Banned-IPs
	Type string

	// The list of players
	List []string
}

PlayerList struct to represent the playerlist

func (*PlayerList) AddEntry

func (playerList *PlayerList) AddEntry(s *Session, entries []string) (err error)

Add a name to the player list

func (*PlayerList) RemoveEntry

func (playerList *PlayerList) RemoveEntry(s *Session, entries []string) (err error)

Remove a name from the player list

type Players

type Players struct {
	// The server max player
	Max int64 `json:"max"`

	// The active player count
	Count int64 `json:"count"`

	// The active player list
	List []interface{} `json:"list"`
}

Players struct to represent player list

type Request

type Request struct {
	Success string `json:"success"`
	Errors  string `json:"error"`
	Data    string `json:"data"`
}

Request struct for the REST API result

type Server

type Server struct {
	// The server unique ID
	ID string `json:"id"`

	// The server name
	Name string `json:"name"`

	// The server full address
	Address string `json:"address"`

	// The server MOTD
	MOTD string `json:"motd"`

	// The server status is an integer as described in the documentation.
	// https://developers.exaroton.com/#header-server-status
	Status int64 `json:"status"`

	// The server host adress, only available if the server is online
	Host interface{} `json:"host"`

	// The server port, only available if the server is online
	Port interface{} `json:"port"`

	// The server player information
	Players Players `json:"players"`

	// The server software
	Software Software `json:"software"`

	// Whether the server is shared.
	Shared bool `json:"shared"`
}

Server struct to represent a Exaroton server

func (*Server) ExecuteCommand

func (server *Server) ExecuteCommand(s *Session, command string) (err error)

Execute a command in the server console

func (*Server) GetLogs

func (server *Server) GetLogs(s *Session) (logs string, err error)

Get the content of the server logs

func (*Server) GetPlayerList

func (server *Server) GetPlayerList(s *Session, types ...string) (playerList *PlayerList, err error)

Get the server player list by name

func (*Server) GetRam

func (server *Server) GetRam(s *Session) (ram float64, err error)

Get the server ram

func (*Server) GetStatus

func (server *Server) GetStatus() string

Get the server status

func (*Server) Restart

func (server *Server) Restart(s *Session) (err error)

Restart the server

func (*Server) SetMOTD added in v1.0.2

func (server *Server) SetMOTD(s *Session, motd string) (err error)

Set the server MOTD

func (*Server) SetRam

func (server *Server) SetRam(s *Session, ram float64) (err error)

Set the ram of the server

func (*Server) ShareLogs

func (server *Server) ShareLogs(s *Session) (shareLogs *ShareLogs, err error)

Upload the content of the server logs to mclo.gs

func (*Server) Start

func (server *Server) Start(s *Session) (err error)

Start the server

func (*Server) Stop

func (server *Server) Stop(s *Session) (err error)

Stop the server

type Session

type Session struct {
	// General configurable settings.
	// Authentication token for this session
	Token string

	// The http client used for REST requests
	Client *http.Client

	// The user agent used for REST APIs
	UserAgent string
}

Session struct to represent a connection to the Exaroton API.

func New

func New(token string) (s *Session)

Creates a new Exaroton session

func (*Session) Account

func (s *Session) Account() (account *Account, err error)

Account returns the account information as struct

func (*Session) Request

func (s *Session) Request(method, url string, body io.Reader) (response []byte, err error)

Send a request to the Exaroton REST API with the given method and path.

func (*Session) Server

func (s *Session) Server(serverID string) (server *Server, err error)

Get the server details as a struct

func (*Session) Servers

func (s *Session) Servers() (servers []*Server, err error)

Get a list of all servers that the user has access to

type ShareLogs

type ShareLogs struct {
	// The log id
	ID string `json:"id"`

	// The mc.logs url
	URL string `json:"url"`

	// The raw mc.logs url
	Raw string `json:"raw"`
}

ShareLogs struct to represent the mc.logs share link

type Software

type Software struct {
	// The software unique ID
	// NO USAGE (Internal Usage Only)
	ID string `json:"id"`

	// The software name
	Name string `json:"name"`

	// The software version
	Version string `json:"version"`
}

Software struct to represent the server software

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL