Skip to content

Commit

Permalink
feat/fix: fix backend docker paths, migration db connection issue, ad…
Browse files Browse the repository at this point in the history
…d frontend dockerfile

* feat: docker compose support

* Set base URL outside of generated client

---------

Co-authored-by: Yukine <[email protected]>
  • Loading branch information
GerardSmit and DevYukine authored Jan 26, 2024
1 parent 1bcd6fc commit aad319f
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 8 deletions.
13 changes: 7 additions & 6 deletions backend/MangaMagnet.Api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["MangaMagnet/MangaMagnet.csproj", "MangaMagnet/"]
RUN dotnet restore "MangaMagnet/MangaMagnet.csproj"
COPY ["MangaMagnet.Api/MangaMagnet.Api.csproj", "MangaMagnet.Api/"]
COPY ["MangaMagnet.Core/MangaMagnet.Core.csproj", "MangaMagnet.Core/"]
RUN dotnet restore "MangaMagnet.Api/MangaMagnet.Api.csproj"
COPY . .
WORKDIR "/src/MangaMagnet"
RUN dotnet build "MangaMagnet.csproj" -c $BUILD_CONFIGURATION -o /app/build
WORKDIR "/src/MangaMagnet.Api"
RUN dotnet build "MangaMagnet.Api.csproj" --no-restore -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "MangaMagnet.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
RUN dotnet publish "MangaMagnet.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MangaMagnet.dll"]
ENTRYPOINT ["dotnet", "MangaMagnet.Api.dll"]
7 changes: 6 additions & 1 deletion backend/MangaMagnet.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Data;
using System.Net;
using System.Text.Json.Serialization;
using Asp.Versioning;
Expand Down Expand Up @@ -151,7 +152,11 @@

if (dbContext?.Database.GetDbConnection() is NpgsqlConnection npgsqlConnection)
{
await npgsqlConnection.OpenAsync();
if (npgsqlConnection.State != ConnectionState.Open)
{
await npgsqlConnection.OpenAsync();
}

try
{
await npgsqlConnection.ReloadTypesAsync();
Expand Down
45 changes: 45 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3.1'

services:
postgres:
image: postgres:16
environment:
POSTGRES_USER: mangamagnet
POSTGRES_PASSWORD: password
POSTGRES_DB: mangamagnet
healthcheck:
test: ["CMD-SHELL", "pg_isready -U mangamagnet -d mangamagnet"]
interval: 5s
timeout: 5s
retries: 5
volumes:
- data:/var/lib/postgresql/data

backend:
image: mangamagnet-backend
build:
context: ./backend
dockerfile: MangaMagnet.Api/Dockerfile
ports:
- 43524:8080
depends_on:
postgres:
condition: service_healthy
environment:
DOTNET_ConnectionStrings__PostgreSQLConnection: Host=postgres;Port=5432;Database=mangamagnet;Username=mangamagnet;Password=password
DOTNET_ConnectionStrings__QuartzPostgresSQLConnection: Host=postgres;Port=5432;Database=mangamagnet;Username=mangamagnet;Password=password;Search Path=quartz

frontend:
image: mangamagnet-frontend
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- 43525:3000
depends_on:
- backend
environment:
API_BASE: http://backend:8080

volumes:
data:
5 changes: 5 additions & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.git
.gitignore
*.md
dist
25 changes: 25 additions & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM node:20-slim AS base
WORKDIR /app

FROM base AS base-pnpm
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

FROM base-pnpm AS prod-deps
COPY package.json pnpm-lock.yaml ./
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile

FROM base-pnpm AS build
COPY . /app
ENV NEXT_TELEMETRY_DISABLED 1
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm run build

FROM base
COPY --from=build /app/public ./public
COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static
ENV PORT 3000
EXPOSE 3000
CMD ["node", "server.js"]
1 change: 1 addition & 0 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
reactStrictMode: true,
images: {
remotePatterns: [{
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NextRequest, NextResponse } from "next/server";

export const config = {
matcher: ["/api/:path*"],
};

export function middleware(request: NextRequest) {
const API_BASE = process.env.API_BASE ?? 'http://localhost:5248'

return NextResponse.rewrite(
new URL(
`${API_BASE}${request.nextUrl.pathname}${request.nextUrl.search}`
),
{ request }
);
}
3 changes: 3 additions & 0 deletions frontend/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { OpenAPI } from '@/services/openapi'
import '@/styles/globals.css'
import type { AppProps } from 'next/app'

OpenAPI.BASE = '';

export default function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
2 changes: 1 addition & 1 deletion frontend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2015",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down
7 changes: 7 additions & 0 deletions frontend/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface ImportMetaEnv {
readonly API_BASE: string
}

interface ImportMeta {
readonly env?: ImportMetaEnv
}

0 comments on commit aad319f

Please sign in to comment.