add aspnet server
This commit is contained in:
1
AZKi Server/.gitignore
vendored
Normal file
1
AZKi Server/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
wwwroot/*
|
||||
18
AZKi Server/AZKi Server.csproj
Normal file
18
AZKi Server/AZKi Server.csproj
Normal file
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Grpc.AspNetCore" Version="2.64.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="3.5.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
32
AZKi Server/Models/BsonIdModelBinderProvider.cs
Normal file
32
AZKi Server/Models/BsonIdModelBinderProvider.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace AZKiServer.Models;
|
||||
|
||||
public class BsonIdModelBinderProvider : IModelBinderProvider
|
||||
{
|
||||
public IModelBinder? GetBinder(ModelBinderProviderContext context)
|
||||
{
|
||||
if (context.Metadata.ModelType == typeof(ObjectId))
|
||||
return new BsonIdModelBinder();
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public class BsonIdModelBinder : IModelBinder
|
||||
{
|
||||
public Task BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
|
||||
if (value == ValueProviderResult.None)
|
||||
return Task.CompletedTask;
|
||||
|
||||
if (ObjectId.TryParse(value.FirstValue, out var id))
|
||||
bindingContext.Result = ModelBindingResult.Success(id);
|
||||
else
|
||||
bindingContext.Result = ModelBindingResult.Failed();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
45
AZKi Server/Program.cs
Normal file
45
AZKi Server/Program.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using AZKiServer.Models;
|
||||
using AZKiServer.Services;
|
||||
|
||||
using MongoDB.Driver;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var config = builder.Configuration;
|
||||
|
||||
var dbString = config["DB_STRING"];
|
||||
var dbClient = new MongoClient(dbString);
|
||||
var db = dbClient.GetDatabase("AZKi");
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddSingleton(dbClient);
|
||||
builder.Services.AddSingleton<IMongoDatabase>(db);
|
||||
builder.Services.AddGrpc();
|
||||
builder.Services.AddControllers(opt => opt.ModelBinderProviders.Add(new BsonIdModelBinderProvider()));
|
||||
builder.Services.AddHostedService<FileScannerService>();
|
||||
builder.Services.AddTransient<MediaService>();
|
||||
|
||||
builder.Services.AddCors(o =>
|
||||
{
|
||||
o.AddPolicy("AllowAll", p =>
|
||||
{
|
||||
p.AllowAnyOrigin();
|
||||
p.AllowAnyMethod();
|
||||
p.AllowAnyHeader();
|
||||
});
|
||||
o.AddPolicy("RPC", p =>
|
||||
{
|
||||
p.AllowAnyMethod();
|
||||
p.AllowAnyHeader();
|
||||
p.WithExposedHeaders("Grpc-Status", "Grpc-Message", "Grpc-Encoding", "Grpc-Accept-Encoding");
|
||||
p.AllowAnyOrigin();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
//app.MapGrpcService<GreeterService>();
|
||||
app.MapFallbackToFile("index.html");
|
||||
|
||||
app.Run();
|
||||
23
AZKi Server/Properties/launchSettings.json
Normal file
23
AZKi Server/Properties/launchSettings.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5177",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7231;http://localhost:5177",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
AZKi Server/Protos/greet.proto
Normal file
21
AZKi Server/Protos/greet.proto
Normal file
@@ -0,0 +1,21 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option csharp_namespace = "AZKi_Server";
|
||||
|
||||
package greet;
|
||||
|
||||
// The greeting service definition.
|
||||
service Greeter {
|
||||
// Sends a greeting
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
// The request message containing the user's name.
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
// The response message containing the greetings.
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
16
AZKi Server/Services/FileScannerService.cs
Normal file
16
AZKi Server/Services/FileScannerService.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
namespace AZKiServer.Services;
|
||||
|
||||
public class FileScannerService : IHostedService
|
||||
{
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
22
AZKi Server/Services/GreeterService.cs
Normal file
22
AZKi Server/Services/GreeterService.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using AZKiServer;
|
||||
using Grpc.Core;
|
||||
|
||||
namespace AZKiServer.Services
|
||||
{
|
||||
public class GreeterService : Greeter.GreeterBase
|
||||
{
|
||||
private readonly ILogger<GreeterService> _logger;
|
||||
public GreeterService(ILogger<GreeterService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
|
||||
{
|
||||
return Task.FromResult(new HelloReply
|
||||
{
|
||||
Message = "Hello " + request.Name
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
6
AZKi Server/Services/MediaService.cs
Normal file
6
AZKi Server/Services/MediaService.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace AZKiServer.Services;
|
||||
|
||||
public class MediaService
|
||||
{
|
||||
|
||||
}
|
||||
8
AZKi Server/appsettings.Development.json
Normal file
8
AZKi Server/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
14
AZKi Server/appsettings.json
Normal file
14
AZKi Server/appsettings.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Kestrel": {
|
||||
"EndpointDefaults": {
|
||||
"Protocols": "Http2"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user