tesses-webserver-extras/Tesses.WebServer.SwagmeTest/Program.cs

50 lines
2.2 KiB
C#

using Tesses.WebServer;
using Tesses.WebServer.Swagme;
int value=0;
SwagmeServer svr=new SwagmeServer();
svr.AbsoluteUrl=true;
svr.Add("/Progress",async (e)=>{
await e.SendJsonAsync(new{Progress=50});
},new SwagmeDocumentation("Get Progress","Some progress in <a href=\"https://json.org/\">json</a>"),group: "Getting Values");
svr.Add("/GetValue",async (e)=>{
await e.SendJsonAsync(new{num=value});
},new SwagmeDocumentation("Get Value","Value from <a href=\"./SetValue?num=42\">SetValue?num=42</a> in <a href=\"https://json.org/\">json</a>"),group: "Getting Values");
svr.Add("/SetValue",(e)=>{
string _va;
if(e.QueryParams.TryGetFirst("num",out _va))
{
int.TryParse(_va,out value);
}
},new SwagmeDocumentation("/SetValue?num=42","Set a number","Set an integer in program<br><br><ul><li>num: <i>an integer</i></li></ul>"),group: "Setting Values");
svr.Add("/SetValue",(e)=>{
e.ParseBody();
string _va;
if(e.QueryParams.TryGetFirst("num",out _va))
{
int.TryParse(_va,out value);
}
},new SwagmeDocumentation("/SetValue?num=42","Set a number","Set an integer in program<br><br><ul><li>num: <i>an integer</i></li></ul>"),"POST",group: "Setting Values");
svr.Add("/JsonEx",async(ctx)=>{
var req= await ctx.ReadJsonAsync<ExampleRequest>();
string myMessage = $"{req.Name} {req.BeforeNumber} {req.NumberOfTimes} {req.AfterNumber}";
ExampleResponse resp=new ExampleResponse();
resp.Number = req.NumberOfTimes;
resp.YourMessage = myMessage;
await ctx.SendJsonAsync(resp);
},new SwagmeDocumentation("/JsonEx","Json Example Request Response",new ExampleRequest(),new ExampleResponse()),"POST","Json");
RouteServer rsvr=new RouteServer();
rsvr.Add("/",(e)=>{
e.SendText("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>Swagme Test</title></head><body><h1>Swagme Test</h1><a href=\"./api/\">Api</a></body></html>");
});
MountableServer mount =new MountableServer(rsvr);
mount.Mount("/api/",svr);
mount.StartServer(4202);