diff --git a/.github/workflows/build-docker-image.yml b/.github/workflows/build-docker-image.yml deleted file mode 100644 index b3b3c08..0000000 --- a/.github/workflows/build-docker-image.yml +++ /dev/null @@ -1,95 +0,0 @@ -name: Docker Publish - -on: - release: - types: [published] - -env: - REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }} - RELEASE_TAG: ${{ github.event.release.tag_name }} - -jobs: - build: - name: 'Docker Publish' - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - # This is used to complete the identity challenge with sigstore/fulcio when running outside of PRs. - id-token: write - - steps: - # Checkout the release tag version - - name: Checkout repository ${{ env.RELEASE_TAG }} - uses: actions/checkout@v3 - with: - ref: ${{ env.RELEASE_TAG }} - - # Get git commit hash - - name: Get short hash - run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV - - # Need to lower case the image name for the docker tags when publishing - - name: Downcase IMAGE_NAME variable - run: echo "IMAGE_NAME_LOWER=${IMAGE_NAME,,}" >> $GITHUB_ENV - - # Sort out the image tags - - name: Set initial tag - run: echo "IMAGE_TAGS=${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}:${{ env.RELEASE_TAG }}" >> $GITHUB_ENV - - - name: Add latest tag if we're not production release - if: contains(env.RELEASE_TAG, 'next') - run: echo "IMAGE_TAGS=${{ env.IMAGE_TAGS }},${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}:latest" >> $GITHUB_ENV - - #debug - - name: Log the tags - run: echo "Calculated tags value => ${{ env.IMAGE_TAGS }}" - - # Setup docker build tool - - name: Setup Docker buildx - uses: docker/setup-buildx-action@v3 - - # Login against a Docker registry - - name: Log into registry ${{ env.REGISTRY }} - uses: docker/login-action@v3 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - # Extract metadata (tags, labels) for Docker - - name: Extract Docker metadata - id: meta - uses: docker/metadata-action@v5 - with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - - # Build and push Docker image with Buildx (don't push on PR) - - name: Build and push Docker image - id: build-and-push - uses: docker/build-push-action@v5 - with: - context: ./src - file: ./src/Childrens-Social-Care-CPD-Indexer/Dockerfile - push: true - tags: ${{ env.IMAGE_TAGS }} - labels: ${{ steps.meta.outputs.labels }} - build-args: | - VCSREF=${{ env.sha_short }} - VCSTAG=${{ env.RELEASE_TAG }} - cache-from: type=gha - cache-to: type=gha,mode=max - - # Sign the resulting Docker image digest. - # This will only write to the public Rekor transparency log when the Docker - # repository is public to avoid leaking data. If you would like to publish - # transparency data even for private images, pass --force to cosign below. - # https://github.com/sigstore/cosign - - name: Install Cosign - uses: sigstore/cosign-installer@v3.2.0 - - name: Check install! - run: cosign version - - name: Sign the published Docker image - # This step uses the identity token to provision an ephemeral certificate against the sigstore community Fulcio instance. - run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign --yes {}@${{ steps.build-and-push.outputs.digest }} diff --git a/.github/workflows/dotnet-publish.yml b/.github/workflows/dotnet-publish.yml new file mode 100644 index 0000000..8967a03 --- /dev/null +++ b/.github/workflows/dotnet-publish.yml @@ -0,0 +1,37 @@ +name: Dotnet Publish + +on: + release: + types: [published] + +env: + RELEASE_TAG: ${{ github.event.release.tag_name }} + +jobs: + build: + name: 'Build Deployment Package' + runs-on: ubuntu-latest + steps: + - name: Checkout repository ${{ env.RELEASE_TAG }} + uses: actions/checkout@v3 + with: + ref: ${{ env.RELEASE_TAG }} + + - name: Setup dotnet + uses: actions/setup-dotnet@v3 + with: + dotnet-version: '8.x' + + - name: dotnet publish + run: dotnet publish ./Childrens-Social-Care-CPD-Indexer/Childrens-Social-Care-CPD-Indexer.csproj -c Release -o ./publish + working-directory: ./src + + - name: Zip output + run: zip -r ../${{ env.RELEASE_TAG }}.zip ./ + working-directory: ./src/publish + + - name: Upload artifacts + uses: actions/upload-artifact@v3 + with: + name: resource-indexing-deployment + path: ./src/${{ env.RELEASE_TAG }}.zip \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer.Tests/IndexingServiceTests.cs b/src/Childrens-Social-Care-CPD-Indexer.Tests/IndexingServiceTests.cs index 57280f6..782e78a 100644 --- a/src/Childrens-Social-Care-CPD-Indexer.Tests/IndexingServiceTests.cs +++ b/src/Childrens-Social-Care-CPD-Indexer.Tests/IndexingServiceTests.cs @@ -1,5 +1,6 @@ using Childrens_Social_Care_CPD_Indexer.Core; using Microsoft.ApplicationInsights; +using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; using NSubstitute.ExceptionExtensions; @@ -7,28 +8,18 @@ namespace Childrens_Social_Care_CPD_Indexer.Tests; public class IndexingServiceTests { - private ILogger _logger; + private ILogger _logger; private IResourcesIndexerConfig _config; private IResourcesIndexer _indexer; - private IndexingService _sut; + private Indexer _sut; [SetUp] public void Setup() { - _logger = Substitute.For>(); + _logger = Substitute.For>(); _config = Substitute.For(); _indexer = Substitute.For(); - _sut = new IndexingService(_indexer, _logger, _config); - } - - [Test] - public void StopAsync_Returns_Completed_Task() - { - // act - var task = _sut.StopAsync(default); - - // assert - task.IsCompleted.Should().BeTrue(); + _sut = new Indexer(_logger, _indexer, _config); } [Test] @@ -38,7 +29,7 @@ public async Task StartAsync_Deletes_Index_If_Configured() _config.RecreateIndex.Returns(true); // act - await _sut.StartAsync(default); + await _sut.Run(new TimerInfo()); // assert await _indexer.Received(1).DeleteIndexAsync(Arg.Any(), Arg.Any()); @@ -52,7 +43,7 @@ public async Task StartAsync_Populates_Index() _config.RecreateIndex.Returns(false); // act - await _sut.StartAsync(default); + await _sut.Run(new TimerInfo()); // assert await _indexer.Received(1).PopulateIndexAsync(Arg.Any(), Arg.Any(), Arg.Any()); @@ -67,7 +58,7 @@ public async Task StartAsync_Logs_Exception() _indexer.DeleteIndexAsync(Arg.Any(), Arg.Any()).Throws(exception); // act - await _sut.StartAsync(default); + await _sut.Run(new TimerInfo()); // assert _logger.Received(1).LogError(exception, "Unhandled exception occured"); diff --git a/src/Childrens-Social-Care-CPD-Indexer.sln b/src/Childrens-Social-Care-CPD-Indexer.sln index ede56d3..21908a1 100644 --- a/src/Childrens-Social-Care-CPD-Indexer.sln +++ b/src/Childrens-Social-Care-CPD-Indexer.sln @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.8.34322.80 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Childrens-Social-Care-CPD-Indexer", "Childrens-Social-Care-CPD-Indexer\Childrens-Social-Care-CPD-Indexer.csproj", "{D806CCEA-5505-4CD9-98B6-08A5F161C06A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Childrens-Social-Care-CPD-Indexer.Tests", "Childrens-Social-Care-CPD-Indexer.Tests\Childrens-Social-Care-CPD-Indexer.Tests.csproj", "{6984BF56-808E-4294-949D-FFE4B02CCE16}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Childrens-Social-Care-CPD-Indexer.Tests", "Childrens-Social-Care-CPD-Indexer.Tests\Childrens-Social-Care-CPD-Indexer.Tests.csproj", "{6984BF56-808E-4294-949D-FFE4B02CCE16}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Childrens-Social-Care-CPD-Indexer", "Childrens-Social-Care-CPD-Indexer\Childrens-Social-Care-CPD-Indexer.csproj", "{3555944D-1913-4979-B5BF-991C7064613E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -13,14 +13,14 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {D806CCEA-5505-4CD9-98B6-08A5F161C06A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D806CCEA-5505-4CD9-98B6-08A5F161C06A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D806CCEA-5505-4CD9-98B6-08A5F161C06A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D806CCEA-5505-4CD9-98B6-08A5F161C06A}.Release|Any CPU.Build.0 = Release|Any CPU {6984BF56-808E-4294-949D-FFE4B02CCE16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {6984BF56-808E-4294-949D-FFE4B02CCE16}.Debug|Any CPU.Build.0 = Debug|Any CPU {6984BF56-808E-4294-949D-FFE4B02CCE16}.Release|Any CPU.ActiveCfg = Release|Any CPU {6984BF56-808E-4294-949D-FFE4B02CCE16}.Release|Any CPU.Build.0 = Release|Any CPU + {3555944D-1913-4979-B5BF-991C7064613E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3555944D-1913-4979-B5BF-991C7064613E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3555944D-1913-4979-B5BF-991C7064613E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3555944D-1913-4979-B5BF-991C7064613E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Childrens-Social-Care-CPD-Indexer/.gitignore b/src/Childrens-Social-Care-CPD-Indexer/.gitignore new file mode 100644 index 0000000..ff5b00c --- /dev/null +++ b/src/Childrens-Social-Care-CPD-Indexer/.gitignore @@ -0,0 +1,264 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# Azure Functions localsettings file +local.settings.json + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/Childrens-Social-Care-CPD-Indexer.csproj b/src/Childrens-Social-Care-CPD-Indexer/Childrens-Social-Care-CPD-Indexer.csproj index 48ae2fb..1692839 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Childrens-Social-Care-CPD-Indexer.csproj +++ b/src/Childrens-Social-Care-CPD-Indexer/Childrens-Social-Care-CPD-Indexer.csproj @@ -1,26 +1,39 @@ - - + net8.0 - enable + v4 + Exe enable - dotnet-Childrens_Social_Care_CPD_Indexer-5181f816-6a9f-4c7a-a524-576a7d37d013 + enable Childrens_Social_Care_CPD_Indexer + /home/site/wwwroot Linux - - - - + + + + - + - + + + PreserveNewest + + + PreserveNewest + Never + + + + + + \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/Core/IResourcesIndexer.cs b/src/Childrens-Social-Care-CPD-Indexer/Core/IResourcesIndexer.cs index 13c2707..f519948 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Core/IResourcesIndexer.cs +++ b/src/Childrens-Social-Care-CPD-Indexer/Core/IResourcesIndexer.cs @@ -1,6 +1,6 @@ namespace Childrens_Social_Care_CPD_Indexer.Core; -internal interface IResourcesIndexer +public interface IResourcesIndexer { Task CreateIndexAsync(string indexName, CancellationToken cancellationToken); Task DeleteIndexAsync(string indexName, CancellationToken cancellationToken); diff --git a/src/Childrens-Social-Care-CPD-Indexer/Core/IResourcesIndexerConfig.cs b/src/Childrens-Social-Care-CPD-Indexer/Core/IResourcesIndexerConfig.cs index 9f59606..b83e033 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Core/IResourcesIndexerConfig.cs +++ b/src/Childrens-Social-Care-CPD-Indexer/Core/IResourcesIndexerConfig.cs @@ -1,6 +1,6 @@ namespace Childrens_Social_Care_CPD_Indexer.Core; -internal interface IResourcesIndexerConfig +public interface IResourcesIndexerConfig { string ApiKey { get; } string AppInsightsConnectionString { get; } diff --git a/src/Childrens-Social-Care-CPD-Indexer/Core/ResourcesIndexer.cs b/src/Childrens-Social-Care-CPD-Indexer/Core/ResourcesIndexer.cs index e26f1a6..408acf7 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Core/ResourcesIndexer.cs +++ b/src/Childrens-Social-Care-CPD-Indexer/Core/ResourcesIndexer.cs @@ -3,6 +3,7 @@ using Azure.Search.Documents.Indexes.Models; using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.DataContracts; +using Microsoft.Extensions.Logging; namespace Childrens_Social_Care_CPD_Indexer.Core; diff --git a/src/Childrens-Social-Care-CPD-Indexer/Dockerfile b/src/Childrens-Social-Care-CPD-Indexer/Dockerfile index 9b63d70..c4a2687 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Dockerfile +++ b/src/Childrens-Social-Care-CPD-Indexer/Dockerfile @@ -1,8 +1,8 @@ #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/runtime:8.0 AS base -USER app -WORKDIR /app +FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 AS base +WORKDIR /home/site/wwwroot +EXPOSE 8080 FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build ARG BUILD_CONFIGURATION=Release @@ -18,6 +18,7 @@ ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./Childrens-Social-Care-CPD-Indexer.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false FROM base AS final -WORKDIR /app +WORKDIR /home/site/wwwroot COPY --from=publish /app/publish . -ENTRYPOINT ["dotnet", "Childrens-Social-Care-CPD-Indexer.dll"] \ No newline at end of file +ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ + AzureFunctionsJobHost__Logging__Console__IsEnabled=true \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/IndexingService.cs b/src/Childrens-Social-Care-CPD-Indexer/Indexer.cs similarity index 66% rename from src/Childrens-Social-Care-CPD-Indexer/IndexingService.cs rename to src/Childrens-Social-Care-CPD-Indexer/Indexer.cs index fac8624..b7c069d 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/IndexingService.cs +++ b/src/Childrens-Social-Care-CPD-Indexer/Indexer.cs @@ -1,10 +1,17 @@ using Childrens_Social_Care_CPD_Indexer.Core; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Logging; namespace Childrens_Social_Care_CPD_Indexer; -internal class IndexingService(IResourcesIndexer resourcesIndexer, ILogger logger, IResourcesIndexerConfig config) : IHostedService +public class Indexer(ILogger logger, IResourcesIndexer resourcesIndexer, IResourcesIndexerConfig config) { - public async Task StartAsync(CancellationToken cancellationToken) + [Function("IndexResources")] + public async Task Run([TimerTrigger("0 0 * * SUN" + #if DEBUG + , RunOnStartup= true + #endif + )] TimerInfo myTimer, CancellationToken cancellationToken = default) { logger.LogInformation("Indexing started at: {startTime}", DateTime.Now); try @@ -27,9 +34,4 @@ public async Task StartAsync(CancellationToken cancellationToken) logger.LogInformation("Indexing finished at: {finishTime}", DateTime.Now); } } - - public Task StopAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; - } } diff --git a/src/Childrens-Social-Care-CPD-Indexer/Program.cs b/src/Childrens-Social-Care-CPD-Indexer/Program.cs index 3a1eb29..f3352b8 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Program.cs +++ b/src/Childrens-Social-Care-CPD-Indexer/Program.cs @@ -1,65 +1,59 @@ -using System.Diagnostics.CodeAnalysis; using Childrens_Social_Care_CPD_Indexer.Core; using Childrens_Social_Care_CPD_Indexer; -using Contentful.Core; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Contentful.Core.Configuration; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.ApplicationInsights.WorkerService; using Azure.Search.Documents.Indexes; using Azure; -using Microsoft.ApplicationInsights.WorkerService; +using Contentful.Core.Configuration; +using Contentful.Core; using Microsoft.ApplicationInsights; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Logging; -var builder = Host.CreateDefaultBuilder(args); +var host = new HostBuilder() + .ConfigureFunctionsWorkerDefaults() + .ConfigureServices((context, services) => + { + services.AddTransient(); + var config = new ResourcesIndexerConfig(context.Configuration); -builder.ConfigureServices((context, services) => -{ - // Our configuration - services.AddTransient(); - var config = new ResourcesIndexerConfig(context.Configuration); - // Service - services.AddHostedService(); - - // Logging - services.AddLogging(); - var options = new ApplicationInsightsServiceOptions() - { - ApplicationVersion = config.ApplicationVersion, - ConnectionString = config.AppInsightsConnectionString, - }; - services.AddApplicationInsightsTelemetryWorkerService(options); - - // Code dependencies - services.TryAddTransient(); - services.AddTransient(servicesProvider => { - var httpClient = servicesProvider.GetRequiredService(); - var resourcesIndexerConfig = servicesProvider.GetRequiredService(); - var contentfulOptions = new ContentfulOptions() + // Logging + var options = new ApplicationInsightsServiceOptions() { - DeliveryApiKey = resourcesIndexerConfig.ContentfulApiKey, - SpaceId = resourcesIndexerConfig.ContentfulSpaceId, - Environment = resourcesIndexerConfig.ContentfulEnvironmentId + ApplicationVersion = config.ApplicationVersion, + ConnectionString = config.AppInsightsConnectionString, }; - return new ContentfulClient(httpClient, contentfulOptions); - }); - services.AddTransient(); - services.AddTransient(servicesProvider => { - var logger = servicesProvider.GetRequiredService>(); - var config = servicesProvider.GetRequiredService(); - var documentFetcher = servicesProvider.GetRequiredService(); - var searchEndpointUri = new Uri(config.Endpoint); - var searchIndexClient = new SearchIndexClient(searchEndpointUri, new AzureKeyCredential(config.ApiKey)); - var telemtryClient = servicesProvider.GetRequiredService(); - return new ResourcesIndexer(searchIndexClient, documentFetcher, logger, telemtryClient); - }); -}); -using (var host = builder.Build()) -{ - var lifetime = host.Services.GetRequiredService(); - await host.StartAsync().ContinueWith(x => lifetime.StopApplication()); -} + services.AddApplicationInsightsTelemetryWorkerService(options); + services.ConfigureFunctionsApplicationInsights(); + + // Code dependencies + services.TryAddTransient(); + services.AddTransient(servicesProvider => { + var httpClient = servicesProvider.GetRequiredService(); + var resourcesIndexerConfig = servicesProvider.GetRequiredService(); + var contentfulOptions = new ContentfulOptions() + { + DeliveryApiKey = resourcesIndexerConfig.ContentfulApiKey, + SpaceId = resourcesIndexerConfig.ContentfulSpaceId, + Environment = resourcesIndexerConfig.ContentfulEnvironmentId + }; + return new ContentfulClient(httpClient, contentfulOptions); + }); + services.AddTransient(); + services.AddTransient(servicesProvider => { + var logger = servicesProvider.GetRequiredService>(); + var config = servicesProvider.GetRequiredService(); + var documentFetcher = servicesProvider.GetRequiredService(); + var searchEndpointUri = new Uri(config.Endpoint); + var searchIndexClient = new SearchIndexClient(searchEndpointUri, new AzureKeyCredential(config.ApiKey)); + var telemtryClient = servicesProvider.GetRequiredService(); + return new ResourcesIndexer(searchIndexClient, documentFetcher, logger, telemtryClient); + }); + }) + .Build(); -[ExcludeFromCodeCoverage] -public partial class Program() -{} \ No newline at end of file +host.Run(); diff --git a/src/Childrens-Social-Care-CPD-Indexer/Properties/launchSettings.json b/src/Childrens-Social-Care-CPD-Indexer/Properties/launchSettings.json index cae296e..3939375 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Properties/launchSettings.json +++ b/src/Childrens-Social-Care-CPD-Indexer/Properties/launchSettings.json @@ -2,14 +2,13 @@ "profiles": { "Childrens_Social_Care_CPD_Indexer": { "commandName": "Project", - "environmentVariables": { - "DOTNET_ENVIRONMENT": "Development" - }, - "dotnetRunMessages": true + "commandLineArgs": "--port 7016" }, "Docker": { - "commandName": "Docker" + "commandName": "Docker", + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", + "httpPort": 31453, + "useSSL": false } - }, - "$schema": "http://json.schemastore.org/launchsettings.json" + } } \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.json b/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.json new file mode 100644 index 0000000..df4dcc9 --- /dev/null +++ b/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "appInsights1": { + "type": "appInsights" + }, + "storage1": { + "type": "storage", + "connectionId": "AzureWebJobsStorage" + } + } +} \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.local.json b/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.local.json new file mode 100644 index 0000000..b804a28 --- /dev/null +++ b/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.local.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "appInsights1": { + "type": "appInsights.sdk" + }, + "storage1": { + "type": "storage.emulator", + "connectionId": "AzureWebJobsStorage" + } + } +} \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/ResourcesIndexerConfig.cs b/src/Childrens-Social-Care-CPD-Indexer/ResourcesIndexerConfig.cs index d67a842..1c74e9e 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/ResourcesIndexerConfig.cs +++ b/src/Childrens-Social-Care-CPD-Indexer/ResourcesIndexerConfig.cs @@ -1,4 +1,5 @@ using Childrens_Social_Care_CPD_Indexer.Core; +using Microsoft.Extensions.Configuration; namespace Childrens_Social_Care_CPD_Indexer; diff --git a/src/Childrens-Social-Care-CPD-Indexer/appsettings.Development.json b/src/Childrens-Social-Care-CPD-Indexer/appsettings.Development.json deleted file mode 100644 index b2dcdb6..0000000 --- a/src/Childrens-Social-Care-CPD-Indexer/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.Hosting.Lifetime": "Information" - } - } -} diff --git a/src/Childrens-Social-Care-CPD-Indexer/appsettings.json b/src/Childrens-Social-Care-CPD-Indexer/appsettings.json deleted file mode 100644 index e770633..0000000 --- a/src/Childrens-Social-Care-CPD-Indexer/appsettings.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.Hosting.Lifetime": "Information" - }, - "ApplicationInsights": { - "LogLevel": { - "Default": "Information" - } - } - } -} diff --git a/src/Childrens-Social-Care-CPD-Indexer/host.json b/src/Childrens-Social-Care-CPD-Indexer/host.json new file mode 100644 index 0000000..ee5cf5f --- /dev/null +++ b/src/Childrens-Social-Care-CPD-Indexer/host.json @@ -0,0 +1,12 @@ +{ + "version": "2.0", + "logging": { + "applicationInsights": { + "samplingSettings": { + "isEnabled": true, + "excludedTypes": "Request" + }, + "enableLiveMetricsFilters": true + } + } +} \ No newline at end of file