Implement /data and /themes directories (#38)

This will allow volumes to be mounted for persistent data/themes
This commit is contained in:
Daniel J. Summers 2024-01-05 17:32:38 -05:00
parent 5a3d9f05f8
commit 3226f882a3
5 changed files with 21 additions and 16 deletions

2
.gitignore vendored
View File

@ -265,4 +265,4 @@ src/MyWebLog/wwwroot/img/bit-badger
src/MyWebLog/appsettings.Production.json
# SQLite database files
src/MyWebLog/*.db*
src/MyWebLog/data/*.db*

View File

@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /mwl
COPY ./MyWebLog.sln ./
COPY ./Directory.Build.props ./
@ -9,7 +9,7 @@ RUN dotnet restore
COPY . ./
WORKDIR /mwl/MyWebLog
RUN dotnet publish -f net7.0 -c Release -r linux-x64 --no-self-contained -p:PublishSingleFile=false
RUN dotnet publish -f net8.0 -c Release -r linux-x64 --no-self-contained -p:PublishSingleFile=false
FROM alpine AS theme
RUN apk add --no-cache zip
@ -19,12 +19,13 @@ RUN zip default-theme.zip ./default-theme/*
COPY ./admin-theme ./admin-theme/
RUN zip admin-theme.zip ./admin-theme/*
FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine as final
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine as final
WORKDIR /app
RUN apk add --no-cache icu-libs
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
COPY --from=build /mwl/MyWebLog/bin/Release/net7.0/linux-x64/publish/ ./
COPY --from=build /mwl/MyWebLog/bin/Release/net8.0/linux-x64/publish/ ./
COPY --from=theme /themes/*.zip /app/
RUN mkdir themes
EXPOSE 80
CMD [ "dotnet", "/app/MyWebLog.dll" ]

View File

@ -482,7 +482,7 @@ module Theme =
do! ThemeAssetCache.refreshTheme themeId data
TemplateCache.invalidateTheme themeId
// Save the .zip file
use file = new FileStream($"{themeId}-theme.zip", FileMode.Create)
use file = new FileStream($"./themes/{themeId}-theme.zip", FileMode.Create)
do! themeFile.CopyToAsync file
do! addMessage ctx
{ UserMessage.Success with
@ -517,7 +517,7 @@ module Theme =
| _ ->
match! data.Theme.Delete (ThemeId themeId) with
| true ->
let zippedTheme = $"{themeId}-theme.zip"
let zippedTheme = $"./themes/{themeId}-theme.zip"
if File.Exists zippedTheme then File.Delete zippedTheme
do! addMessage ctx { UserMessage.Success with Message = $"Theme ID {themeId} deleted successfully" }
return! all next ctx

View File

@ -98,7 +98,7 @@ module DataImplementation =
log.LogInformation $"Using PostgreSQL database {conn.Database}"
PostgresData(log, Json.configure (JsonSerializer.CreateDefault()))
else
createSQLite "Data Source=./myweblog.db;Cache=Shared"
createSQLite "Data Source=./data/myweblog.db;Cache=Shared"
open System.Threading.Tasks
@ -176,7 +176,7 @@ let main args =
let _ = builder.Services.AddScoped<SqliteConnection>(fun sp -> Sqlite.Configuration.dbConn ())
let _ = builder.Services.AddScoped<IData, SQLiteData>()
// Use SQLite for caching as well
let cachePath = defaultArg (Option.ofObj (cfg.GetConnectionString "SQLiteCachePath")) "./session.db"
let cachePath = defaultArg (Option.ofObj (cfg.GetConnectionString "SQLiteCachePath")) "./data/session.db"
let _ = builder.Services.AddSqliteCache(fun o -> o.CachePath <- cachePath)
()
| :? PostgresData as postgres ->
@ -214,9 +214,12 @@ let main args =
printfn $"""Unrecognized command "{it}" - valid commands are:"""
showHelp ()
| None -> task {
// Load all themes in the application directory
for themeFile in Directory.EnumerateFiles(".", "*-theme.zip") do
do! Maintenance.loadTheme [| ""; themeFile |] app.Services
// Load admin and default themes, and all themes in the /themes directory
do! Maintenance.loadTheme [| ""; "./admin-theme.zip" |] app.Services
do! Maintenance.loadTheme [| ""; "./default-theme.zip" |] app.Services
if Directory.Exists "./themes" then
for themeFile in Directory.EnumerateFiles("./themes", "*-theme.zip") do
do! Maintenance.loadTheme [| ""; themeFile |] app.Services
let _ = app.UseForwardedHeaders()

View File

@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /mwl
COPY ./MyWebLog.sln ./
COPY ./Directory.Build.props ./
@ -9,7 +9,7 @@ RUN dotnet restore
COPY . ./
WORKDIR /mwl/MyWebLog
RUN dotnet publish -f net7.0 -c Release -r linux-x64
RUN dotnet publish -f net8.0 -c Release -r linux-x64
FROM alpine AS theme
RUN apk add --no-cache zip
@ -19,10 +19,11 @@ RUN zip default-theme.zip ./default-theme/*
COPY ./admin-theme ./admin-theme/
RUN zip admin-theme.zip ./admin-theme/*
FROM mcr.microsoft.com/dotnet/aspnet:7.0 as final
FROM mcr.microsoft.com/dotnet/aspnet:8.0 as final
WORKDIR /app
COPY --from=build /mwl/MyWebLog/bin/Release/net7.0/linux-x64/publish/ ./
COPY --from=build /mwl/MyWebLog/bin/Release/net8.0/linux-x64/publish/ ./
COPY --from=theme /themes/*.zip /app/
RUN mkdir data themes
EXPOSE 80
CMD [ "/app/MyWebLog" ]