-
Notifications
You must be signed in to change notification settings - Fork 4
/
WebServer.cs
78 lines (67 loc) · 3.3 KB
/
WebServer.cs
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
using Newtonsoft.Json;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
namespace BlockChainDemo
{
public class WebServer
{
public WebServer(BlockChain chain)
{
var settings = ConfigurationManager.AppSettings;
string host = settings["host"]?.Length > 1 ? settings["host"] : "localhost";
string port = settings["port"]?.Length > 1 ? settings["port"] : "12345";
var server = new TinyWebServer.WebServer(request =>
{
string path = request.Url.PathAndQuery.ToLower();
string query = "";
string json = "";
if (path.Contains("?"))
{
string[] parts = path.Split('?');
path = parts[0];
query = parts[1];
}
switch (path)
{
//GET: http://localhost:12345/mine
case "/mine":
return chain.Mine();
//POST: http://localhost:12345/transactions/new
//{ "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" }
case "/transactions/new":
if (request.HttpMethod != HttpMethod.Post.Method)
return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}";
json = new StreamReader(request.InputStream).ReadToEnd();
Transaction trx = JsonConvert.DeserializeObject<Transaction>(json);
int blockId = chain.CreateTransaction(trx.Sender, trx.Recipient, trx.Amount);
return $"Your transaction will be included in block {blockId}";
//GET: http://localhost:12345/chain
case "/chain":
return chain.GetFullChain();
//POST: http://localhost:12345/nodes/register
//{ "Urls": ["localhost:54321", "localhost:54345", "localhost:12321"] }
case "/nodes/register":
if (request.HttpMethod != HttpMethod.Post.Method)
return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}";
json = new StreamReader(request.InputStream).ReadToEnd();
var urlList = new { Urls = new string[0] };
var obj = JsonConvert.DeserializeAnonymousType(json, urlList);
return chain.RegisterNodes(obj.Urls);
//GET: http://localhost:12345/nodes/resolve
case "/nodes/resolve":
return chain.Consensus();
}
return "";
},
$"http://{host}:{port}/mine/",
$"http://{host}:{port}/transactions/new/",
$"http://{host}:{port}/chain/",
$"http://{host}:{port}/nodes/register/",
$"http://{host}:{port}/nodes/resolve/"
);
server.Run();
}
}
}