using System.Drawing; using Tesses; using Tesses.WebServer; using Tesses.WebServer.HtmlLayout; namespace Tesses.WebServer.ConsoleApp { class JsonObj { public string Name {get;set;}=""; public DateTime Birthday {get;set;}=DateTime.Now; } public class MyOther { [FormNewLine] public string Hello {get;set;}=""; [FormNewLine] public Color FavoriteColor {get;set;}=Color.Pink; // [FormNewLine] //public HttpFileResponseEntry[] Files {get;set;}=new HttpFileResponseEntry[0]; } public enum TestEnum { Apple, Orange, Grape, Banana, Raspberry, Blueberry, Strawberry } class Test { [FormNewLine] [FormText("Your Name",Placeholder="Name",Name="name")] public string Name {get;set;}=""; [FormNewLine] [FormText("Describe yourself",Placeholder="Description",Name="description")] public string Description {get;set;}=""; [FormNewLine] [FormCheckbox("Are you an adult")] public bool Adult {get;set;}=false; [FormNewLine] [FormCheckbox("Email Me")] public bool EmailMe {get;set;}=true; [FormNewLine] [FormRadio("fruit")] public TestEnum Fruit {get;set;}=TestEnum.Raspberry; [FormNewLine] [FormFieldSet] public MyOther MyOther {get;set;}=new MyOther(); } 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("/john",async(ctx)=>{ Test other = new Test(); ctx.ParseSmartForm(other); await ctx.SendJsonAsync(other); },"POST"); rserver.Add("/html_ex",async(ctx)=>{await ctx.SendHtmlAsync(H.Html( H.Head( H.Meta().WithAttribute("charset","UTF-8"), H.Meta().WithAttribute("name","viewport").WithAttribute("content","width=device-width, initial-scale=1.0"), H.Title("Document") ), H.Body(H.Form("./john",new Test(),true)) ).WithAttribute("lang","en"));}); rserver.Add("/absolute_paths",(ctx)=>{ using(var sw = ctx.GetResponseStreamWriter()) { sw.WriteLine($"Root: {ctx.GetRealRootUrl()}"); sw.WriteLine($"Current Server Root: {ctx.GetCurrentServerPath()}"); sw.WriteLine($"Demetria: {ctx.GetRealUrlRelativeToCurrentServer("/page")}"); sw.WriteLine($"Headers: {ctx.GetRealUrlRelativeToCurrentServer("/headers")}"); sw.WriteLine($"Relative To Root: {ctx.GetRealUrl("/johnconnor/")}"); } }); rserver.Add("/headers",(ctx)=>{ using(var sw = ctx.GetResponseStreamWriter()) { foreach(var item in ctx.RequestHeaders) { foreach(var item2 in item.Value) { sw.WriteLine($"{item.Key}: {item2}"); } } } }); rserver.Add("/jsonEndpoint",async(ctx)=>{ var res=await ctx.ReadJsonAsync(); 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 } } }