From 53ef1763aa34bd3382c5237fa7183146f296f268 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Tue, 14 Feb 2023 17:51:22 -0800 Subject: [PATCH] Rebuilding in dotnet for practice --- .gitignore | 3 + .vscode/launch.json | 35 +++++++++ .vscode/settings.json | 6 ++ .vscode/tasks.json | 41 ++++++++++ .../Controllers/WeatherForecastController.cs | 32 ++++++++ .../Newsbot.Collector.Api.csproj | 21 +++++ Newsbot.Collector.Api/Program.cs | 44 +++++++++++ .../Properties/launchSettings.json | 41 ++++++++++ Newsbot.Collector.Api/WeatherForecast.cs | 12 +++ .../appsettings.template.json | 31 ++++++++ Newsbot.Collector.Domain/Class1.cs | 5 ++ Newsbot.Collector.Domain/Models/Config.cs | 16 ++++ .../Newsbot.Collector.Domain.csproj | 9 +++ Newsbot.Collector.Services/EnvLoader.cs | 77 +++++++++++++++++++ Newsbot.Collector.Services/Jobs/Factory.cs | 0 .../Jobs/HelloWorldJob.cs | 18 +++++ .../Newsbot.Collector.Services.csproj | 17 ++++ .../Newsbot.Collector.Tests.csproj | 24 ++++++ Newsbot.Collector.Tests/UnitTest1.cs | 10 +++ Newsbot.Collector.Tests/Usings.cs | 1 + Newsbot.Collector.sln | 40 ++++++++++ 21 files changed, 483 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 Newsbot.Collector.Api/Controllers/WeatherForecastController.cs create mode 100644 Newsbot.Collector.Api/Newsbot.Collector.Api.csproj create mode 100644 Newsbot.Collector.Api/Program.cs create mode 100644 Newsbot.Collector.Api/Properties/launchSettings.json create mode 100644 Newsbot.Collector.Api/WeatherForecast.cs create mode 100644 Newsbot.Collector.Api/appsettings.template.json create mode 100644 Newsbot.Collector.Domain/Class1.cs create mode 100644 Newsbot.Collector.Domain/Models/Config.cs create mode 100644 Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj create mode 100644 Newsbot.Collector.Services/EnvLoader.cs create mode 100644 Newsbot.Collector.Services/Jobs/Factory.cs create mode 100644 Newsbot.Collector.Services/Jobs/HelloWorldJob.cs create mode 100644 Newsbot.Collector.Services/Newsbot.Collector.Services.csproj create mode 100644 Newsbot.Collector.Tests/Newsbot.Collector.Tests.csproj create mode 100644 Newsbot.Collector.Tests/UnitTest1.cs create mode 100644 Newsbot.Collector.Tests/Usings.cs create mode 100644 Newsbot.Collector.sln diff --git a/.gitignore b/.gitignore index dfcfd56..e62a942 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,9 @@ *.userosscache *.sln.docstates +appsettings.Development.json +appsettings.json + # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c90f1a3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,35 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/Newsbot.Collector.Api/bin/Debug/net7.0/Newsbot.Collector.Api.dll", + "args": [], + "cwd": "${workspaceFolder}/Newsbot.Collector.Api", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1c6dc21 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.exclude": { + "**/obj": true, + "**/bin": true + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..2d69145 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Newsbot.Collector.Api/Controllers/WeatherForecastController.cs b/Newsbot.Collector.Api/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..8a25c93 --- /dev/null +++ b/Newsbot.Collector.Api/Controllers/WeatherForecastController.cs @@ -0,0 +1,32 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Newsbot.Collector.Api.Controllers; + +[ApiController] +[Route("[controller]")] +public class WeatherForecastController : ControllerBase +{ + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } +} diff --git a/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj b/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj new file mode 100644 index 0000000..d2595e3 --- /dev/null +++ b/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj @@ -0,0 +1,21 @@ + + + + net7.0 + enable + enable + + + + + + + + + + + + + + + diff --git a/Newsbot.Collector.Api/Program.cs b/Newsbot.Collector.Api/Program.cs new file mode 100644 index 0000000..7656abc --- /dev/null +++ b/Newsbot.Collector.Api/Program.cs @@ -0,0 +1,44 @@ +using Hangfire; +using Hangfire.MemoryStorage; +using Newsbot.Collector.Services; +using Newsbot.Collector.Domain.Models; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +// Build the conifg +var config = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .AddEnvironmentVariables() + .Build(); +var cfg = config.GetRequiredSection("Config").Get(); +builder.Configuration.AddConfiguration(config); + +builder.Services.AddHangfire(f => f.UseMemoryStorage()); +builder.Services.AddHangfireServer(); + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseHangfireDashboard(); +//RecurringJob.AddOrUpdate() + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/Newsbot.Collector.Api/Properties/launchSettings.json b/Newsbot.Collector.Api/Properties/launchSettings.json new file mode 100644 index 0000000..1b57468 --- /dev/null +++ b/Newsbot.Collector.Api/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:12777", + "sslPort": 44387 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5011", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7155;http://localhost:5011", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Newsbot.Collector.Api/WeatherForecast.cs b/Newsbot.Collector.Api/WeatherForecast.cs new file mode 100644 index 0000000..48c6e29 --- /dev/null +++ b/Newsbot.Collector.Api/WeatherForecast.cs @@ -0,0 +1,12 @@ +namespace Newsbot.Collector.Api; + +public class WeatherForecast +{ + public DateOnly Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } +} diff --git a/Newsbot.Collector.Api/appsettings.template.json b/Newsbot.Collector.Api/appsettings.template.json new file mode 100644 index 0000000..63e9531 --- /dev/null +++ b/Newsbot.Collector.Api/appsettings.template.json @@ -0,0 +1,31 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "Config": { + "ServerAddress": "", + "SqlConnectionString": "", + "Reddit": { + "IsEnabled": false, + "PullHot": false, + "PullNsfw": false, + "PullTop": false + }, + "Youtube": { + "IsEnabled": false, + "Debug": false + }, + "Twitch": { + "IsEnabled": false, + "ClientID": "", + "ClientSecret": "" + }, + "FFXIV": { + "IsEnabled": false + } + }, + "AllowedHosts": "*" +} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Class1.cs b/Newsbot.Collector.Domain/Class1.cs new file mode 100644 index 0000000..25ee9bb --- /dev/null +++ b/Newsbot.Collector.Domain/Class1.cs @@ -0,0 +1,5 @@ +namespace Newsbot.Collector.Domain; +public class Class1 +{ + +} diff --git a/Newsbot.Collector.Domain/Models/Config.cs b/Newsbot.Collector.Domain/Models/Config.cs new file mode 100644 index 0000000..bee2f48 --- /dev/null +++ b/Newsbot.Collector.Domain/Models/Config.cs @@ -0,0 +1,16 @@ +namespace Newsbot.Collector.Domain.Models; + +public class ConfigModel +{ + public string? ServerAddress { get; set; } + public string? SqlConnectionString { get; set; } + public RedditConfigModel? Reddit { get; set; } +} + +public class RedditConfigModel +{ + public bool IsEnabled { get; set; } + public bool PullHot { get; set; } + public bool PullNsfw { get; set; } + public bool PullTop { get; set; } +} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj b/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/Newsbot.Collector.Services/EnvLoader.cs b/Newsbot.Collector.Services/EnvLoader.cs new file mode 100644 index 0000000..2dd8c49 --- /dev/null +++ b/Newsbot.Collector.Services/EnvLoader.cs @@ -0,0 +1,77 @@ +using Newsbot.Collector.Domain.Models; + +namespace Newsbot.Collector.Services; + +public static class EnvLoader +{ + + public static ConfigModel Load() + { + var reddit = new RedditConfigModel + { + IsEnabled = Bool("FEATURE_ENABLE_REDDIT_BACKEND"), + PullHot = Bool("REDDIT_PULL_HOT"), + PullNsfw = Bool("REDDIT_PULL_NSFW"), + PullTop = Bool("REDDIT_PULL_TOP") + }; + + return new ConfigModel + { + ServerAddress = String("SERVER_ADDRESS"), + SqlConnectionString = String("SQL_CONNECTION_STRING"), + Reddit = reddit, + }; + } + + public static void LoadEnvFile() + { + var curDir = Directory.GetCurrentDirectory(); + var filePath = Path.Combine(curDir, ".env"); + + if (!File.Exists(filePath)) + return; + + foreach (var line in File.ReadAllLines(filePath)) + { + var parts = line.Split('=', StringSplitOptions.RemoveEmptyEntries); + + if (parts.Length != 2) + continue; + + if (parts[1].Contains("'") == true ){ + parts[1] = parts[1].Replace("'", ""); + } + + Environment.SetEnvironmentVariable(parts[0], parts[1]); + } +} + +private static string String(string Key) +{ + var result = Environment.GetEnvironmentVariable(Key); + if (result is null) + { + return ""; + } + + return result; +} + +private static bool Bool(string Key) +{ + var result = String(Key); + if (result == "") + { + return false; + } + + if (result.ToLower() == "true") + { + return true; + } + else + { + return false; + } +} +} \ No newline at end of file diff --git a/Newsbot.Collector.Services/Jobs/Factory.cs b/Newsbot.Collector.Services/Jobs/Factory.cs new file mode 100644 index 0000000..e69de29 diff --git a/Newsbot.Collector.Services/Jobs/HelloWorldJob.cs b/Newsbot.Collector.Services/Jobs/HelloWorldJob.cs new file mode 100644 index 0000000..8ff4d9d --- /dev/null +++ b/Newsbot.Collector.Services/Jobs/HelloWorldJob.cs @@ -0,0 +1,18 @@ + +namespace Newsbot.Collector.Services.Jobs; + +public class HelloWorldJob +{ + + public readonly string _message; + + public HelloWorldJob(string message) + { + _message = message; + } + + public void Execute() + { + Console.WriteLine(_message); + } +} \ No newline at end of file diff --git a/Newsbot.Collector.Services/Newsbot.Collector.Services.csproj b/Newsbot.Collector.Services/Newsbot.Collector.Services.csproj new file mode 100644 index 0000000..f793c4e --- /dev/null +++ b/Newsbot.Collector.Services/Newsbot.Collector.Services.csproj @@ -0,0 +1,17 @@ + + + + + + + + + + + + net7.0 + enable + enable + + + diff --git a/Newsbot.Collector.Tests/Newsbot.Collector.Tests.csproj b/Newsbot.Collector.Tests/Newsbot.Collector.Tests.csproj new file mode 100644 index 0000000..86a36ef --- /dev/null +++ b/Newsbot.Collector.Tests/Newsbot.Collector.Tests.csproj @@ -0,0 +1,24 @@ + + + + net7.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/Newsbot.Collector.Tests/UnitTest1.cs b/Newsbot.Collector.Tests/UnitTest1.cs new file mode 100644 index 0000000..bf54d54 --- /dev/null +++ b/Newsbot.Collector.Tests/UnitTest1.cs @@ -0,0 +1,10 @@ +namespace Newsbot.Collector.Tests; + +public class UnitTest1 +{ + [Fact] + public void Test1() + { + + } +} \ No newline at end of file diff --git a/Newsbot.Collector.Tests/Usings.cs b/Newsbot.Collector.Tests/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/Newsbot.Collector.Tests/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/Newsbot.Collector.sln b/Newsbot.Collector.sln new file mode 100644 index 0000000..1f368a9 --- /dev/null +++ b/Newsbot.Collector.sln @@ -0,0 +1,40 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newsbot.Collector.Api", "Newsbot.Collector.Api\Newsbot.Collector.Api.csproj", "{BDAA344D-0C78-40E5-9C7D-BFAAA8F3FCEB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newsbot.Collector.Domain", "Newsbot.Collector.Domain\Newsbot.Collector.Domain.csproj", "{BAEF51D8-DB35-4A67-A13A-9DB77525C769}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newsbot.Collector.Services", "Newsbot.Collector.Services\Newsbot.Collector.Services.csproj", "{58D012BB-DAC0-4BF6-AB03-A10F5AE6A1D7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newsbot.Collector.Tests", "Newsbot.Collector.Tests\Newsbot.Collector.Tests.csproj", "{B677151F-5E71-4AA9-968A-D2AAB6375DE4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BDAA344D-0C78-40E5-9C7D-BFAAA8F3FCEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BDAA344D-0C78-40E5-9C7D-BFAAA8F3FCEB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BDAA344D-0C78-40E5-9C7D-BFAAA8F3FCEB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BDAA344D-0C78-40E5-9C7D-BFAAA8F3FCEB}.Release|Any CPU.Build.0 = Release|Any CPU + {BAEF51D8-DB35-4A67-A13A-9DB77525C769}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BAEF51D8-DB35-4A67-A13A-9DB77525C769}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BAEF51D8-DB35-4A67-A13A-9DB77525C769}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BAEF51D8-DB35-4A67-A13A-9DB77525C769}.Release|Any CPU.Build.0 = Release|Any CPU + {58D012BB-DAC0-4BF6-AB03-A10F5AE6A1D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {58D012BB-DAC0-4BF6-AB03-A10F5AE6A1D7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {58D012BB-DAC0-4BF6-AB03-A10F5AE6A1D7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {58D012BB-DAC0-4BF6-AB03-A10F5AE6A1D7}.Release|Any CPU.Build.0 = Release|Any CPU + {B677151F-5E71-4AA9-968A-D2AAB6375DE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B677151F-5E71-4AA9-968A-D2AAB6375DE4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B677151F-5E71-4AA9-968A-D2AAB6375DE4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B677151F-5E71-4AA9-968A-D2AAB6375DE4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal