Skip to content

Your favorite shift management application!

feat: implement versioning system with version info generation and display in UI (#2)

panteLd9f48a33182f
diff --git a/.env.example b/.env.example
new file mode 100644
index 0000000..0f56e40
--- /dev/null
+++ b/.env.example
@@ -0,0 +1,22 @@
+# BetterShift Environment Variables Example
+
+# Database Configuration
+DATABASE_URL=file:./data/sqlite.db
+
+# GitHub API Configuration (for version endpoint)
+GITHUB_REPO_OWNER=panteLx
+GITHUB_REPO_NAME=BetterShift
+GITHUB_BRANCH=main
+# GITHUB_TOKEN=your_github_token_here # Optional: Increases GitHub API rate limits from 60 to 5000 requests/hour
+
+# Cache Settings (in seconds)
+VERSION_CACHE_DURATION=3600 # Version info cache: 1 hour in seconds
+GITHUB_API_REVALIDATE=3600 # GitHub API revalidation: 1 hour in seconds
+
+# Internationalization (i18n)
+DEFAULT_LOCALE=en # Default language if browser detection fails. Options: en, de
+
+# Application Settings
+NODE_ENV=production
+PORT=3000
+HOSTNAME=0.0.0.0
diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml
index 246d763..edec831 100644
--- a/.github/workflows/docker-publish.yml
+++ b/.github/workflows/docker-publish.yml
@@ -61,6 +61,3 @@ jobs:
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- build-args: |
- BUILD_DATE=${{ steps.meta.outputs.created }}
- VCS_REF=${{ github.sha }}
diff --git a/.gitignore b/.gitignore
index 59bca00..d8cde63 100644
--- a/.gitignore
+++ b/.gitignore
@@ -47,4 +47,7 @@ sqlite.db-journal
data/
# docker
-docker-compose.override.yml
\ No newline at end of file
+docker-compose.override.yml
+
+# version info (generated at build time)
+public/version.json
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
index c955e1d..5ee4ba0 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -52,4 +52,4 @@ ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Start the application
-CMD ["node", "server.js"]
+CMD ["node", "server.js"]
\ No newline at end of file
diff --git a/app/api/version/route.ts b/app/api/version/route.ts
new file mode 100644
index 0000000..50ecf27
--- /dev/null
+++ b/app/api/version/route.ts
@@ -0,0 +1,114 @@
+import { NextResponse } from "next/server";
+
+// Cache version info for 1 hour
+let cachedVersion: {
+ version: string;
+ branch: string;
+ commitHash: string;
+ buildDate: string;
+ timestamp: number;
+} | null = null;
+
+const CACHE_SECONDS = parseInt(
+ process.env.VERSION_CACHE_DURATION || "3600",
+ 10
+); // Default: 1 hour in seconds
+const CACHE_DURATION = CACHE_SECONDS * 1000; // Convert to milliseconds for in-memory TTL
+const GITHUB_API_REVALIDATE = parseInt(
+ process.env.GITHUB_API_REVALIDATE || "3600",
+ 10
+); // Default: 1 hour in seconds
+
+const GITHUB_REPO_OWNER = process.env.GITHUB_REPO_OWNER || "panteLx";
+const GITHUB_REPO_NAME = process.env.GITHUB_REPO_NAME || "BetterShift";
+const GITHUB_BRANCH = process.env.GITHUB_BRANCH || "main";
+const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
+
+async function fetchGitHubVersion() {
+ try {
+ // Get latest commit from configured branch
+ const response = await fetch(
+ `https://api.github.com/repos/${GITHUB_REPO_OWNER}/${GITHUB_REPO_NAME}/commits/${GITHUB_BRANCH}`,
+ {
+ headers: {
+ Accept: "application/vnd.github.v3+json",
+ ...(GITHUB_TOKEN && { Authorization: `Bearer ${GITHUB_TOKEN}` }),
+ },
+ next: { revalidate: GITHUB_API_REVALIDATE },
+ }
+ );
+
+ if (!response.ok) {
+ throw new Error(`GitHub API error: ${response.status}`);
+ }
+
+ const data = await response.json();
+ const commitHash = data.sha.substring(0, 7);
+ const branch = GITHUB_BRANCH;
+
+ return {
+ version: `${branch}-${commitHash}`,
+ branch,
+ commitHash,
+ buildDate: data.commit.committer.date,
+ timestamp: Date.now(),
+ };
+ } catch (error) {
+ console.error("Failed to fetch GitHub version:", error);
+ return null;
+ }
+}
+
+export async function GET() {
+ // Check if we have a valid cached version
+ if (cachedVersion && Date.now() - cachedVersion.timestamp < CACHE_DURATION) {
+ return NextResponse.json(
+ {
+ version: cachedVersion.version,
+ branch: cachedVersion.branch,
+ commitHash: cachedVersion.commitHash,
+ buildDate: cachedVersion.buildDate,
+ },
+ {
+ headers: {
+ "Cache-Control": `public, max-age=${CACHE_SECONDS}`,
+ },
+ }
+ );
+ }
+
+ // Fetch new version from GitHub
+ const versionInfo = await fetchGitHubVersion();
+
+ if (versionInfo) {
+ cachedVersion = versionInfo;
+ return NextResponse.json(
+ {
+ version: versionInfo.version,
+ branch: versionInfo.branch,
+ commitHash: versionInfo.commitHash,
+ buildDate: versionInfo.buildDate,
+ },
+ {
+ headers: {
+ "Cache-Control": `public, max-age=${CACHE_SECONDS}`,
+ },
+ }
+ );
+ }
+
+ // Fallback to dev-local if GitHub API fails
+ return NextResponse.json(
+ {
+ version: "dev-local",
+ branch: "unknown",
+ commitHash: "unknown",
+ buildDate: new Date().toISOString(),
+ },
+ {
+ headers: {
+ "Cache-Control": "public, max-age=60", // Shorter cache on error
+ },
+ }
+ );
+}
diff --git a/app/page.tsx b/app/page.tsx
index a6b451b..3768636 100644
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -96,10 +96,23 @@ function HomeContent() {
const [isConnected, setIsConnected] = useState(true);
const lastSyncTimeRef = useRef<number>(Date.now());
const disconnectTimeRef = useRef<number | null>(null);
+ const [version, setVersion] = useState<string | null>(null);
+
+ // Fetch version information
+ const fetchVersion = async () => {
+ try {
+ const response = await fetch("/api/version");
+ const data = await response.json();
+ setVersion(data.version);
+ } catch (error) {
+ console.error("Failed to fetch version:", error);
+ }
+ };
// Fetch calendars on mount and setup cleanup
useEffect(() => {
fetchCalendars();
+ fetchVersion();
return () => {
if (eventSourceRef.current) {
@@ -1565,8 +1578,13 @@ function HomeContent() {
{/* Footer */}
<div className="border-t bg-background mt-auto">
- <div className="container max-w-4xl mx-auto p-3 sm:p-4 flex justify-center">
+ <div className="container max-w-4xl mx-auto p-3 sm:p-4 flex justify-between items-center">
<LanguageSwitcher />
+ {version && (
+ <div className="text-xs text-muted-foreground font-mono">
+ {t("version.label")}: {version}
+ </div>
+ )}
</div>
</div>
</div>
diff --git a/docker-compose.yml b/docker-compose.yml
index 2a794c0..c73aa05 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -11,9 +11,8 @@ services:
- "3000:3000"
volumes:
- ./data:/app/data
- environment:
- - NODE_ENV=production
- - DATABASE_URL=file:./data/sqlite.db
+ env_file:
+ - .env
healthcheck:
test:
[
diff --git a/lib/i18n.ts b/lib/i18n.ts
index dcdf5f4..79cb5bc 100644
--- a/lib/i18n.ts
+++ b/lib/i18n.ts
@@ -4,7 +4,13 @@ import { cookies, headers } from "next/headers";
export const locales = ["de", "en"] as const;
export type Locale = (typeof locales)[number];
-export const defaultLocale: Locale = "en";
+const configuredDefaultLocale = process.env.DEFAULT_LOCALE as
+ | Locale
+ | undefined;
+export const defaultLocale: Locale =
+ configuredDefaultLocale && locales.includes(configuredDefaultLocale)
+ ? configuredDefaultLocale
+ : "en";
function getLocaleFromNavigator(acceptLanguage: string | null): Locale {
if (!acceptLanguage) return defaultLocale;
diff --git a/messages/de.json b/messages/de.json
index a0e332f..6bb6919 100644
--- a/messages/de.json
+++ b/messages/de.json
@@ -154,5 +154,8 @@
"welcome": "Willkommen bei BetterShift",
"description": "Erstelle deinen ersten Kalender, um deine Schichten zu verwalten.",
"createCalendar": "Kalender erstellen"
+ },
+ "version": {
+ "label": "Version"
}
}
diff --git a/messages/en.json b/messages/en.json
index c52a495..8a52dee 100644
--- a/messages/en.json
+++ b/messages/en.json
@@ -154,5 +154,8 @@
"welcome": "Welcome to BetterShift",
"description": "Create your first calendar to manage your shifts.",
"createCalendar": "Create Calendar"
+ },
+ "version": {
+ "label": "Version"
}
}