Added startup/shutdown messages to Rest, for troubleshooting bad configuration settings.

Added server nickname configuration option, to be returned in the /status API endpoint
Added configuration option for disabling the /check API endpoint in the event that users don't like that.
Added /status API endpoint
This commit is contained in:
Lucas Nicodemus 2011-09-05 00:30:19 -06:00
parent ce8d12b27f
commit 92d940e5dc
4 changed files with 47 additions and 6 deletions

View file

@ -187,6 +187,12 @@ namespace TShockAPI
[Description("This will announce a player's location on join")]
public bool EnableGeoIP = false;
[Description("This will turn on a token requirement for the /status API endpoint.")]
public bool EnableTokenEndpointAuthentication = false;
[Description("This is used when the API endpoint /status is queried.")]
public string ServerNickname = "TShock Server";
public static ConfigFile Read(string path)
{
if (!File.Exists(path))

View file

@ -36,5 +36,5 @@ using System.Runtime.InteropServices;
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.3.0.0904")]
[assembly: AssemblyFileVersion("3.3.0.0904")]
[assembly: AssemblyVersion("3.3.0.0905")]
[assembly: AssemblyFileVersion("3.3.0.0905")]

View file

@ -44,10 +44,12 @@ namespace TShockAPI
Ip = ip;
Port = port;
Start();
Console.WriteLine("TShock REST API loaded on " + ip + ":" + port + ".");
}
public virtual void Stop()
{
listener.Stop();
Console.WriteLine("TShock REST API disabled.");
}
public void Register(string path, RestCommandD callback)

View file

@ -16,14 +16,47 @@ namespace TShockAPI {
public void RegisterRestfulCommands()
{
Rest.Register(new RestCommand("/HelloWorld/name/{username}", usertest));
//Rest.Register(new RestCommand("/wizard/{username}", wizard));
Rest.Register(new RestCommand("/status", Status));
//RegisterExamples();
}
#region RestMethods
object Status(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request)
{
var ReturnBlock = new Dictionary<string, string>();
if (TShock.Config.EnableTokenEndpointAuthentication)
{
ReturnBlock.Add("status", "403");
ReturnBlock.Add("error", "Server settings require a token for this API call.");
return ReturnBlock;
}
ReturnBlock.Add("status", "200");
ReturnBlock.Add("name", TShock.Config.ServerNickname);
ReturnBlock.Add("port", Convert.ToString(TShock.Config.ServerPort));
ReturnBlock.Add("playercount", Convert.ToString(TShock.Players.Count()));
string CurrentPlayers = "";
foreach (TSPlayer tplayer in TShock.Players)
{
CurrentPlayers += tplayer.Name + ", ";
}
ReturnBlock.Add("players", CurrentPlayers);
return ReturnBlock;
}
#endregion
#region RestExampleMethods
public void RegisterExamples()
{
Rest.Register(new RestCommand("/HelloWorld/name/{username}", UserTest));
Rest.Register(new RestCommand("/wizard/{username}", Wizard));
}
//The Wizard example, for demonstrating the response convention:
object wizard(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request)
object Wizard(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request)
{
var returnBack = new Dictionary<string, string>();
returnBack.Add("status", "200"); //Keep this in everything, 200 = ok, etc. Standard http status codes.
@ -33,7 +66,7 @@ namespace TShockAPI {
}
//http://127.0.0.1:8080/HelloWorld/name/{username}?type=status
object usertest(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request)
object UserTest(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request)
{
var ret = new Dictionary<string, string>();
var type = parameters["type"];