IP/Port can now be changed before started.

RestVerbs added instead of Dictionary<string,string>
This commit is contained in:
high 2011-09-05 01:11:08 -04:00
parent d824e71507
commit f087ae5c83
2 changed files with 25 additions and 8 deletions

View file

@ -17,21 +17,33 @@ namespace TShockAPI
/// <param name="parameters">Parameters in the url</param> /// <param name="parameters">Parameters in the url</param>
/// <param name="request">Http request</param> /// <param name="request">Http request</param>
/// <returns>Response object or null to not handle request</returns> /// <returns>Response object or null to not handle request</returns>
public delegate object RestCommandD(Dictionary<string,string> verbs, IParameterCollection parameters, RequestEventArgs request); public delegate object RestCommandD(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request);
public class Rest : IDisposable public class Rest : IDisposable
{ {
readonly List<RestCommand> commands = new List<RestCommand>(); readonly List<RestCommand> commands = new List<RestCommand>();
HttpListener listener; HttpListener listener;
public IPAddress Ip { get; set; }
public int Port { get; set; }
public Rest(IPAddress ip, int port) public Rest(IPAddress ip, int port)
{ {
listener = HttpListener.Create(ip, port); Ip = ip;
listener.RequestReceived += OnRequest; Port = port;
} }
public void Start() public void Start()
{ {
listener.Start(int.MaxValue); if (listener != null)
{
listener = HttpListener.Create(Ip, Port);
listener.RequestReceived += OnRequest;
listener.Start(int.MaxValue);
}
}
public void Start(IPAddress ip, int port)
{
Ip = ip;
Port = port;
Start();
} }
public void Stop() public void Stop()
{ {
@ -67,7 +79,7 @@ namespace TShockAPI
var matches = Regex.Matches(e.Request.Uri.AbsolutePath, com.UriMatch); var matches = Regex.Matches(e.Request.Uri.AbsolutePath, com.UriMatch);
if (matches.Count == com.UriNames.Length) if (matches.Count == com.UriNames.Length)
{ {
var verbs = new Dictionary<string, string>(); var verbs = new RestVerbs();
for (int i = 0; i < matches.Count; i++) for (int i = 0; i < matches.Count; i++)
verbs.Add(com.UriNames[i], matches[i].Groups[1].Value); verbs.Add(com.UriNames[i], matches[i].Groups[1].Value);
@ -104,6 +116,11 @@ namespace TShockAPI
#endregion #endregion
} }
public class RestVerbs : Dictionary<string, string>
{
}
public class RestCommand public class RestCommand
{ {
public string UriTemplate { get; protected set; } public string UriTemplate { get; protected set; }

View file

@ -203,7 +203,7 @@ namespace TShockAPI
Log.ConsoleInfo("Backups " + (Backups.Interval > 0 ? "Enabled" : "Disabled")); Log.ConsoleInfo("Backups " + (Backups.Interval > 0 ? "Enabled" : "Disabled"));
if (Initialized != null) if (Initialized != null)
Initialized(); Initialized();
RestApi.Register(new RestCommand("/HelloWorld/name/{username}", usertest)); RestApi.Register(new RestCommand("/HelloWorld/name/{username}", usertest));
} }
@ -237,7 +237,7 @@ namespace TShockAPI
} }
//http://127.0.0.1:8080/HelloWorld/name/{username}?type=status //http://127.0.0.1:8080/HelloWorld/name/{username}?type=status
object usertest(Dictionary<string,string> verbs, IParameterCollection parameters, RequestEventArgs request) object usertest(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request)
{ {
var ret = new Dictionary<string, string>(); var ret = new Dictionary<string, string>();
var type = parameters["type"]; var type = parameters["type"];