First version

This commit is contained in:
Michael Nolan
2022-04-02 16:15:20 -05:00
parent c144565f70
commit d9e66a1427
9 changed files with 654 additions and 79 deletions
+11 -3
View File
@@ -1,4 +1,6 @@
namespace Tesses.WebServer.ConsoleApp
using Tesses;
using Tesses.WebServer;
namespace Tesses.WebServer.ConsoleApp
{
class MainClass
@@ -6,8 +8,14 @@
public static void Main(string[] args)
{
var ip=System.Net.IPAddress.Any;
StaticServer server = new StaticServer("/home/ddlovato/Videos/");
HttpServerListener s = new HttpServerListener(new System.Net.IPEndPoint(ip, 24240),server);
StaticServer static_server = new StaticServer(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyVideos));
MountableServer mountable = new MountableServer(static_server);
mountable.Mount("/api/",new DynamicServer());
HttpServerListener s = new HttpServerListener(new System.Net.IPEndPoint(ip, 24240),mountable);
s.ListenAsync(System.Threading.CancellationToken.None).Wait();
}
}
+85
View File
@@ -0,0 +1,85 @@
using System;
using System.Text;
using System.Threading.Tasks;
namespace Tesses.WebServer.ConsoleApp
{
public class DynamicServer : Server
{
public DynamicServer()
{
}
Random rand = new Random();
int count = 0;
public override async Task GetAsync(ServerContext ctx)
{
//Console.WriteLine("HANDLE");
if(ctx.UrlPath=="/count")
{
count++;
await ctx.SendTextAsync($"This page has been viewed {count} times");
}
if(ctx.UrlPath=="/rand")
{
int min = 0;
int max = 65536;
int times = 5;
bool dont_show_hint = false;
if(ctx.QueryParams.ContainsKey("min"))
{
if(!int.TryParse(ctx.QueryParams.GetFirst("min"),out min))
{
min = 0;
}
else
{
dont_show_hint = true;
}
}
if (ctx.QueryParams.ContainsKey("max"))
{
if (!int.TryParse(ctx.QueryParams.GetFirst("max"), out max))
{
max = 65536;
}
else
{
dont_show_hint = true;
}
}
if (ctx.QueryParams.ContainsKey("times"))
{
if (!int.TryParse(ctx.QueryParams.GetFirst("times"), out times))
{
times = 5;
}
else
{
dont_show_hint = true;
}
}
max++;
StringBuilder html = new StringBuilder();
html.Append("<html><head><title>Random Numbers</title></head><body><h1>Random Numbers</h1>");
if(!dont_show_hint)
{
string hint = "Hint: <a href=\"./rand?min=41&max=1992&times=42\">./rand?min=41&max=1992&times=42</a><br>";
html.Append(hint);
}
html.Append(rand.Next(min, max));
for(int i = 1;i<times;i++)
{
html.Append($", {rand.Next(min, max)}");
}
html.Append("</body></html>");
await ctx.SendTextAsync(html.ToString());
}
}
}
}
@@ -40,6 +40,7 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Server.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tesses.WebServer\Tesses.WebServer.csproj">