private IndexPutAction CreatePutIndexAction(int status, String message, NameValueCollection parameters) { IndexPutAction indexAction = new IndexPutAction(); indexAction.Operation = "put"; indexAction.Status = status; indexAction.Message = message; IndexEntry indexEntry = new IndexEntry(); indexEntry.Id = parameters["id"]; indexEntry.Name = parameters["name"]; indexEntry.Mobile = parameters["mobile"]; indexAction.Entry = indexEntry; return(indexAction); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async(context) => { string queryString = context.Request.QueryString.ToString(); string directory = System.Environment.GetEnvironmentVariable("APPSETTING_directory"); directory = directory ?? "c:\\temp\\Lucene"; NameValueCollection parameters = HttpUtility.ParseQueryString(queryString); if (context.Request.Path.Equals("/put") || context.Request.Method.Equals("PUT")) { if (parameters["id"] == null || parameters["name"] == null) { HttpResponse response = context.Response; response.StatusCode = 500; await response.WriteAsync("Missing Parameter must have [id, name]"); } else { indexer.AddIndexEntry(parameters); IndexPutAction indexAction = CreatePutIndexAction(200, "[put] operation successful", parameters); await context.Response.WriteAsync(SerializeIndexAction(indexAction)); } } else if (context.Request.Path.Equals("/get")) { var result = indexer.Get(parameters["id"]); if (result != null) { var output = SerializeIndexItem(result); await context.Response.WriteAsync(output); } else { HttpResponse response = context.Response; response.StatusCode = 401; await response.WriteAsync(parameters["id"] + ": Not Found"); } } else if (context.Request.Path.Equals("/delete") || context.Request.Method.Equals("DELETE")) { indexer.Delete(parameters["id"]); await context.Response.WriteAsync(SerializeIndexAction( CreateDeleteIndexAction(200, "[delete] operation successful", parameters["id"]))); } else if (context.Request.Path.Equals("/find")) { if (parameters["field"] == null || parameters["term"] == null) { HttpResponse response = context.Response; response.StatusCode = 500; await response.WriteAsync("Missing Parameter must have [field] and [term]"); } else { var result = indexer.Find(parameters["field"], parameters["term"]); var output = SerializeIndexItems(result); await context.Response.WriteAsync(output); } } else if (context.Request.Path.Equals("/search")) { if (parameters["term"] == null) { HttpResponse response = context.Response; response.StatusCode = 500; await response.WriteAsync("Missing Parameter must have [term]"); } else { var result = indexer.Search(parameters["term"]); var output = SerializeIndexItems(result); await context.Response.WriteAsync(output); } } else if (context.Request.Path.Equals("/upload")) { Stream stream = context.Request.Body; List <IndexEntry> putOperations = new List <IndexEntry>(); HttpMultipartParser parser = new HttpMultipartParser(stream); foreach (MultiPartFile file in parser.Files) { var str = Encoding.ASCII.GetString(file.FileContents).Trim(); Console.WriteLine("'" + str + "'"); List <IndexOperation> operations = Deserialize <List <IndexOperation> >(str); foreach (IndexOperation operation in operations) { putOperations.Add(operation.Entry); } indexer.AddIndexEntries(putOperations); await context.Response.WriteAsync(SerializeIndexAction( CreatBulkPutIndexAction(200, "[bulk(put)] operation successful", operations.Count))); } } else { HttpResponse response = context.Response; response.StatusCode = 500; await response.WriteAsync("Unknown command"); } }); }