-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
99 lines (80 loc) · 3.01 KB
/
Copy pathProgram.cs
File metadata and controls
99 lines (80 loc) · 3.01 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using Npgsql;
class Program
{
public static HttpListener listener;
public static string url = "http://*:8000/";
public static int pageViews = 0;
public static int requestCount = 0;
public static string pageData =
"<!DOCTYPE>" +
"<html>" +
" <head>" +
" <title>HttpListener Example</title>" +
" </head>" +
" <body>" +
" <p>Welcome to .NET Server (c) Ignat Strelets</p>" +
" </body>" +
"</html>";
public static async Task HandleIncomingConnections()
{
bool runServer = true;
while (runServer)
{
HttpListenerContext ctx = await listener.GetContextAsync();
HttpListenerRequest req = ctx.Request;
HttpListenerResponse resp = ctx.Response;
Console.WriteLine("Request #: {0}", ++requestCount);
Console.WriteLine(req.Url.ToString());
Console.WriteLine(req.HttpMethod);
Console.WriteLine(req.UserHostName);
Console.WriteLine(req.UserAgent);
Console.WriteLine();
if (req.Url.AbsolutePath != "/favicon.ico")
pageViews += 1;
string disableSubmit = !runServer ? "disabled" : "";
byte[] data = Encoding.UTF8.GetBytes(String.Format(pageData, pageViews, disableSubmit));
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = data.LongLength;
await resp.OutputStream.WriteAsync(data, 0, data.Length);
resp.Close();
}
}
static void Main(string[] args)
{
string dbHost = Environment.GetEnvironmentVariable("DB_HOST");
string dbUsername = Environment.GetEnvironmentVariable("DB_USERNAME");
string dbPassword = Environment.GetEnvironmentVariable("DB_PASSWORD");
string dbName = Environment.GetEnvironmentVariable("DB_NAME");
string connectionString = string.Format("Host={0};Port=5432;Username={1};Password={2};Database={3};",dbHost,dbUsername,dbPassword,dbName);
NpgsqlConnection connection = new NpgsqlConnection(connectionString);
var sql = "SELECT version()";
using var cmd = new NpgsqlCommand(sql, connection);
try
{
connection.Open();
Console.WriteLine("Connected to PostgreSQL!");
Console.WriteLine("Postgres Version:");
Console.WriteLine(connection.PostgreSqlVersion);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
connection.Close();
}
listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Start();
Console.WriteLine("Listening for connections on {0}", url);
Task listenTask = HandleIncomingConnections();
listenTask.GetAwaiter().GetResult();
}
}