Web UI development started

This commit is contained in:
zone117x
2015-04-14 08:51:56 -06:00
parent 7b4e6f97d5
commit a4f18471a8
18 changed files with 4035 additions and 175 deletions

View File

@@ -14,23 +14,12 @@ namespace Jackett
{
HttpListener listener;
IndexerManager indexerManager;
static string[] StaticFiles = Directory.EnumerateFiles("WebContent", "*", SearchOption.AllDirectories).Select(Path.GetFileName).ToArray();
enum WebApiMethod
{
GetConfigForm,
ConfigureIndexer
}
static Dictionary<string, WebApiMethod> WebApiMethods = new Dictionary<string, WebApiMethod>
{
{ "get_config_form", WebApiMethod.GetConfigForm },
{ "configure_indexer", WebApiMethod.ConfigureIndexer }
};
WebApi webApi;
public Server()
{
indexerManager = new IndexerManager();
webApi = new WebApi(indexerManager);
listener = new HttpListener();
listener.Prefixes.Add("http://*:9117/");
@@ -42,7 +31,7 @@ namespace Jackett
while (true)
{
var context = await listener.GetContextAsync();
ProcessContext(context);
ProcessHttpRequest(context);
}
}
@@ -52,114 +41,10 @@ namespace Jackett
listener.Abort();
}
static Dictionary<string, string> MimeMapping = new Dictionary<string, string> {
{ ".html", "text/html" },
{ ".js", "application/javascript" }
};
async void ServeStaticFile(HttpListenerContext context, string file)
async void ProcessHttpRequest(HttpListenerContext context)
{
var contentFile = File.ReadAllBytes(Path.Combine("WebContent", file));
string contentType;
MimeMapping.TryGetValue(Path.GetExtension(file), out contentType);
context.Response.ContentType = contentType;
context.Response.StatusCode = (int)HttpStatusCode.OK;
await context.Response.OutputStream.WriteAsync(contentFile, 0, contentFile.Length);
context.Response.OutputStream.Close();
}
async void ProcessWebApiRequest(HttpListenerContext context, WebApiMethod method)
{
var query = HttpUtility.ParseQueryString(context.Request.Url.Query);
string postData = await new StreamReader(context.Request.InputStream).ReadToEndAsync();
JToken dataJson = JObject.Parse(postData);
JToken jsonReply = new JObject();
var indexerString = (string)dataJson["indexer"];
IndexerInterface indexer;
try
if (webApi.HandleRequest(context))
{
indexer = indexerManager.GetIndexer(indexerString);
}
catch (Exception ex)
{
jsonReply["result"] = "error";
jsonReply["error"] = ex.Message;
ReplyWithJson(context, jsonReply);
return;
}
context.Response.ContentType = "text/json";
context.Response.StatusCode = (int)HttpStatusCode.OK;
switch (method)
{
case WebApiMethod.GetConfigForm:
try
{
var config = await indexer.GetConfigurationForSetup();
jsonReply = config.ToJson();
}
catch (Exception ex)
{
jsonReply["result"] = "error";
jsonReply["error"] = ex.Message;
}
break;
case WebApiMethod.ConfigureIndexer:
try
{
await indexer.ApplyConfiguration(dataJson);
await indexer.VerifyConnection();
jsonReply["result"] = "success";
}
catch (Exception ex)
{
jsonReply["result"] = "error";
jsonReply["error"] = ex.Message;
if (ex is ExceptionWithConfigData)
{
jsonReply["config"] = ((ExceptionWithConfigData)ex).ConfigData.ToJson();
}
}
break;
default:
jsonReply["result"] = "error";
jsonReply["error"] = "Invalid API method";
break;
}
ReplyWithJson(context, jsonReply);
}
async void ReplyWithJson(HttpListenerContext context, JToken json)
{
byte[] jsonBytes = Encoding.UTF8.GetBytes(json.ToString());
await context.Response.OutputStream.WriteAsync(jsonBytes, 0, jsonBytes.Length);
context.Response.OutputStream.Close();
}
async void ProcessContext(HttpListenerContext context)
{
Console.WriteLine(context.Request.Url.Query);
string path = context.Request.Url.AbsolutePath.TrimStart('/');
if (path == "")
path = "index.html";
if (Array.IndexOf(StaticFiles, path) > -1)
{
ServeStaticFile(context, path);
return;
}
WebApiMethod apiMethod;
if (WebApiMethods.TryGetValue(path, out apiMethod))
{
ProcessWebApiRequest(context, apiMethod);
return;
}