-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdotnet.yml
72 lines (64 loc) · 2.48 KB
/
dotnet.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
source: clear
features:
- dotnet latest
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT dotnet run
commands:
- dotnet new console --name App
- filename: Program.cs
content: |
using System.Text;
using System.Net;
class HttpServer
{
public static HttpListener listener = new();
public static string pageBody = @"
<!DOCTYPE html>
<html>
<head>
<title>DotNet App</title>
<link rel=""stylesheet"" href=""//unpkg.com/bootstrap/dist/css/bootstrap.min.css"">
</head>
<body class=""p-5 text-center"">
<p><img src=""//images.unsplash.com/photo-1465153690352-10c1b29577f8?fit=crop&w=200&h=200""
class=""img-fluid rounded-circle""></p>
<h1 class=""mb-3"">Hello, world!</h1>
<p>Serving from DotNet version {0}</p>
<p class=""text-muted"">DOM Cloud</p>
</body>
</html>
";
public static async Task HandleIncomingConnections()
{
string version = Environment.Version.ToString();
byte[] pageData = Encoding.UTF8.GetBytes(string.Format(pageBody, version));
while (true)
{
// Will wait here until we hear from a connection
HttpListenerContext ctx = await listener.GetContextAsync();
// Peel out the requests and response objects
HttpListenerResponse resp = ctx.Response;
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = pageData.LongLength;
await resp.OutputStream.WriteAsync(pageData);
resp.Close();
}
}
public static void Main(string[] args)
{
// Create a Http server and start listening for incoming connections
string? port = Environment.GetEnvironmentVariable("PORT") ?? "3000";
listener.Prefixes.Add($"http://localhost:{port}/");
listener.Start();
Console.WriteLine("Listening for connections on localhost:{0}", port);
// Handle requests
Task listenTask = HandleIncomingConnections();
listenTask.GetAwaiter().GetResult();
// Close the listener
listener.Close();
}
}