Skip to content
This repository was archived by the owner on Jun 25, 2020. It is now read-only.

Commit 54024d8

Browse files
author
Jérémie Bertrand
committed
Merge pull request #298 from hartez/nowin-webhost
Replace Firefly with Nowin
2 parents 1e49a17 + 56e2d3e commit 54024d8

File tree

3 files changed

+81
-54
lines changed

3 files changed

+81
-54
lines changed

src/Pretzel/Pretzel.csproj

+11-2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@
4242
<Reference Include="DotLiquid">
4343
<HintPath>..\..\libs\DotLiquid.dll</HintPath>
4444
</Reference>
45-
<Reference Include="Firefly">
46-
<HintPath>..\packages\Firefly.0.6.3\lib\Firefly.dll</HintPath>
45+
<Reference Include="Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
46+
<HintPath>..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
47+
<Private>True</Private>
48+
</Reference>
49+
<Reference Include="Microsoft.Owin.Hosting, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
50+
<HintPath>..\packages\Microsoft.Owin.Hosting.3.0.1\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
51+
<Private>True</Private>
4752
</Reference>
4853
<Reference Include="NDesk.Options">
4954
<HintPath>..\packages\NDesk.Options.0.2.1\lib\NDesk.Options.dll</HintPath>
5055
</Reference>
56+
<Reference Include="Nowin, Version=0.22.2.0, Culture=neutral, processorArchitecture=MSIL">
57+
<HintPath>..\packages\Nowin.0.22.2.0\lib\net45\Nowin.dll</HintPath>
58+
<Private>True</Private>
59+
</Reference>
5160
<Reference Include="Owin">
5261
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
5362
</Reference>

src/Pretzel/WebHost/WebHost.cs

+67-52
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Globalization;
43
using System.IO;
54
using System.Threading.Tasks;
6-
using Firefly.Http;
75
using Gate;
86
using Pretzel.Logic.Extensions;
7+
using Owin;
8+
using Microsoft.Owin.Hosting;
99

1010
namespace Pretzel
1111
{
@@ -39,77 +39,92 @@ public bool Start()
3939
return false;
4040
}
4141

42-
var server = new ServerFactory();
43-
host = server.Create(NewServerCallback, Port);
42+
var options = new StartOptions
43+
{
44+
ServerFactory = "Nowin",
45+
Port = Port
46+
};
47+
48+
Startup.Content = content;
49+
host = WebApp.Start<Startup>(options);
4450

4551
IsRunning = true;
4652

4753
return true;
4854
}
4955

50-
public bool Stop()
56+
public class Startup
5157
{
52-
if (!IsRunning)
53-
{
54-
return false;
55-
}
56-
57-
host.Dispose();
58-
host = null;
58+
public static IWebContent Content { get; set; }
5959

60-
return true;
61-
}
62-
63-
Task NewServerCallback(IDictionary<string, object> env)
64-
{
65-
var path = (string)env[OwinConstants.RequestPath];
60+
public void Configuration(IAppBuilder app)
61+
{
62+
app.Run(context =>
63+
{
64+
var path = context.Request.Path.Value;
65+
var env = context.Request.Environment;
6666

67-
Tracing.Info(path);
67+
Tracing.Info(path);
6868

69-
if (!content.IsAvailable(path))
70-
{
71-
var path404 = "/404.html";
72-
var use404 = content.IsAvailable(path404);
69+
if (!Content.IsAvailable(path))
70+
{
71+
var path404 = "/404.html";
72+
var use404 = Content.IsAvailable(path404);
73+
74+
var response = new Response(env) { ContentType = use404 ? path404.MimeType() : path.MimeType() };
75+
using (var writer = new StreamWriter(response.OutputStream))
76+
{
77+
if (use404)
78+
{
79+
writer.Write(Content.GetContent(path404));
80+
}
81+
else
82+
{
83+
writer.Write("Page not found: " + path);
84+
}
85+
}
86+
return TaskHelpers.Completed();
87+
}
7388

74-
var response = new Response(env) { ContentType = use404 ? path404.MimeType() : path.MimeType() };
75-
using (var writer = new StreamWriter(response.OutputStream))
76-
{
77-
if (use404)
89+
if (path.MimeType().IsBinaryMime())
90+
{
91+
var fileContents = Content.GetBinaryContent(path);
92+
var response = new Response(env) { ContentType = path.MimeType() };
93+
response.Headers["Content-Range"] = new[] { string.Format("bytes 0-{0}", (fileContents.Length - 1)) };
94+
response.Headers["Content-Length"] = new[] { fileContents.Length.ToString(CultureInfo.InvariantCulture) };
95+
response.Write(new ArraySegment<byte>(fileContents));
96+
}
97+
else if (Content.IsDirectory(path) && !path.EndsWith("/"))
7898
{
79-
writer.Write(content.GetContent(path404));
99+
// if path is a directory without trailing slash, redirects to the same url with a trailing slash
100+
var response = new Response(env) { Status = "301 Moved Permanently" };
101+
response.Headers["Location"] = new[] { String.Format("http://localhost:{0}{1}/", context.Request.LocalPort, path) };
80102
}
81103
else
82104
{
83-
writer.Write("Page not found: " + path);
105+
var response = new Response(env) { ContentType = path.MimeType() };
106+
using (var writer = new StreamWriter(response.OutputStream))
107+
{
108+
writer.Write(Content.GetContent(path));
109+
}
84110
}
85-
}
86-
return TaskHelpers.Completed();
111+
112+
return Task.Delay(0);
113+
});
87114
}
115+
}
88116

89-
if (path.MimeType().IsBinaryMime())
90-
{
91-
var fileContents = content.GetBinaryContent(path);
92-
var response = new Response(env) { ContentType = path.MimeType() };
93-
response.Headers["Content-Range"] = new[] { string.Format("bytes 0-{0}", (fileContents.Length - 1)) };
94-
response.Headers["Content-Length"] = new[] { fileContents.Length.ToString(CultureInfo.InvariantCulture) };
95-
response.Write(new ArraySegment<byte>(fileContents));
96-
}
97-
else if (content.IsDirectory(path) && !path.EndsWith("/"))
98-
{
99-
//if path is a directory without trailing slash, redirects to the same url with a trailing slash
100-
var response = new Response(env) { Status = "301 Moved Permanently" };
101-
response.Headers["Location"] = new[] { String.Format("http://localhost:{0}{1}/", Port, path) };
102-
}
103-
else
117+
public bool Stop()
118+
{
119+
if (!IsRunning)
104120
{
105-
var response = new Response(env) { ContentType = path.MimeType() };
106-
using (var writer = new StreamWriter(response.OutputStream))
107-
{
108-
writer.Write(content.GetContent(path));
109-
}
121+
return false;
110122
}
111123

112-
return TaskHelpers.Completed();
124+
host.Dispose();
125+
host = null;
126+
127+
return true;
113128
}
114129

115130
#region IDisposable

src/Pretzel/packages.config

+3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
<package id="Gate.Middleware.Sources" version="0.27" targetFramework="net4" />
66
<package id="Gate.Sources" version="0.27" targetFramework="net4" />
77
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
8+
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" />
9+
<package id="Microsoft.Owin.Hosting" version="3.0.1" targetFramework="net45" />
810
<package id="NDesk.Options" version="0.2.1" />
11+
<package id="Nowin" version="0.22.2.0" targetFramework="net45" />
912
<package id="Owin" version="1.0" targetFramework="net4" />
1013
<package id="Owin.Builder" version="0.8.5" targetFramework="net4" />
1114
<package id="Owin.Extensions.Sources" version="0.8.5" targetFramework="net4" />

0 commit comments

Comments
 (0)