From 09bcf505cb262b1bf28c6b2958f514d8053b5722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=A8=D0=B8?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD=D1=81=D0=BA=D0=B8=D0=B9?= Date: Tue, 19 Dec 2023 20:24:16 +0300 Subject: [PATCH] add args parser --- Dockerfile | 7 +++++-- Pages/Authorize.cshtml | 2 +- Program.cs | 11 ++++++++++- Startup.cs | 22 ++++++++++------------ appsettings.Development.json | 5 ++++- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index 41de5f0..b30358a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,7 @@ #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. +ARG CLIENTID +ARG CLIENTSECRET +ARG REDIRECTURL FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base WORKDIR /app @@ -11,10 +14,10 @@ COPY ["yaflay.ru.csproj", "."] RUN dotnet restore "./yaflay.ru.csproj" COPY . . WORKDIR "/src/." -RUN dotnet build "yaflay.ru.csproj" -c Release -o /app/build +RUN dotnet build "yaflay.ru.csproj" -c Release -o /app/build FROM build AS publish -RUN dotnet publish "yaflay.ru.csproj" -c Release -o /app/publish /p:UseAppHost=false +RUN dotnet publish "yaflay.ru.csproj" -c Release -o /app/publish /p:UseAppHost=false;redirectUrl=$REDIRECTURL;clientId=$CLIENTID;clientSecret=$CLIENTSECRET FROM base AS final WORKDIR /app diff --git a/Pages/Authorize.cshtml b/Pages/Authorize.cshtml index eaa758a..f4659f3 100644 --- a/Pages/Authorize.cshtml +++ b/Pages/Authorize.cshtml @@ -42,7 +42,7 @@ new HttpRequestMessage(HttpMethod.Post, "https://discordapp.com/api/oauth2/token")) { requestMessage.Content = new StringContent( - @$"grant_type=authorization_code&code={Model.code}&client_id={Startup.clientId}&client_secret={Startup.clientSecret}&scope=identify&redirect_uri=https://yawaflua.ru/authorize", + @$"grant_type=authorization_code&code={Model.code}&client_id={Startup.clientId}&client_secret={Startup.clientSecret}&scope=identify&redirect_uri={Startup.redirectUrl}", new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded") ); message = await Startup.client.SendAsync(requestMessage); diff --git a/Program.cs b/Program.cs index a111f34..6ad1f12 100644 --- a/Program.cs +++ b/Program.cs @@ -2,9 +2,18 @@ using Microsoft.AspNetCore.Hosting; using yaflay.ru; public class Program { - public static void Main() => CreateHostBuilder() + public static void Main(string[] args) + { + var parsedArgs = args.FirstOrDefault(k => k.StartsWith("/p:")).Replace("/p:", "").Split(";"); + var parse = (string name) => parsedArgs.FirstOrDefault(k => k.StartsWith(name))?.Split("=")[1] ?? null; + Startup.clientId = parse("clientId"); + Startup.clientSecret = parse("clientSecret"); + Startup.redirectUrl = parse("redirectUrl"); + + CreateHostBuilder() .Build() .Run(); + } private static IHostBuilder CreateHostBuilder() { return Host.CreateDefaultBuilder() diff --git a/Startup.cs b/Startup.cs index 9ca25ed..8f8126f 100644 --- a/Startup.cs +++ b/Startup.cs @@ -17,24 +17,22 @@ namespace yaflay.ru public static HttpClientHandler handler = new() { CookieContainer = cookieContainer}; public static HttpClient client = new(handler); public static AppDbContext? dbContext; - public static string applicationId; - public static string appToken; - public static string clientId; - public static string clientSecret; - public static string redirectUrl; + public static string? clientId = null; + public static string? clientSecret = null; + public static string? redirectUrl = null; public Startup() { configuration = new ConfigurationBuilder() .AddEnvironmentVariables(prefix: "m.") - .AddJsonFile("appsettings.json", optional: false) + .AddJsonFile("appsettings.json", optional: true) .Build(); + if (clientId == null | clientSecret == null | redirectUrl == null) + { + clientId = configuration.GetValue("clientId"); + clientSecret = configuration.GetValue("clientSecret"); + redirectUrl = configuration.GetValue("redirectUrl"); + } - Console.WriteLine(configuration.GetValue("applicationId")); - applicationId = configuration.GetValue("applicationId"); - appToken = configuration.GetValue("appToken"); - clientId = configuration.GetValue("clientId"); - clientSecret = configuration.GetValue("clientSecret"); - redirectUrl = configuration.GetValue("redirectUrl"); } public void ConfigureServices(IServiceCollection services) { diff --git a/appsettings.Development.json b/appsettings.Development.json index 770d3e9..edc9a46 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -5,5 +5,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } - } + }, + "clientId": "111111111111", + "clientSecret": "aAbBcCdD", + "redirectUrl": "https://example.com/authorize" }