Refactoring done, Moved some handlers to the lib, · Code-Sharp/uHttpSharp@06afad7 · GitHub
Skip to content

Commit 06afad7

Browse files
committed
Refactoring done, Moved some handlers to the lib,
1 parent 429d7da commit 06afad7

21 files changed

Lines changed: 400 additions & 165 deletions
Lines changed: 2 additions & 2 deletions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
using System.Threading.Tasks;
2020
using uhttpsharp;
2121

22-
namespace uhttpsharpdemo
22+
namespace uhttpsharpdemo.Handlers
2323
{
2424
public class ErrorHandler : IHttpRequestHandler
2525
{
26-
public async Task<HttpResponse> Handle(HttpRequest httpRequest, System.Func<System.Threading.Tasks.Task<HttpResponse>> next)
26+
public async Task<HttpResponse> Handle(IHttpRequest httpRequest, System.Func<System.Threading.Tasks.Task<HttpResponse>> next)
2727
{
2828
return new HttpResponse(HttpResponseCode.NotFound, "These are not the droids you are looking for.");
2929
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
using uhttpsharp;
2020

21-
namespace uhttpsharpdemo
21+
namespace uhttpsharpdemo.Handlers
2222
{
2323
public class IndexHandler : IHttpRequestHandler
2424
{
2525

26-
public async System.Threading.Tasks.Task<HttpResponse> Handle(HttpRequest httpRequest, System.Func<System.Threading.Tasks.Task<HttpResponse>> next)
26+
public async System.Threading.Tasks.Task<HttpResponse> Handle(IHttpRequest httpRequest, System.Func<System.Threading.Tasks.Task<HttpResponse>> next)
2727
{
2828
return new HttpResponse(HttpResponseCode.Ok, "Welcome to the Index. ☺");
2929
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Diagnostics;
3+
using uhttpsharp;
4+
5+
namespace uhttpsharpdemo.Handlers
6+
{
7+
public class TimingHandler : IHttpRequestHandler
8+
{
9+
10+
public async System.Threading.Tasks.Task<HttpResponse> Handle(IHttpRequest httpRequest, Func<System.Threading.Tasks.Task<HttpResponse>> next)
11+
{
12+
var stopWatch = Stopwatch.StartNew();
13+
var retVal = await next();
14+
Console.WriteLine(httpRequest.Uri + " : " + stopWatch.Elapsed);
15+
return retVal;
16+
}
17+
}
18+
}

‎uhttpsharp-demo/Program.cs‎

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,41 @@
1717
*/
1818

1919
using System;
20-
using System.Diagnostics;
21-
using System.IO.Ports;
22-
using System.Net;
2320
using System.Net.Sockets;
2421
using uhttpsharp;
22+
using uhttpsharp.Handlers;
23+
using uhttpsharpdemo.Handlers;
2524

2625
namespace uhttpsharpdemo
2726
{
2827
internal static class Program
2928
{
3029
private static void Main()
3130
{
32-
for (var port = 8000; port <= 65535; ++port)
33-
{
34-
var httpServer = new HttpServer(port);
35-
36-
httpServer.Use(new TimingHandler());
37-
httpServer.Use(new HttpRouter().With(string.Empty, new IndexHandler())
38-
.With("about", new AboutHandler()));
39-
httpServer.Use(new FileHandler());
40-
httpServer.Use(new ErrorHandler());
31+
log4net.Config.XmlConfigurator.Configure();
4132

42-
try
43-
{
44-
httpServer.Start();
45-
}
46-
catch (SocketException)
33+
for (var port = 8000; port <= 65535; port++)
34+
{
35+
using (var httpServer = new HttpServer(port, new HttpRequestProvider()))
4736
{
48-
continue;
37+
httpServer.Use(new TimingHandler());
38+
httpServer.Use(new HttpRouter().With(string.Empty, new IndexHandler())
39+
.With("about", new AboutHandler()));
40+
httpServer.Use(new FileHandler());
41+
httpServer.Use(new ErrorHandler());
42+
43+
try
44+
{
45+
httpServer.Start();
46+
Console.ReadLine();
47+
}
48+
catch (SocketException)
49+
{
50+
continue;
51+
}
4952
}
50-
break;
53+
return;
5154
}
52-
Console.ReadLine();
53-
}
54-
}
55-
56-
public class TimingHandler : IHttpRequestHandler
57-
{
58-
59-
public async System.Threading.Tasks.Task<HttpResponse> Handle(HttpRequest httpRequest, Func<System.Threading.Tasks.Task<HttpResponse>> next)
60-
{
61-
var stopWatch = Stopwatch.StartNew();
62-
var retVal = await next();
63-
Console.WriteLine(stopWatch.Elapsed);
64-
return retVal;
6555
}
6656
}
6757
}

‎uhttpsharp-demo/Properties/AssemblyInfo.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
using System.Reflection;
2020
using System.Runtime.InteropServices;
21-
2221
// General Information about an assembly is controlled through the following
2322
// set of attributes. Change these attribute values to modify the information
2423
// associated with an assembly.
24+
using System.Runtime.InteropServices.ComTypes;
25+
using log4net.Config;
2526

2627
[assembly: AssemblyTitle("µHttpSharp Demo")]
2728

‎uhttpsharp-demo/app.config‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3-
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
3+
4+
<configSections>
5+
<section name="log4net"
6+
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />
7+
</configSections>
8+
9+
<startup>
10+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
11+
</startup>
12+
13+
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
14+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
15+
16+
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
17+
<mapping>
18+
<level value="ERROR" />
19+
<foreColor value="White" />
20+
<backColor value="Red, HighIntensity" />
21+
</mapping>
22+
<layout type="log4net.Layout.PatternLayout">
23+
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
24+
</layout>
25+
</appender>
26+
27+
<root>
28+
<level value="DEBUG" />
29+
<appender-ref ref="ColoredConsoleAppender" />
30+
</root>
31+
32+
</log4net>
33+
34+
35+
</configuration>

‎uhttpsharp-demo/packages.config‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="log4net" version="2.0.3" targetFramework="net45" />
4+
</packages>

‎uhttpsharp-demo/uhttpsharp-demo.csproj‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
<Prefer32Bit>false</Prefer32Bit>
3838
</PropertyGroup>
3939
<ItemGroup>
40+
<Reference Include="log4net">
41+
<HintPath>..\packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath>
42+
</Reference>
4043
<Reference Include="System" />
4144
<Reference Include="System.Core" />
4245
<Reference Include="System.Xml.Linq" />
@@ -49,12 +52,12 @@
4952
<Compile Include="..\AssemblyCommon.cs">
5053
<Link>Properties\AssemblyCommon.cs</Link>
5154
</Compile>
52-
<Compile Include="AboutHandler.cs" />
53-
<Compile Include="ErrorHandler.cs" />
54-
<Compile Include="FileHandler.cs" />
55-
<Compile Include="IndexHandler.cs" />
55+
<Compile Include="Handlers\AboutHandler.cs" />
56+
<Compile Include="Handlers\ErrorHandler.cs" />
57+
<Compile Include="Handlers\IndexHandler.cs" />
5658
<Compile Include="Program.cs" />
5759
<Compile Include="Properties\AssemblyInfo.cs" />
60+
<Compile Include="Handlers\TimingHandler.cs" />
5861
</ItemGroup>
5962
<ItemGroup>
6063
<ProjectReference Include="..\uhttpsharp\uhttpsharp.csproj">
@@ -64,6 +67,7 @@
6467
</ItemGroup>
6568
<ItemGroup>
6669
<None Include="app.config" />
70+
<None Include="packages.config" />
6771
</ItemGroup>
6872
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6973
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Lines changed: 3 additions & 4 deletions

0 commit comments

Comments
 (0)