From 441bcc4e961adff2d7864815cd46ebedb2b11dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Henrique=20Ivanchechen?= Date: Sun, 19 Nov 2023 16:41:00 -0300 Subject: [PATCH] Adding multiple projects & API --- Docker/start.sh | 1 + Dockerfile | 9 ++++- Kasbot.API/Controllers/ReadinessController.cs | 22 +++++++++++ Kasbot.API/Controllers/StatusController.cs | 24 ++++++++++++ Kasbot.API/Dockerfile | 22 +++++++++++ Kasbot.API/Kasbot.API.csproj | 30 +++++++++++++++ Kasbot.API/Kasbot.API.csproj.user | 11 ++++++ Kasbot.API/Program.cs | 34 +++++++++++++++++ Kasbot.API/Properties/launchSettings.json | 38 +++++++++++++++++++ Kasbot.API/Services/StatusService.cs | 19 ++++++++++ Kasbot.API/appsettings.json | 9 +++++ Kasbot.APP/Internal/Services/StatusService.cs | 22 +++++++++++ Kasbot.APP/Kasbot.App.csproj | 11 ++++++ Kasbot.APP/Program.cs | 18 ++++++++- Kasbot.sln | 11 ++++++ Proto/status.proto | 19 ++++++++++ 16 files changed, 297 insertions(+), 3 deletions(-) create mode 100644 Docker/start.sh create mode 100644 Kasbot.API/Controllers/ReadinessController.cs create mode 100644 Kasbot.API/Controllers/StatusController.cs create mode 100644 Kasbot.API/Dockerfile create mode 100644 Kasbot.API/Kasbot.API.csproj create mode 100644 Kasbot.API/Kasbot.API.csproj.user create mode 100644 Kasbot.API/Program.cs create mode 100644 Kasbot.API/Properties/launchSettings.json create mode 100644 Kasbot.API/Services/StatusService.cs create mode 100644 Kasbot.API/appsettings.json create mode 100644 Kasbot.APP/Internal/Services/StatusService.cs create mode 100644 Proto/status.proto diff --git a/Docker/start.sh b/Docker/start.sh new file mode 100644 index 0000000..5bdf768 --- /dev/null +++ b/Docker/start.sh @@ -0,0 +1 @@ +dotnet Kasbot.App.dll & ; dotnet Kasbot.API.dll & ; wait \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 1134d44..44e5c4d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,7 @@ WORKDIR /App # Copy everything COPY ./Kasbot.APP ./Kasbot.APP +COPY ./Kasbot.API ./Kasbot.API COPY ./Kasbot.sln ./Kasbot.sln # Restore as distinct layers @@ -13,7 +14,13 @@ RUN dotnet build -c Release -o out # Build runtime image FROM mcr.microsoft.com/dotnet/aspnet:6.0 + RUN apt update && apt install -y ffmpeg libopus-dev opus-tools libsodium-dev WORKDIR /App + +COPY Docker/start.sh . +RUN chmod +x start.sh + COPY --from=build-env /App/out . -ENTRYPOINT ["dotnet", "Kasbot.App.dll"] \ No newline at end of file + +ENTRYPOINT ["./start.sh" ] \ No newline at end of file diff --git a/Kasbot.API/Controllers/ReadinessController.cs b/Kasbot.API/Controllers/ReadinessController.cs new file mode 100644 index 0000000..a9a298f --- /dev/null +++ b/Kasbot.API/Controllers/ReadinessController.cs @@ -0,0 +1,22 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Kasbot.API.Controllers +{ + [ApiController] + [Route("[controller]")] + public class ReadinessController : ControllerBase + { + private readonly ILogger logger; + + public ReadinessController(ILogger logger) + { + this.logger = logger; + } + + [HttpGet(Name = "readiness")] + public IActionResult GetReadiness() + { + return Ok(); + } + } +} \ No newline at end of file diff --git a/Kasbot.API/Controllers/StatusController.cs b/Kasbot.API/Controllers/StatusController.cs new file mode 100644 index 0000000..b726703 --- /dev/null +++ b/Kasbot.API/Controllers/StatusController.cs @@ -0,0 +1,24 @@ +using Kasbot.API.Services; +using Microsoft.AspNetCore.Mvc; + +namespace Kasbot.API.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class StatusController : Controller + { + private readonly StatusService statusService; + + public StatusController(StatusService statusService) + { + this.statusService = statusService; + } + + [HttpGet("ok")] + public async Task IsOk() + { + var result = await statusService.IsOk(); + return Ok(result); + } + } +} diff --git a/Kasbot.API/Dockerfile b/Kasbot.API/Dockerfile new file mode 100644 index 0000000..0a30d7e --- /dev/null +++ b/Kasbot.API/Dockerfile @@ -0,0 +1,22 @@ +#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base +WORKDIR /app +EXPOSE 80 +EXPOSE 443 + +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +WORKDIR /src +COPY ["Kasbot.API/Kasbot.API.csproj", "Kasbot.API/"] +RUN dotnet restore "Kasbot.API/Kasbot.API.csproj" +COPY . . +WORKDIR "/src/Kasbot.API" +RUN dotnet build "Kasbot.API.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "Kasbot.API.csproj" -c Release -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "Kasbot.API.dll"] \ No newline at end of file diff --git a/Kasbot.API/Kasbot.API.csproj b/Kasbot.API/Kasbot.API.csproj new file mode 100644 index 0000000..a8941dd --- /dev/null +++ b/Kasbot.API/Kasbot.API.csproj @@ -0,0 +1,30 @@ + + + + net6.0 + enable + enable + a8c1813f-fb5a-4771-868e-60e280ccaf77 + Linux + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + diff --git a/Kasbot.API/Kasbot.API.csproj.user b/Kasbot.API/Kasbot.API.csproj.user new file mode 100644 index 0000000..8981ce5 --- /dev/null +++ b/Kasbot.API/Kasbot.API.csproj.user @@ -0,0 +1,11 @@ + + + + Kasbot.API + MvcControllerEmptyScaffolder + root/Common/MVC/Controller + + + ProjectDebugger + + \ No newline at end of file diff --git a/Kasbot.API/Program.cs b/Kasbot.API/Program.cs new file mode 100644 index 0000000..1065ab9 --- /dev/null +++ b/Kasbot.API/Program.cs @@ -0,0 +1,34 @@ +using Kasbot.API.Services; + +internal class Program +{ + private static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + + builder.Services.AddControllers(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + builder.Services.AddScoped(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } + + app.UseHttpsRedirection(); + + app.UseAuthorization(); + + app.MapControllers(); + + app.Run(); + } +} diff --git a/Kasbot.API/Properties/launchSettings.json b/Kasbot.API/Properties/launchSettings.json new file mode 100644 index 0000000..c2eac26 --- /dev/null +++ b/Kasbot.API/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "profiles": { + "Kasbot.API": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "https://localhost:7174;http://localhost:5276" + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", + "publishAllPorts": true, + "useSSL": true + } + }, + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:47958", + "sslPort": 44359 + } + } +} \ No newline at end of file diff --git a/Kasbot.API/Services/StatusService.cs b/Kasbot.API/Services/StatusService.cs new file mode 100644 index 0000000..5b15a1c --- /dev/null +++ b/Kasbot.API/Services/StatusService.cs @@ -0,0 +1,19 @@ +using Status; +using Grpc.Net.Client; + +namespace Kasbot.API.Services +{ + public class StatusService + { + public StatusService() { } + + public async Task IsOk() + { + using var channel = GrpcChannel.ForAddress("https://localhost:7042"); + var client = new StatusRequester.StatusRequesterClient(channel); + + var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" }); + return reply.Message == "Hello GreeterClient"; + } + } +} diff --git a/Kasbot.API/appsettings.json b/Kasbot.API/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Kasbot.API/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Kasbot.APP/Internal/Services/StatusService.cs b/Kasbot.APP/Internal/Services/StatusService.cs new file mode 100644 index 0000000..9fd0959 --- /dev/null +++ b/Kasbot.APP/Internal/Services/StatusService.cs @@ -0,0 +1,22 @@ +using Grpc.Core; +using Microsoft.Extensions.Logging; +using Status; + +namespace Kasbot.App.Internal.Services +{ + public class StatusService : StatusRequester.StatusRequesterBase + { + private readonly ILogger _logger; + + public StatusService(ILoggerFactory loggerFactory) + { + _logger = loggerFactory.CreateLogger(); + } + + public override Task SayHello(HelloRequest request, ServerCallContext context) + { + _logger.LogInformation($"Sending hello to {request.Name}"); + return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); + } + } +} diff --git a/Kasbot.APP/Kasbot.App.csproj b/Kasbot.APP/Kasbot.App.csproj index 4e6c9f7..bde400d 100644 --- a/Kasbot.APP/Kasbot.App.csproj +++ b/Kasbot.APP/Kasbot.App.csproj @@ -15,8 +15,19 @@ + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/Kasbot.APP/Program.cs b/Kasbot.APP/Program.cs index 22448cf..bbe60d1 100644 --- a/Kasbot.APP/Program.cs +++ b/Kasbot.APP/Program.cs @@ -1,13 +1,16 @@ using Discord; using Discord.Commands; using Discord.WebSocket; +using Google.Protobuf.WellKnownTypes; +using Kasbot.App.Internal.Services; using Kasbot.Services; using Kasbot.Services.Internal; +using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; namespace Kasbot { - class Program + public class Program { private static string TOKEN = Environment.GetEnvironmentVariable("TOKEN"); private static int SHARDS = int.Parse(Environment.GetEnvironmentVariable("SHARDS") ?? "0"); @@ -24,17 +27,28 @@ namespace Kasbot SHARDS = 1; } + Task.Factory.StartNew(() => new Program().RunGrpc(args)); + new Program() .MainAsync() .GetAwaiter() .GetResult(); } + private void RunGrpc(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + builder.Services.AddGrpc(); + + var app = builder.Build(); + app.MapGrpcService(); + app.Run(url: "https://localhost:7042"); + } + public async Task MainAsync() { using (var services = ConfigureServices()) { - var client = services.GetRequiredService(); client.Log += LogAsync; diff --git a/Kasbot.sln b/Kasbot.sln index 8fe50f1..d3eecc3 100644 --- a/Kasbot.sln +++ b/Kasbot.sln @@ -5,6 +5,13 @@ VisualStudioVersion = 17.4.33213.308 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kasbot.App", "Kasbot.APP\Kasbot.App.csproj", "{BD715B63-0FC8-42F5-A449-9FF029B8E599}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kasbot.API", "Kasbot.API\Kasbot.API.csproj", "{9FCC6B19-185A-4A50-912B-40C1C8530EC5}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Proto", "Proto", "{3A0D4589-E8B2-4485-BEF7-2A6237D6E5BA}" + ProjectSection(SolutionItems) = preProject + status.proto = status.proto + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +22,10 @@ Global {BD715B63-0FC8-42F5-A449-9FF029B8E599}.Debug|Any CPU.Build.0 = Debug|Any CPU {BD715B63-0FC8-42F5-A449-9FF029B8E599}.Release|Any CPU.ActiveCfg = Release|Any CPU {BD715B63-0FC8-42F5-A449-9FF029B8E599}.Release|Any CPU.Build.0 = Release|Any CPU + {9FCC6B19-185A-4A50-912B-40C1C8530EC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9FCC6B19-185A-4A50-912B-40C1C8530EC5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9FCC6B19-185A-4A50-912B-40C1C8530EC5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9FCC6B19-185A-4A50-912B-40C1C8530EC5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Proto/status.proto b/Proto/status.proto new file mode 100644 index 0000000..dbcdbd9 --- /dev/null +++ b/Proto/status.proto @@ -0,0 +1,19 @@ +syntax = "proto3"; + +package status; + +// The greeting service definition. +service StatusRequester { + // 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; +} \ No newline at end of file