Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 5f88271

Browse files
author
Mathew McLoughlin
committed
Moved over to Nancy
1 parent f665007 commit 5f88271

9 files changed

Lines changed: 159 additions & 4196 deletions

SqlStreamStore.HAL/HALResponse.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
using System.Linq;
32
using Newtonsoft.Json;
43
using SqlStreamStore.Streams;
54

@@ -81,7 +80,7 @@ public static Links CreateItemLink(long position)
8180
{
8281
return new Links
8382
{
84-
Self = new { Href = "/streamMessage?position=" + position },
83+
Self = new { Href = "/stream/" + position },
8584
};
8685
}
8786
}

SqlStreamStore.HAL/Program.cs

Lines changed: 5 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,31 @@
1-
using Microsoft.Owin.Builder;
2-
using Microsoft.Owin.Hosting;
3-
using Owin;
1+
using Microsoft.Owin.Hosting;
42
using SqlStreamStore.Streams;
53
using System;
6-
using System.Web.Http;
7-
using System.Web.Http.Dependencies;
8-
using TinyIoC;
9-
using MidFunc = System.Func<
10-
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>,
11-
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>>;
12-
using System.Collections.Generic;
13-
using System.IO;
14-
using System.Linq;
15-
using Newtonsoft.Json.Serialization;
164

175
namespace SqlStreamStore.HAL
186
{
197
class Program
208
{
21-
static void Main(string[] args)
9+
static void Main()
2210
{
2311
var streamStore = new InMemoryStreamStore();
2412
var messages = SeedData.Get(1000);
2513

2614
streamStore.AppendToStream("SomeStream", ExpectedVersion.Any, messages);
2715

28-
var settings = new HALSettings
16+
var settings = new SqlStreamStoreHalSettings
2917
{
3018
Store = streamStore,
3119
PageSize = 20
3220
};
3321

34-
var baseUrl = "http://localhost:8080";
22+
var baseUrl = "http://+:8080";
3523

36-
using (WebApp.Start(baseUrl, app => app.Use(HALMiddleware.Handle(settings))))
24+
using (WebApp.Start(baseUrl, app => app.UseSqlStreamStoreHal(settings)))
3725
{
3826
Console.WriteLine("Press Enter to quit.");
3927
Console.ReadKey();
4028
}
4129
}
4230
}
43-
44-
internal static class HALMiddleware
45-
{
46-
public static MidFunc Handle(HALSettings settings)
47-
{
48-
return next =>
49-
{
50-
var config = new HttpConfiguration();
51-
52-
var container = new TinyIoCContainer();
53-
container.Register(settings);
54-
55-
config.DependencyResolver = new Resolver(container);
56-
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
57-
58-
var baseUrl = settings.BaseUrl == null ? "" : settings.BaseUrl + "/";
59-
60-
config.Routes.MapHttpRoute(
61-
"SqlStreamStoreHAL.Paged",
62-
Path.Combine(baseUrl, "stream/{direction}/{position}"),
63-
new { controller = "SqlStreamStoreHAL", position = RouteParameter.Optional, action = "index", direction = "forwards" }
64-
);
65-
66-
config.Routes.MapHttpRoute(
67-
"SqlStreamStoreHAL.Message",
68-
Path.Combine(baseUrl, "streanMessage/{position}"),
69-
new { controller = "SqlStreamStoreHAL", action = "message", position = RouteParameter.Optional }
70-
);
71-
72-
var appBuilder = new AppBuilder();
73-
appBuilder.UseWebApi(config);
74-
75-
return appBuilder.Build();
76-
};
77-
}
78-
}
79-
80-
class Resolver : IDependencyResolver
81-
{
82-
private TinyIoCContainer _container;
83-
84-
public Resolver(TinyIoCContainer container)
85-
{
86-
_container = container;
87-
}
88-
89-
public IDependencyScope BeginScope()
90-
{
91-
return this;
92-
}
93-
94-
public void Dispose()
95-
{
96-
}
97-
98-
public object GetService(Type serviceType)
99-
{
100-
if (!_container.CanResolve(serviceType))
101-
{
102-
return null;
103-
}
104-
105-
return _container.Resolve(serviceType);
106-
}
107-
108-
public IEnumerable<object> GetServices(Type serviceType)
109-
{
110-
if (!_container.CanResolve(serviceType))
111-
{
112-
return Enumerable.Empty<object>();
113-
}
114-
115-
return _container.ResolveAll(serviceType);
116-
}
117-
}
118-
119-
public class HALSettings
120-
{
121-
public IReadonlyStreamStore Store { get; set; }
122-
123-
public string BaseUrl { get; set; }
124-
125-
public int PageSize { get; set; }
126-
}
12731
}

SqlStreamStore.HAL/SeedData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static IEnumerable<NewStreamMessage> Get(int count)
2525

2626
yield return new NewStreamMessage(
2727
Guid.NewGuid(),
28-
message.GetType().FullName.ToString(),
28+
message.GetType().FullName,
2929
SimpleJson.SerializeObject(message)
3030
);
3131
}

SqlStreamStore.HAL/SqlStreamStore.HAL.csproj

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@
4545
<HintPath>..\packages\Microsoft.Owin.Hosting.3.0.1\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
4646
<Private>True</Private>
4747
</Reference>
48+
<Reference Include="Nancy, Version=1.4.2.0, Culture=neutral, processorArchitecture=MSIL">
49+
<HintPath>..\packages\Nancy.1.4.3\lib\net40\Nancy.dll</HintPath>
50+
<Private>True</Private>
51+
</Reference>
52+
<Reference Include="Nancy.Owin, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
53+
<HintPath>..\packages\Nancy.Owin.1.4.1\lib\net40\Nancy.Owin.dll</HintPath>
54+
<Private>True</Private>
55+
</Reference>
4856
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
4957
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
5058
<Private>True</Private>
@@ -53,8 +61,8 @@
5361
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
5462
<Private>True</Private>
5563
</Reference>
56-
<Reference Include="Sigil, Version=4.6.1.0, Culture=neutral, PublicKeyToken=2d06c3494341c8ab, processorArchitecture=MSIL">
57-
<HintPath>..\packages\Sigil.4.6.1\lib\net45\Sigil.dll</HintPath>
64+
<Reference Include="Sigil, Version=4.7.0.0, Culture=neutral, PublicKeyToken=2d06c3494341c8ab, processorArchitecture=MSIL">
65+
<HintPath>..\packages\Sigil.4.7.0\lib\net45\Sigil.dll</HintPath>
5866
<Private>True</Private>
5967
</Reference>
6068
<Reference Include="SqlStreamStore, Version=0.4.3.0, Culture=neutral, processorArchitecture=MSIL">
@@ -63,33 +71,15 @@
6371
</Reference>
6472
<Reference Include="System" />
6573
<Reference Include="System.Core" />
66-
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
67-
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
68-
<Private>True</Private>
69-
</Reference>
70-
<Reference Include="System.Runtime.Serialization" />
71-
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
72-
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
73-
<Private>True</Private>
74-
</Reference>
75-
<Reference Include="System.Web.Http.Owin, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
76-
<HintPath>..\packages\Microsoft.AspNet.WebApi.Owin.5.2.3\lib\net45\System.Web.Http.Owin.dll</HintPath>
77-
<Private>True</Private>
78-
</Reference>
79-
<Reference Include="System.Xml.Linq" />
80-
<Reference Include="System.Data.DataSetExtensions" />
8174
<Reference Include="Microsoft.CSharp" />
82-
<Reference Include="System.Data" />
83-
<Reference Include="System.Net.Http" />
84-
<Reference Include="System.Xml" />
8575
</ItemGroup>
8676
<ItemGroup>
8777
<Compile Include="HALResponse.cs" />
8878
<Compile Include="Program.cs" />
8979
<Compile Include="Properties\AssemblyInfo.cs" />
9080
<Compile Include="SeedData.cs" />
91-
<Compile Include="SqlStreamStoreHALController.cs" />
92-
<Compile Include="TinyIoC.cs" />
81+
<Compile Include="SqlStreamStoreHalMiddleware.cs" />
82+
<Compile Include="SqlStreamStoreHalModule.cs" />
9383
</ItemGroup>
9484
<ItemGroup>
9585
<None Include="App.config" />

SqlStreamStore.HAL/SqlStreamStoreHALController.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.Owin.Builder;
5+
using Nancy;
6+
using Nancy.Bootstrapper;
7+
using Nancy.TinyIoc;
8+
using Owin;
9+
10+
namespace SqlStreamStore.HAL
11+
{
12+
internal static class SqlStreamStoreHalMiddleware
13+
{
14+
public static void UseSqlStreamStoreHal(this IAppBuilder app, SqlStreamStoreHalSettings settings)
15+
{
16+
app.Use(Handle(settings));
17+
}
18+
19+
private static Func<Func<IDictionary<string, object>, Task>, Func<IDictionary<string, object>, Task>> Handle(SqlStreamStoreHalSettings settings)
20+
{
21+
return next =>
22+
{
23+
var appBuilder = new AppBuilder();
24+
25+
appBuilder
26+
.UseNancy(configuration => configuration.Bootstrapper = new Bootstrapper(settings))
27+
.Run(ctx => next(ctx.Environment));
28+
29+
return appBuilder.Build();
30+
};
31+
}
32+
}
33+
34+
internal class Bootstrapper : DefaultNancyBootstrapper
35+
{
36+
private readonly SqlStreamStoreHalSettings _settings;
37+
38+
public Bootstrapper(SqlStreamStoreHalSettings settings)
39+
{
40+
_settings = settings;
41+
}
42+
43+
protected override IEnumerable<ModuleRegistration> Modules => new List<ModuleRegistration>
44+
{
45+
new ModuleRegistration(typeof(SqlStreamStoreHalModule))
46+
};
47+
48+
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
49+
{
50+
container.Register(_settings);
51+
base.ApplicationStartup(container, pipelines);
52+
}
53+
}
54+
55+
public class SqlStreamStoreHalSettings
56+
{
57+
public IReadonlyStreamStore Store { get; set; }
58+
59+
public string BaseUrl { get; set; }
60+
61+
public int PageSize { get; set; }
62+
}
63+
}

0 commit comments

Comments
 (0)