wip: troubleshooting docker flow

This commit is contained in:
2023-11-21 09:35:42 -06:00
parent c2bd8fac3e
commit 94364d17d6
12 changed files with 162 additions and 26 deletions

4
.gitignore vendored
View File

@@ -3,6 +3,10 @@
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# GITIGNORE FILES ADDED BY MIKAYLA
__notes__
*.env
# User-specific files
*.rsuser
*.suo

1
Unbinder/.dockerignore Normal file
View File

@@ -0,0 +1 @@
__notes__

View File

@@ -1,11 +1,26 @@
using Microsoft.AspNetCore.Mvc;
using Unbinder.Services;
namespace Unbinder.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
public async Task<IActionResult> Index()
{
var objects = await S3Service.ListObjects();
if (objects != null)
{
foreach (var entry in objects.S3Objects)
{
Console.WriteLine(entry.Key);
}
}
else
{
Console.WriteLine("Did not find results in S3");
}
return View();
}
}

View File

@@ -33,7 +33,8 @@ namespace Unbinder.Controllers
{
if (q == null && category == null) return View(_recipeRepository.GetAll);
var result = _recipeRepository.GetAll?.Where(r => r.Name.Contains(q, StringComparison.OrdinalIgnoreCase));
var result = _recipeRepository.GetAll?.Where(r => r.Name.Contains(q!, StringComparison.OrdinalIgnoreCase));
ViewBag.Query = q;
return result == null
? NotFound()

View File

@@ -0,0 +1,25 @@
IF NOT EXISTS(SELECT name FROM sys.databases WHERE name = 'Unbinder')
BEGIN
CREATE DATABASE Unbinder;
USE Unbinder;
CREATE TABLE [dbo].Recipes (
[Id] INT IDENTITY(1,1) NOT NULL,
[Name] NVARCHAR(50) NOT NULL,
[ShortDescription] NVARCHAR(1000) NULL,
[Author] NVARCHAR(100) NULL,
[RecipeText] NVARCHAR(8000) NULL,
[ImageUrl] NVARCHAR(1000) NULL,
[Created] DATETIME NOT NULL,
[Updated] DATETIME NOT NULL,
CONSTRAINT [PK_Recipes] PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].Ingredients (
[Id] INT IDENTITY(1,1) NOT NULL,
[Name] NVARCHAR(50) NOT NULL,
[Description] NVARCHAR(1000) NULL,
CONSTRAINT [PK_Ingredients] PRIMARY KEY CLUSTERED ([Id] ASC)
);
END

View File

@@ -0,0 +1,18 @@
# get .net 8
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
COPY . ./src
WORKDIR /src
RUN dotnet build -o /app
RUN dotnet publish -o /publish
ENV AWS_S3_URL=$AWS_S3_URL
ENV AWS_ACCESS_KEY=$AWS_ACCESS_KEY
ENV AWS_SECRET_KEY=$AWS_SECRET_KEY
ENV AWS_BUCKET_NAME=$AWS_BUCKET_NAME
WORKDIR /publish
EXPOSE 80
ENTRYPOINT ["./Unbinder"]

View File

@@ -0,0 +1,16 @@
# build from sql server image
FROM mcr.microsoft.com/mssql/server:2022-latest
# set environment variables
ENV ACCEPT_EULA=Y
ENV MSSQL_PID Express
ENV SA_PASSWORD=$DB_PASSWORD
# WORKDIR /db
# COPY ../DB/SQL/create_database.sql .
# run the sql script
# CMD /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P fhhwergaiuh12480y53 -d master -i create_database.sql
# run the command to start sql server
CMD /opt/mssql/bin/sqlservr

View File

@@ -8,6 +8,9 @@ using Microsoft.Identity.Web.UI;
using Unbinder.DB;
using Unbinder.Models;
using Unbinder.Repositories;
using Unbinder.Services;
using Amazon.S3;
using Microsoft.Extensions.Azure;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
@@ -43,6 +46,10 @@ builder.Services.AddScoped<IIngredientRepository, IngredientRepository>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddServerSideBlazor();
// include extra services
//builder.Services.AddSingleton(S3Service.Client);
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@@ -2,22 +2,54 @@
using Amazon.S3;
using Amazon.S3.Transfer;
using Amazon.Runtime;
using System.Net;
using Amazon.S3.Model;
namespace Unbinder.Services
{
public class S3Service
public sealed class S3Service
{
public static AWSCredentials Credentials =>
new BasicAWSCredentials(
Environment.GetEnvironmentVariable("AWS_ACCESS_KEY"),
Environment.GetEnvironmentVariable("AWS_SECRET_KEY"));
private static AWSCredentials Credentials
{
get
{
string? accessKey = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY");
string? secret = Environment.GetEnvironmentVariable("AWS_SECRET_KEY");
public static AmazonS3Client Client => new(Credentials, RegionEndpoint.USEast1);
Console.WriteLine(accessKey ?? "(none)", secret ?? "(none)");
if (accessKey == null || secret == null)
{
throw new Exception("AWS credentials not found");
}
return new BasicAWSCredentials(accessKey, secret);
}
}
private static AmazonS3Config Config => new()
{
RegionEndpoint = RegionEndpoint.USEast2
};
private static AmazonS3Client Client => new(Credentials);
private static readonly AmazonS3Client client = Client;
private static readonly string? BucketName = Environment.GetEnvironmentVariable("AWS_BUCKET_NAME");
public static async Task<ListObjectsResponse> ListObjects(string? prefix = "")
{
var bucketName = BucketName;
var request = new ListObjectsRequest
{
BucketName = bucketName,
Prefix = prefix
};
return await client.ListObjectsAsync(request);
}
public async Task<string?> UploadFileAsync(IFormFile file)
{
using IAmazonS3 client = Client;
if (file == null || file.Length == 0)
{
return null;
@@ -34,8 +66,6 @@ namespace Unbinder.Services
public async Task GetFile(string path, string outFile)
{
using IAmazonS3 client = Client;
var bucketName = "unbinder-recipe-images";
using var fileTransferUtility = new TransferUtility(client);
await fileTransferUtility.DownloadAsync(outFile, bucketName, path);

View File

@@ -24,6 +24,8 @@
</ItemGroup>
<ItemGroup>
<Folder Include="DB\SQL\" />
<Folder Include="__notes__\" />
<Folder Include="ViewModels\" />
</ItemGroup>

View File

@@ -1,9 +1,13 @@
@model IEnumerable<Recipe>
@{
string InputValue = ViewBag.Query ?? String.Empty;
}
<div>
<div id="search-page-header" class="mb-8 border-white border-b">
<h3 id="query-message"></h3>
<input class="text-black" id="new-search-query" type="text" />
<input value="@InputValue" class="text-black" id="new-search-query" type="text" />
<button id="execute-search">Search</button>
</div>
<div id="search-results">
@@ -26,11 +30,7 @@
console.log(query);
// set initial message value
if (query) {
message.innerHTML = `Viewing ${@Model.ToArray().Length} results for: ${query}`;
} else {
message.innerHTML = "Enter a new search term below:";
}
message.innerHTML = query ? `Viewing ${@Model.ToArray().Length} results for: ${query}` : "Enter a new search term below:";
// handle search updates on client side
searchButton.onclick = async () => {
@@ -40,13 +40,6 @@
const newResultJson = await newResult.json();
window.location.search = `?q=${textField.value}`;
searchResultsContainer.innerHTML = `@{
foreach (var recipe in @Model)
{
<p>@recipe.Name</p>
<p>@recipe.RecipeText</p>
}
}`;
}
});
</script>

View File

@@ -0,0 +1,24 @@
version: '3.8'
services:
client:
build:
context: .
dockerfile: ./Docker/DOTNET.Dockerfile
args:
AWS_S3_URL: ${AWS_S3_URL}
AWS_ACCESS_KEY: ${AWS_ACCESS_KEY}
AWS_SECRET_KEY: ${AWS_SECRET_KEY}
AWS_BUCKET_NAME: ${AWS_BUCKET_NAME}
ports:
- 80:80
depends_on:
- db
db:
build:
context: .
dockerfile: ./Docker/MSSQL.Dockerfile
args:
DB_PASSWORD: ${DB_PASSWORD}
ports:
- 1433:1433