Skip to content

Commit

Permalink
Final version with SignalR
Browse files Browse the repository at this point in the history
  • Loading branch information
mfidemraizer committed Nov 21, 2014
1 parent 3d94400 commit d150f35
Show file tree
Hide file tree
Showing 105 changed files with 24,380 additions and 108 deletions.
6 changes: 6 additions & 0 deletions App.Api/App.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.AspNet.SignalR.Client">
<HintPath>..\packages\Microsoft.AspNet.SignalR.Client.2.1.2\lib\net45\Microsoft.AspNet.SignalR.Client.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin">
<HintPath>..\packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll</HintPath>
</Reference>
Expand All @@ -58,6 +61,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.2\lib\net45\System.Web.Http.dll</HintPath>
Expand All @@ -74,6 +78,7 @@
<ItemGroup>
<Compile Include="Configuration.cs" />
<Compile Include="Filters\CultureFilterAttribute.cs" />
<Compile Include="Formatting\JsonMediaTypeFormatter.cs" />
<Compile Include="PathHelper.cs" />
<Compile Include="ProductController.cs" />
<Compile Include="Program.cs" />
Expand All @@ -88,6 +93,7 @@
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
6 changes: 6 additions & 0 deletions App.Api/App.Api.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ShowAllFiles</ProjectView>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion App.Api/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public Configuration()
{
//Services.Add(typeof(IExceptionHandler), null);

//Formatters.Insert(0, new JsonNetMediaTypeFormatter());
Formatters.Insert(0, new JsonNetMediaTypeFormatter());
Filters.Add(new CultureFilterAttribute());
this.MapHttpAttributeRoutes();
}
Expand Down
5 changes: 4 additions & 1 deletion App.Api/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static MediaTypeHeaderValue GetMimeType(string path)
{
string extension = Path.GetExtension(path);

switch(extension)
switch (extension)
{
case ".js":
return new MediaTypeHeaderValue("text/javascript");
Expand All @@ -28,6 +28,9 @@ public static MediaTypeHeaderValue GetMimeType(string path)
case ".json":
return new MediaTypeHeaderValue("application/json");

case ".less":
return new MediaTypeHeaderValue("text/css");

default:
throw new NotSupportedException();
}
Expand Down
33 changes: 28 additions & 5 deletions App.Api/ProductController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using App.Api.Redis;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
Expand All @@ -13,19 +19,36 @@ namespace App.Api
[RoutePrefix("v1/products")]
public sealed class ProductController : ApiController
{
[HttpGet]
[HttpGet, Route("")]
public async Task<HttpResponseMessage> GetProductsAsync()
{
HttpResponseMessage response = null;
IDatabase db = RedisClientFactory.GetDatabase();

if(await db.KeyExistsAsync("users"))
{
RedisValue[] result = await db.SortAsync("products", by: "nosort", get: new RedisValue[] { "products:*" });

}
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent('[' + string.Join(",", result.Select(product => (string)product)) + ']');
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

return response;
}

[HttpPost, Route("")]
public async Task<HttpResponseMessage> CreateProductAsync(dynamic args)
{
IDatabase db = RedisClientFactory.GetDatabase();
ITransaction transaction = RedisClientFactory.GetTransaction();

long newId = await db.StringIncrementAsync("products:id");

transaction.SetAddAsync("products", newId);
transaction.StringSetAsync("products:" + newId, (string)await JsonConvert.SerializeObjectAsync(args));

await transaction.ExecuteAsync();

await db.PublishAsync("products:new", (string)args.name);

return Request.CreateResponse();
}
}
}
33 changes: 31 additions & 2 deletions App.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using Microsoft.Owin.Hosting;
using App.Api.Redis;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Hubs;
using Microsoft.Owin.Hosting;
using StackExchange.Redis;
using System;
using System.Threading.Tasks;

namespace App.Api
{
Expand All @@ -9,9 +14,33 @@ static void Main(string[] args)
{
const string host = "http://localhost:5454";

using(WebApp.Start<Startup>(host))
using (WebApp.Start<Startup>(host))
{
Console.WriteLine("Listening on '{0}'", host);

IDatabase db = RedisClientFactory.GetDatabase();
ISubscriber subscriber = db.Multiplexer.GetSubscriber();

subscriber.Subscribe
(
"products:new",
async (channel, value) =>
{
using (HubConnection connection = new HubConnection("http://localhost:9555"))
{
IHubProxy productHubProxy = connection.CreateHubProxy("ProductHub");

await connection.Start();
await productHubProxy.Invoke("NotifyProductCreated", new object[] { (string)value });

//productHubProxy.On("productAdded", name =>
//{

//});
}
}
);

Console.Read();
}
}
Expand Down
2 changes: 1 addition & 1 deletion App.Api/StaticFileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace App.Api
{
public sealed class StaticFileController : ApiController
{
[HttpGet, Route("{*path}")]
[HttpGet, Route("{*path:regex(.+(?:js|css|less|html|json)$)}")]
public HttpResponseMessage GetStaticFile(string path)
{
if(string.IsNullOrEmpty(path) || !Path.HasExtension(path))
Expand Down
Binary file modified App.Api/bin/Debug/App.Api.exe
Binary file not shown.
Binary file modified App.Api/bin/Debug/App.Api.pdb
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit d150f35

Please sign in to comment.