コード例 #1
0
		static public void Main(string[] args)
		{
			Core.Loop(async () =>
			{
				var Server = new HttpServer();
				var Router = new HttpRouter();

				Router.AddRoute("/test", async (Request, Response) =>
				{
					Response.Buffering = true;

					Response.SetHttpCode(HttpCode.OK_200);
					Response.Headers["Content-Type"] = "application/json";

					var MysqlClient = new MysqlClient("FEDORADEV", User: "******", Password: "******");
					await MysqlClient.ConnectAsync();

					foreach (var Row in await MysqlClient.QueryAsync("SELECT 1 as 'k1', 2 as 'k2', 3 * 999, 'test', 1 as 'Ok';"))
					{
						await Response.WriteAsync(Row.ToString());
					}

					await MysqlClient.CloseAsync();
				});

				// Default file serving

				Router.SetDefaultRoute(new HttpStaticFileServer(new LocalFileSystem(@"C:\projects\csharp\NodeNet\static"), Cache: true));

				Server.AddFilterLast(Router);
				await Server.ListenAsync(80, "127.0.0.1");
			});
		}
コード例 #2
0
		static void Main(string[] args)
		{
			Core.Loop(async () =>
			{
				var HttpRouter = new HttpRouter();
				{
					HttpRouter.AddRoute("/", async (Request, Response) =>
					{
						if (Request.Ssl)
						{
							Response.Buffering = true;
							await Response.WriteAsync("Hello World From Https!");
						}
						else
						{
							Response.Redirect("https://" + Request.Headers["HOST"] + ":443/", HttpCode.MOVED_PERMANENTLY_301);
						}
					});
				}

				var HttpsServer = new HttpsServer(
					PublicCertificateString: File.ReadAllText(@"certificate.cer"),
					PrivateKeyString: File.ReadAllText(@"private_key.key")
				);
				HttpsServer.AddFilterFirst(HttpRouter);

				var HttpServer = new HttpServer();
				HttpServer.AddFilterFirst(HttpRouter);
				
				await Task.WhenAll(
					HttpsServer.ListenAsync(443, "127.0.0.1"),
					HttpServer.ListenAsync(80, "127.0.0.1")
				);
			});
		}
コード例 #3
0
		static public void Main(string[] Args)
		{
			Core.Loop(async () =>
			{
				var Server = new HttpServer();
				var Router = new HttpRouter();

				Router.AddRoute("/test", async (Request, Response) =>
				{
					await Response.WriteAsync("Test!");
				});

				// Default file serving
				Router.SetDefaultRoute(new HttpStaticFileServer(Path: @"C:\projects\csharp\nodenet\NodeNet\static", Cache: true));

				Server.AddFilterLast(Router);
				await Server.ListenAsync(80, "127.0.0.1");
			});
		}
コード例 #4
0
		static public void Main(string[] Args)
		{
			Core.Loop(async () => 
			{
				var HttpServer = new HttpServer();
				var HttpRouter = new HttpRouter();

				// Creates a TemplateRendered that will read templates from memory (specified with Add method).
				var MyTemplateRenderer = await TemplateRenderer.CreateFromMemoryAsync(OutputGeneratedCode: true);

				// Adds a layout template
				MyTemplateRenderer.Add("_layout", @"
					<html>
						<head>
							<title>Title</title>
						</head>
						<body>
							<div>{% block Header %}Header{% endblock %}</div>
							<div>{% block Content %}Content{% endblock %}</div>
							<div>{% block Footer %}Footer{% endblock %}</div>
						</body>
					</html>
				".Trim());

				// Adds a template that extends _layout.
				MyTemplateRenderer.Add("test", @"
					{% extends '_layout' %}
					{% block Content %}
						<h1>Item List</h1>
						<ul>
						{% for Item in List %}
							<li>Name: {{ Item.Name }}, Age: {{ Item.Age }}</li>
						{% endfor %}
						</ul>
					{% endblock %}
				".Trim());

				HttpRouter.AddRoute("/", async (Request, Response) =>
				{
					Response.Buffering = true;

					await MyTemplateRenderer.WriteToAsync(
						Stream: Response,
						TemplateName: "test",
						Scope: new Dictionary<string, object>() 
						{
							{
								"List", new[] {
									new Item() { Name = "Hello", Age = 3 },
									new Item() { Name = "World", Age = 17 },
									new Item() { Name = "This", Age = 999 },
									new Item() { Name = "Is", Age = -1 },
									new Item() { Name = "A", Age = 0 },
									new Item() { Name = "Test", Age = 33 },
								}
							},
						}
					);
				});

				HttpServer.AddFilterLast(HttpRouter);
				await HttpServer.ListenAsync(80, "127.0.0.1");
			});
		}