Skip to content

Commit

Permalink
Merge pull request #43 from trifork/feature/cheetah.kafka-nuget
Browse files Browse the repository at this point in the history
Feature/cheetah.kafka nuget
  • Loading branch information
cthtrifork authored Jan 23, 2024
2 parents 6b321b3 + b510aa1 commit d9336a0
Show file tree
Hide file tree
Showing 28 changed files with 413 additions and 288 deletions.
16 changes: 6 additions & 10 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# [Choice] .NET version: 6.0-bullseye-slim, 6.0-jammy, 6.0-focal
FROM mcr.microsoft.com/devcontainers/dotnet:1-6.0-jammy
FROM mcr.microsoft.com/devcontainers/dotnet:1-8.0-bookworm

ENV DOCKER_BUILDKIT=1
ENV DOCKER_DEFAULT_PLATFORM=linux/amd64
Expand All @@ -8,16 +8,12 @@ ENV DOCKER_DEFAULT_PLATFORM=linux/amd64
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends git curl exa

# Install Docker to use in docker-in-docker
RUN apt-get update && apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/* \
&& curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh
# Add user "admin" to the Docker group
#&& usermod -a -G docker admin
ADD https://raw.githubusercontent.com/docker/docker-ce/master/components/cli/contrib/completion/bash/docker /etc/bash_completion.d/docker.sh

USER vscode
#USER vscode
ARG GITHUB_ACTOR
ARG GITHUB_TOKEN

RUN dotnet nuget add source https://nuget.pkg.github.com/trifork/index.json -n trifork-github -u $GITHUB_ACTOR -p $GITHUB_TOKEN --store-password-in-clear-text

# Install dotnet outdated
RUN dotnet tool update --global dotnet-outdated-tool
ENV PATH="$PATH:/root/.dotnet/tools"
24 changes: 7 additions & 17 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,29 @@
},
"containerEnv": {
"GITHUB_ACTOR": "${localEnv:GITHUB_ACTOR}",
"GITHUB_TOKEN": "${localEnv:GITHUB_TOKEN}"
"GITHUB_TOKEN": "${localEnv:GITHUB_TOKEN}",
"DOTNET_ROLL_FORWARD": "LatestMajor",
"DOTNET_HOST_PATH": "/usr/share/dotnet/dotnet"
},
"runArgs": [
// Network where OpenSearch is running
"--network=cheetah-infrastructure"
],
"initializeCommand": "docker network create cheetah-infrastructure || true",
"customizations": {
"vscode": {
"extensions": [
// Recommended extensions - GitHub
"GitHub.vscode-pull-request-github",
"GitHub.copilot",
// Recommended extensions - Collaboration
"eamodio.gitlens",
"EditorConfig.EditorConfig",
"MS-vsliveshare.vsliveshare-pack",
"streetsidesoftware.code-spell-checker",
"redhat.vscode-yaml",
// Recommended extensions - .NET
"Fudge.auto-using",
"jongrant.csharpsortusings",
"kreativ-software.csharpextensions",
// Recommended extensions - Markdown
"bierner.github-markdown-preview",
"DavidAnson.vscode-markdownlint",
"docsmsft.docs-linting",
"yzhang.markdown-all-in-one",
// Required extensions
"ms-dotnettools.csharp",
"VisualStudioExptTeam.vscodeintellicode",
"aliasadidev.nugetpackagemanagergui"
]
"aliasadidev.nugetpackagemanagergui",
"ms-dotnettools.csdevkit"
],
}
},
"features": {
Expand All @@ -63,5 +53,5 @@
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "root"
"remoteUser": "root"
}
4 changes: 2 additions & 2 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ jobs:
max_attempts: 25
retry_wait_seconds: 5
warning_on_retry: false
command: 'docker run --rm --network=cheetah-infrastructure badouralix/curl-jq curl -sS -X GET -H "Content-Type: application/json" http://cheetahwebapi:80/health'
command: 'docker run --rm --network=cheetah-infrastructure badouralix/curl-jq curl -sS -X GET -H "Content-Type: application/json" http://cheetahwebapi:8080/health'

- name: "Check metrics"
shell: bash
run: |
docker run --rm --network=cheetah-infrastructure badouralix/curl-jq curl -sS -X GET -H "Content-Type: application/json" http://cheetahwebapi:80/metrics
docker run --rm --network=cheetah-infrastructure badouralix/curl-jq curl -sS -X GET -H "Content-Type: application/json" http://cheetahwebapi:8080/metrics
- name: "Print logs"
if: always()
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mega-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
DISABLE_LINTERS: "MARKDOWN_MARKDOWN_LINK_CHECK,HTML_HTMLHINT,JSON_V8R,REPOSITORY_GIT_DIFF,REPOSITORY_TRIVY,REPOSITORY_CHECKOV,POWERSHELL_POWERSHELL"
DOCKERFILE_HADOLINT_ARGUMENTS: "--ignore ds026"
#DOCKERFILE_HADOLINT_FILTER_REGEX_EXCLUDE: ".devcontainer"
FILTER_REGEX_EXCLUDE: '(\.automation/test|\.automation/generated|\.github|docs/javascripts|docs/overrides|docs/json-schemas|flavors|clj-kondo|TEMPLATES|.devcontainer)'
FILTER_REGEX_EXCLUDE: '(\.automation/test|\.automation/generated|\.github|docs/javascripts|docs/overrides|docs/json-schemas|flavors|clj-kondo|TEMPLATES|.devcontainer|.vscode)'
REPOSITORY_CHECKOV_ARGUMENTS: "--skip-check CKV2_GHA_1,CKV_DOCKER_7"
EXCLUDED_DIRECTORIES: ".devcontainer"
REPOSITORY_TRIVY_ARGUMENTS: "--skip-dirs .devcontainer"
Expand Down
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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/dotnet/vscode-csharp/blob/main/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}/src/Cheetah.WebApi/bin/Debug/net8.0/Cheetah.WebApi.dll",
"args": [],
"cwd": "${workspaceFolder}/src/Cheetah.WebApi",
"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"
}
]
}
14 changes: 14 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"dotnetAcquisitionExtension.installTimeoutValue": 500,
"dotnet.dotnetPath": "/usr/share/dotnet",
"dotnetAcquisitionExtension.existingDotnetPath": [
{
"extensionId": "msazurermtools.azurerm-vscode-tools",
"path": "/usr/share/dotnet/dotnet"
},
{
"extensionId": "visualstudiotoolsforunity.vstuc",
"path": "/usr/share/dotnet/dotnet"
}
]
}
41 changes: 41 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/src/Cheetah.WebApi.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/src/Cheetah.WebApi.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/src/Cheetah.WebApi.sln"
],
"problemMatcher": "$msCompile"
}
]
}
2 changes: 1 addition & 1 deletion src/Cheetah.WebApi.Test/Cheetah.WebApi.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
Expand Down
16 changes: 9 additions & 7 deletions src/Cheetah.WebApi/Cheetah.WebApi.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<LangVersion>10</LangVersion>
<LangVersion>Latest</LangVersion>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<DocumentationFile>Cheetah.WebApi.xml</DocumentationFile>
<UserSecretsId>bb3fdc17-ede2-42c1-82cf-eae032a2caa5</UserSecretsId>
</PropertyGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="dotnet tool restore" />
<Exec Command="dotnet swagger tofile --output swagger.json $(OutputPath)\$(AssemblyName).dll v1 " />
<Exec Command="dotnet swagger tofile --output swagger.json $(OutputPath)/$(AssemblyName).dll v1.0 " />
<Exec Command="dotnet swagger tofile --output swagger.json $(OutputPath)/$(AssemblyName).dll v2.0 " />
</Target>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="6.0.5" />
<PackageReference Include="Cheetah.WebApi.Shared" Version="1.7.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.0" />
<PackageReference Include="Cheetah.OpenSearch" Version="0.1.0" />
<PackageReference Include="Cheetah.Kafka" Version="0.2.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.26" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.1.0" />
Expand All @@ -29,7 +31,7 @@
<PackageReference Include="prometheus-net" Version="8.2.1" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
<PackageReference Include="prometheus-net.AspNetCore.HealthChecks" Version="8.2.1" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
17 changes: 17 additions & 0 deletions src/Cheetah.WebApi/Cheetah.WebApi.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions src/Cheetah.WebApi/Core/Config/OpenSearchConfig.cs

This file was deleted.

14 changes: 0 additions & 14 deletions src/Cheetah.WebApi/Core/Config/Priorities.cs

This file was deleted.

18 changes: 10 additions & 8 deletions src/Cheetah.WebApi/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

ARG GITHUB_ACTOR
ARG GITHUB_TOKEN
Expand All @@ -14,9 +14,14 @@ WORKDIR /src
COPY "src/NuGet-CI.Config" "NuGet.config"
COPY "src/.config/" ".config/"
COPY ["src/Cheetah.WebApi/Cheetah.WebApi.csproj", "Cheetah.WebApi/"]

# This is only needed because swagger uses .NET 7
ENV DOTNET_ROLL_FORWARD=LatestMajor

RUN --mount=type=secret,id=GITHUB_TOKEN \
GITHUB_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" \
dotnet restore "Cheetah.WebApi/Cheetah.WebApi.csproj"

COPY src .
WORKDIR "/src/Cheetah.WebApi"
RUN dotnet build "Cheetah.WebApi.csproj" -c Release -o /app/build
Expand All @@ -25,14 +30,11 @@ FROM build AS publish
RUN dotnet publish "Cheetah.WebApi.csproj" -c Release -o /app/publish

FROM base AS final
# Create a new user with bash and a homedir
RUN useradd -r -u 1001 -m -s /bin/bash dotnetuser

# Set the user for subsequent commands
USER 1001

ENV COMPlus_EnableDiagnostics=0

WORKDIR /app
COPY --from=publish /app/publish .

# 64198
USER $APP_UID
ENTRYPOINT ["dotnet", "Cheetah.WebApi.dll"]
6 changes: 5 additions & 1 deletion src/Cheetah.WebApi/HostedServices/TopicSubscriberService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Cheetah.WebApi.Core.Config;
using Confluent.Kafka;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Cheetah.WebApi
Expand All @@ -15,16 +16,19 @@ class TopicSubscriberService : IHostedService
{
private IConsumer<Ignore, string> _kafkaConsumer;
private IOptions<KafkaConsumerConfig> _kafkaConsumerOptions;
private readonly ILogger<TopicSubscriberService> _logger;

public TopicSubscriberService(IConsumer<Ignore, string> kafkaConsumer,
IOptions<KafkaConsumerConfig> kafkaConsumerOptions)
IOptions<KafkaConsumerConfig> kafkaConsumerOptions, ILogger<TopicSubscriberService> logger)
{
_kafkaConsumer = kafkaConsumer;
_kafkaConsumerOptions = kafkaConsumerOptions;
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_kafkaConsumer.Subscribe(_kafkaConsumerOptions.Value.Topic);
_logger.LogDebug("Subscribed to {topic} with consumergroup {groupid}", _kafkaConsumerOptions.Value.Topic, _kafkaConsumer.MemberId);
// NB: If you want to consume messages in a hosted service, then look at the cheetah-example-alertservice repository.
return Task.CompletedTask;
}
Expand Down
Loading

0 comments on commit d9336a0

Please sign in to comment.