Skip to content

Commit 4ca0734

Browse files
committed
feature: 添加服务端代码示例
1 parent 3826197 commit 4ca0734

File tree

6 files changed

+135
-1
lines changed

6 files changed

+135
-1
lines changed

src/Server/.idea/.idea.ServerSample/.idea/.gitignore

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Server/.idea/.idea.ServerSample/.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Server/.idea/.idea.ServerSample/.idea/indexLayout.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Server/.idea/.idea.ServerSample/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Server/Program.cs

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,108 @@
1+
using GeneralUpdate.AspNetCore.DTO;
2+
using GeneralUpdate.AspNetCore.Hubs;
3+
using GeneralUpdate.AspNetCore.Services;
4+
using GeneralUpdate.Core.Domain.DTO;
5+
using GeneralUpdate.Core.Domain.Enum;
6+
using Microsoft.AspNetCore.SignalR;
7+
using Newtonsoft.Json;
8+
19
var builder = WebApplication.CreateBuilder(args);
10+
builder.Services.AddSingleton<IUpdateService, GeneralUpdateService>();
11+
builder.Services.AddSignalR();
212
var app = builder.Build();
313

4-
app.MapGet("/", () => "Hello World!");
14+
/**
15+
* Push the latest version information in real time.
16+
*/
17+
app.MapHub<VersionHub>("/versionhub");
18+
19+
app.MapPost("/push", async Task<string> (HttpContext context) =>
20+
{
21+
try
22+
{
23+
var hubContext = context.RequestServices.GetRequiredService<IHubContext<VersionHub>>();
24+
await hubContext.SendMessage("TESTNAME", "123");
25+
}
26+
catch (Exception ex)
27+
{
28+
return ex.Message;
29+
}
30+
return "OK";
31+
});
32+
33+
/**
34+
* Check if an update is required.
35+
*/
36+
app.MapGet("/versions/{clientType}/{clientVersion}/{clientAppKey}", (int clientType, string clientVersion, string clientAppKey, IUpdateService updateService) =>
37+
{
38+
var versions = new List<VersionDTO>();
39+
var md5 = "b03d52c279faf003965c46041f2037f9";
40+
var pubTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
41+
string version = null;
42+
if (clientType == AppType.ClientApp)
43+
{
44+
//client
45+
//version = "0.0.0.0";
46+
version = "9.9.9.9";
47+
}
48+
else if (clientType == AppType.UpgradeApp)
49+
{
50+
//upgrad
51+
//version = "0.0.0.0";
52+
version = "9.9.9.9";
53+
}
54+
var url = $"http://192.168.50.203/testpacket.zip";
55+
var name = "testpacket";
56+
versions.Add(new VersionDTO(md5, pubTime, version, url, name));
57+
return updateService.Update(clientType, clientVersion, version, clientAppKey, GetAppSecretKey(), false, versions);
58+
});
59+
60+
/**
61+
* Upload update package.
62+
*/
63+
app.MapPost("/upload", async Task<string> (HttpContext context, HttpRequest request) =>
64+
{
65+
var uploadReapDTO = new UploadReapDTO();
66+
try
67+
{
68+
var contextReq = context.Request;
69+
int.TryParse(contextReq.Form["clientType"], out int clientType);
70+
var version = contextReq.Form["clientType"].ToString();
71+
var clientAppKey = contextReq.Form["clientAppKey"].ToString();
72+
var md5 = contextReq.Form["md5"].ToString();
73+
74+
if (!request.HasFormContentType) throw new Exception("ContentType was not included in the request !");
75+
var form = await request.ReadFormAsync();
76+
77+
var formFile = form.Files["file"];
78+
if (formFile is null || formFile.Length == 0) throw new ArgumentNullException("Uploaded update package file not found !");
79+
await using var stream = formFile.OpenReadStream();
80+
byte[] buffer = new byte[stream.Length];
81+
stream.Read(buffer, 0, buffer.Length);
82+
//TODO:save to file server.
83+
string localPath = $"E:\\{formFile.FileName}";
84+
await using var fileStream = new FileStream(localPath, FileMode.CreateNew, FileAccess.Write);
85+
fileStream.Write(buffer, 0, buffer.Length);
86+
87+
//TODO: data persistence.To mysql , sqlserver....
88+
89+
uploadReapDTO.Code = HttpStatus.OK;
90+
uploadReapDTO.Body = "Published successfully.";
91+
uploadReapDTO.Message = RespMessage.RequestSucceeded;
92+
return JsonConvert.SerializeObject(uploadReapDTO);
93+
}
94+
catch (Exception ex)
95+
{
96+
uploadReapDTO.Code = HttpStatus.BAD_REQUEST;
97+
uploadReapDTO.Body = $"Failed to publish ! Because : {ex.Message}";
98+
uploadReapDTO.Message = RespMessage.RequestFailed;
99+
return JsonConvert.SerializeObject(uploadReapDTO);
100+
}
101+
});
5102

6103
app.Run();
104+
105+
string GetAppSecretKey()
106+
{
107+
return "B8A7FADD-386C-46B0-B283-C9F963420C7C";
108+
}

src/Server/ServerSample.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<PackageReference Include="GeneralUpdate.AspNetCore" Version="1.4.1" />
11+
</ItemGroup>
12+
913
</Project>

0 commit comments

Comments
 (0)