tesses.webserver/Tesses.WebServer.Console/Program.cs

84 lines
3.4 KiB
C#

using Tesses;
using Tesses.WebServer;
namespace Tesses.WebServer.ConsoleApp
{
class JsonObj
{
public string Name {get;set;}="";
public DateTime Birthday {get;set;}=DateTime.Now;
}
class MainClass
{
public static void Main(string[] args)
{
TestObject some_object = new TestObject();
RouteServer rserver = new RouteServer();
rserver.Add("/", async(ctx) => {
await ctx.SendJsonAsync(some_object);
});
rserver.Add("/page", async(ctx) => {
await ctx.SendTextAsync("Demetria Devonne Lovato 8/20/1992");
});
rserver.Add("/jsonEndpoint",async(ctx)=>{
var res=await ctx.ReadJsonAsync<JsonObj>();
if(res !=null)
{
Console.WriteLine($"Name: {res.Name}");
Console.WriteLine($"Birthday: {res.Birthday.ToShortDateString()}");
await ctx.SendTextAsync("The meaning of life is 42","text/plain");
}
});
rserver.Add("/typewriter",(ctx)=>{
using(var sw=ctx.GetResponseStreamWriter("text/plain"))
{
foreach(var c in "This is a typewriter\nwell this is cool\nThis is thanks to the chunked stream.")
{
sw.Write(c);
sw.Flush();
System.Threading.Thread.Sleep(50);
}
}
});
var ip=System.Net.IPAddress.Any;
StaticServer static_server = new StaticServer(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyVideos));
MountableServer mountable = new MountableServer(static_server);
mountable.Mount("/api/",new DynamicServer());
BasicAuthServer basicAuth = new BasicAuthServer((user, pass) => { return user == "demi" && pass == "password123"; }, rserver, "RouteServer"); //bad pasword I know, This is a sample
mountable.Mount("/api/route/",basicAuth);
HttpServerListener s = new HttpServerListener(new System.Net.IPEndPoint(ip, 24240),mountable);
/*
So this sample application
Route Server (Like dajuric/simple-http's routes (uses modified code from that project))
(In this example It is password protected, Username: "demi", Password: "password123")
I know password123 is a bad password (but its ok for this sample project)
/api/route/page: shows authors favorite artist and the birthday
/api/route/: shows authors name, birthday, gender
Dynamic Server (native api)
/api/rand: shows how you can use query params
/api/count: counts up every time you go to it
everything else is files in My Videos
*/
s.ListenAsync(System.Threading.CancellationToken.None).Wait();
}
public class TestObject
{
public string name => "Mike Nolan";
public int month => 12;
public int day => 2;
public int year => 2000;
public string gender => "Male"; //duh
}
}
}