Skip to content

Commit 985af62

Browse files
committed
Results site in .NET Core
1 parent 879e5bc commit 985af62

File tree

14 files changed

+165
-42
lines changed

14 files changed

+165
-42
lines changed

docker-compose-windows.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: "3.2"
2+
3+
services:
4+
vote:
5+
build:
6+
context: ./vote/dotnet
7+
ports:
8+
- "5000:80"
9+
depends_on:
10+
- message-queue
11+
12+
result:
13+
build:
14+
context: ./result/dotnet
15+
ports:
16+
- "5001:80"
17+
environment:
18+
- "Data:ConnectionString=Server=db;Port=4000;Database=votes;User=root;SslMode=None"
19+
depends_on:
20+
- db
21+
22+
worker:
23+
build:
24+
context: ./worker/dotnet
25+
environment:
26+
- "Data:ConnectionString=Server=db;Port=4000;Database=votes;User=root;SslMode=None"
27+
depends_on:
28+
- message-queue
29+
- db
30+
31+
message-queue:
32+
image: nats:nanoserver
33+
34+
db:
35+
image: dockersamples/tidb:nanoserver
36+
ports:
37+
- "3306:4000"
38+
39+
networks:
40+
default:
41+
external:
42+
name: nat

result/dotnet/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM microsoft/dotnet:2.1-sdk as builder
2+
3+
WORKDIR /Result
4+
COPY Result/Result.csproj .
5+
RUN dotnet restore
6+
7+
COPY /Result .
8+
RUN dotnet publish -c Release -o /out Result.csproj
9+
10+
# app image
11+
FROM microsoft/dotnet:2.1-aspnetcore-runtime
12+
13+
WORKDIR /app
14+
ENTRYPOINT ["dotnet", "Result.dll"]
15+
16+
COPY --from=builder /out .
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
using System.Threading.Tasks;
2-
using Microsoft.AspNetCore.SignalR;
3-
using Result.Models;
1+
using Microsoft.AspNetCore.SignalR;
42

53
namespace Result.Hubs
64
{
75
public class ResultsHub : Hub
86
{
9-
public async Task UpdateResults(ResultsModel results)
10-
{
11-
await Clients.All.SendAsync("UpdateResults", results);
12-
}
7+
//no public methods, only used for push from PublishRTesultsTimer
138
}
149
}

result/dotnet/Result/Models/ResultsModel.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,5 @@ public class ResultsModel
77
public int OptionB { get; set; }
88

99
public int VoteCount { get; set; }
10-
11-
public double OptionAPercent { get; set; }
12-
13-
public double OptionBPercent { get; set; }
1410
}
1511
}

result/dotnet/Result/Pages/Index.cshtml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
@page
2+
@model Result.Pages.IndexModel
3+
24
<!DOCTYPE html>
35
<html>
46
<head>
57
<meta charset="utf-8">
6-
<title>Cats vs Dogs -- Result</title>
8+
<title>@Model.OptionA vs @Model.OptionB -- Result</title>
79
<base href="/index.html">
810
<meta name="viewport" content="width=device-width, initial-scale = 1.0">
911
<meta name="keywords" content="docker-compose, docker, stack">
1012
<meta name="author" content="Docker">
11-
<link rel='stylesheet' href='/stylesheets/style.css' />
13+
<link rel='stylesheet' href='~/css/site.css' />
1214
</head>
1315
<body>
1416
<div id="background-stats">
@@ -22,22 +24,20 @@
2224
<div id="content-container">
2325
<div id="content-container-center">
2426
<div id="choice">
25-
<div class="choice cats">
26-
<div class="label">Cats</div>
27+
<div class="choice resulta">
28+
<div class="label">@Model.OptionA</div>
2729
<div class="stat" id="optionA"></div>
2830
</div>
2931
<div class="divider"></div>
30-
<div class="choice dogs">
31-
<div class="label">Dogs</div>
32+
<div class="choice resultb">
33+
<div class="label">@Model.OptionB</div>
3234
<div class="stat" id="optionB"></div>
3335
</div>
3436
</div>
3537
</div>
3638
</div>
3739
<div id="result">
38-
<span ng-if="total == 0">No votes yet</span>
39-
<span ng-if="total == 1">{{total}} vote</span>
40-
<span ng-if="total >= 2">{{total}} votes</span>
40+
<span id="totalVotes">No votes yet</span>
4141
</div>
4242
<script src="~/lib/signalr/dist/browser/signalr.js"></script>
4343
<script src="~/js/results.js"></script>

result/dotnet/Result/Pages/Index.cshtml.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,30 @@
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Mvc;
66
using Microsoft.AspNetCore.Mvc.RazorPages;
7+
using Microsoft.Extensions.Configuration;
78

89
namespace Result.Pages
910
{
1011
public class IndexModel : PageModel
1112
{
12-
public void OnGet()
13+
private string _optionA;
14+
private string _optionB;
15+
protected readonly IConfiguration _configuration;
16+
17+
public string OptionA { get; private set; }
18+
public string OptionB { get; private set; }
19+
20+
public IndexModel(IConfiguration configuration)
1321
{
22+
_configuration = configuration;
23+
_optionA = _configuration.GetValue<string>("Voting:OptionA");
24+
_optionB = _configuration.GetValue<string>("Voting:OptionB");
25+
}
1426

27+
public void OnGet()
28+
{
29+
OptionA = _optionA;
30+
OptionB = _optionB;
1531
}
1632
}
1733
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
{
1+
{
22
"iisSettings": {
3-
"windowsAuthentication": false,
4-
"anonymousAuthentication": true,
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
55
"iisExpress": {
66
"applicationUrl": "http://localhost:56785",
7-
"sslPort": 44369
7+
"sslPort": 0
88
}
99
},
1010
"profiles": {
@@ -18,10 +18,10 @@
1818
"Result": {
1919
"commandName": "Project",
2020
"launchBrowser": true,
21-
"applicationUrl": "https://localhost:5001;http://localhost:5000",
2221
"environmentVariables": {
2322
"ASPNETCORE_ENVIRONMENT": "Development"
24-
}
23+
},
24+
"applicationUrl": "https://localhost:5001;http://localhost:5000"
2525
}
2626
}
2727
}

result/dotnet/Result/Startup.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
51
using Microsoft.AspNetCore.Builder;
62
using Microsoft.AspNetCore.Hosting;
7-
using Microsoft.AspNetCore.Http;
8-
using Microsoft.AspNetCore.HttpsPolicy;
93
using Microsoft.AspNetCore.Mvc;
104
using Microsoft.Extensions.Configuration;
115
using Microsoft.Extensions.DependencyInjection;
126
using Result.Hubs;
7+
using Result.Timers;
138

149
namespace Result
1510
{
@@ -26,6 +21,7 @@ public void ConfigureServices(IServiceCollection services)
2621
{
2722
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
2823
services.AddSignalR();
24+
services.AddSingleton<PublishResultsTimer>();
2925
}
3026

3127
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
@@ -45,6 +41,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
4541
routes.MapHub<ResultsHub>("/resultsHub");
4642
});
4743
app.UseMvc();
44+
45+
var timer = app.ApplicationServices.GetService<PublishResultsTimer>();
46+
timer.Start();
4847
}
4948
}
5049
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Timers;
3+
using Microsoft.AspNetCore.SignalR;
4+
using Microsoft.Extensions.Configuration;
5+
using Result.Hubs;
6+
using Result.Models;
7+
8+
namespace Result.Timers
9+
{
10+
public class PublishResultsTimer
11+
{
12+
private readonly IHubContext<ResultsHub> _hubContext;
13+
private readonly Timer _timer;
14+
//TODO- temp
15+
private static Random _Random = new Random();
16+
17+
public PublishResultsTimer(IHubContext<ResultsHub> hubContext, IConfiguration configuration)
18+
{
19+
_hubContext = hubContext;
20+
var publishMilliseconds = configuration.GetValue<int>("ResultsTimer:PublishMilliseconds");
21+
_timer = new Timer(publishMilliseconds)
22+
{
23+
Enabled = false
24+
};
25+
_timer.Elapsed += PublishResults;
26+
}
27+
28+
public void Start()
29+
{
30+
if (!_timer.Enabled)
31+
{
32+
_timer.Start();
33+
}
34+
}
35+
36+
private void PublishResults(object sender, ElapsedEventArgs e)
37+
{
38+
var model = new ResultsModel
39+
{
40+
OptionA = _Random.Next(0, 100),
41+
OptionB = _Random.Next(0, 100)
42+
};
43+
model.VoteCount = model.OptionA + model.OptionB;
44+
_hubContext.Clients.All.SendAsync("UpdateResults", model);
45+
}
46+
}
47+
}

result/dotnet/Result/appsettings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
{
2+
"Voting": {
3+
"OptionA": "Cats",
4+
"OptionB": "Dogs"
5+
},
6+
"ResultsTimer": {
7+
"PublishMilliseconds": 1500
8+
},
29
"Logging": {
310
"LogLevel": {
411
"Default": "Warning"

result/dotnet/Result/wwwroot/css/site.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ body {
9595
text-transform: uppercase;
9696
}
9797

98-
#choice .choice.dogs {
98+
#choice .choice.resultb {
9999
color: #00cbca;
100100
float: right;
101101
}
102102

103-
#choice .choice.cats {
103+
#choice .choice.resulta {
104104
color: #2196f3;
105105
float: left;
106106
}

result/dotnet/Result/wwwroot/js/results.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@
33
var connection = new signalR.HubConnectionBuilder().withUrl("/resultsHub").build();
44

55
connection.on("UpdateResults", function (results) {
6-
data = JSON.parse(json);
6+
document.body.style.opacity=1;
77

8-
var a = parseInt(data.optionA || 0);
9-
var b = parseInt(data.optionB || 0);
8+
var a = parseInt(results.optionA || 0);
9+
var b = parseInt(results.optionB || 0);
1010
var percentages = getPercentages(a, b);
1111

1212
document.getElementById("optionA").innerText = percentages.a + "%";
1313
document.getElementById("optionB").innerText = percentages.b + "%";
14+
if (results.voteCount > 0) {
15+
var totalVotes = results.voteCount + (results.voteCount > 1 ? " votes" : " vote");
16+
document.getElementById("totalVotes").innerText = totalVotes;
17+
}
18+
19+
var bg1 = document.getElementById('background-stats-1');
20+
var bg2 = document.getElementById('background-stats-2');
21+
bg1.style.width = percentages.a + "%";
22+
bg2.style.width = percentages.b + "%";
1423
});
1524

1625
connection.start().catch(function (err) {

result/dotnet/Result/wwwroot/js/site.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

result/dotnet/Result/wwwroot/js/site.min.js

Whitespace-only changes.

0 commit comments

Comments
 (0)