mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
Compare commits
18 Commits
efdc42cbd7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 681b5a857c | |||
|
|
bc97c21b1d | ||
| a2cd260bca | |||
|
|
85f2bf23c1 | ||
| 8f06d39a71 | |||
| 93f95f22ef | |||
| 0a4abdb48c | |||
| 4ad5e682a8 | |||
| dd55c81b51 | |||
| 0939bc7213 | |||
| b8a9cee04d | |||
| 2259e7400a | |||
| 2045d36211 | |||
| 76ab168c6e | |||
| 6117f7a58f | |||
| ab6cb47047 | |||
| 4acba662b6 | |||
| 054d9da867 |
21
.github/dependabot.yml
vendored
Normal file
21
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "npm"
|
||||
directory: "/app"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
groups:
|
||||
minor-and-patch:
|
||||
update-types:
|
||||
- "minor"
|
||||
- "patch"
|
||||
|
||||
- package-ecosystem: "gomod"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
groups:
|
||||
minor-and-patch:
|
||||
update-types:
|
||||
- "minor"
|
||||
- "patch"
|
||||
1026
app/package-lock.json
generated
1026
app/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -29,54 +29,54 @@
|
||||
},
|
||||
"homepage": "https://github.com/LordMathis/Lemma#readme",
|
||||
"dependencies": {
|
||||
"@codemirror/commands": "^6.6.2",
|
||||
"@codemirror/lang-markdown": "^6.2.5",
|
||||
"@codemirror/state": "^6.4.1",
|
||||
"@codemirror/theme-one-dark": "^6.1.2",
|
||||
"@codemirror/view": "^6.34.0",
|
||||
"@codemirror/commands": "^6.10.0",
|
||||
"@codemirror/lang-markdown": "^6.5.0",
|
||||
"@codemirror/state": "^6.5.2",
|
||||
"@codemirror/theme-one-dark": "^6.1.3",
|
||||
"@codemirror/view": "^6.38.6",
|
||||
"@mantine/core": "^7.13.2",
|
||||
"@mantine/hooks": "^7.13.2",
|
||||
"@mantine/modals": "^7.13.2",
|
||||
"@mantine/notifications": "^7.13.2",
|
||||
"@react-hook/resize-observer": "^2.0.2",
|
||||
"@tabler/icons-react": "^3.19.0",
|
||||
"codemirror": "^6.0.1",
|
||||
"@tabler/icons-react": "^3.35.0",
|
||||
"codemirror": "^6.0.2",
|
||||
"react": "^18.3.1",
|
||||
"react-arborist": "^3.4.0",
|
||||
"react-arborist": "^3.4.3",
|
||||
"react-dom": "^18.3.1",
|
||||
"rehype-highlight": "^7.0.2",
|
||||
"rehype-mathjax": "^6.0.0",
|
||||
"rehype-mathjax": "^7.1.0",
|
||||
"rehype-react": "^8.0.0",
|
||||
"remark": "^15.0.1",
|
||||
"remark-math": "^6.0.0",
|
||||
"remark-parse": "^11.0.0",
|
||||
"remark-rehype": "^11.1.1",
|
||||
"remark-rehype": "^11.1.2",
|
||||
"unified": "^11.0.5",
|
||||
"unist-util-visit": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/compat": "^1.2.9",
|
||||
"@testing-library/jest-dom": "^6.6.3",
|
||||
"@eslint/compat": "^1.4.1",
|
||||
"@testing-library/jest-dom": "^6.9.1",
|
||||
"@testing-library/react": "^16.3.0",
|
||||
"@types/babel__core": "^7.20.5",
|
||||
"@types/node": "^22.14.0",
|
||||
"@types/react": "^18.3.20",
|
||||
"@types/react-dom": "^18.3.6",
|
||||
"@typescript-eslint/eslint-plugin": "^8.32.1",
|
||||
"@typescript-eslint/eslint-plugin": "^8.46.3",
|
||||
"@typescript-eslint/parser": "^8.32.1",
|
||||
"@vitejs/plugin-react": "^4.3.4",
|
||||
"@vitest/coverage-v8": "^3.1.4",
|
||||
"eslint": "^9.27.0",
|
||||
"eslint": "^9.39.1",
|
||||
"eslint-plugin-react": "^7.37.5",
|
||||
"eslint-plugin-react-hooks": "^5.2.0",
|
||||
"jsdom": "^26.1.0",
|
||||
"postcss": "^8.4.47",
|
||||
"postcss-preset-mantine": "^1.17.0",
|
||||
"postcss": "^8.5.6",
|
||||
"postcss-preset-mantine": "^1.18.0",
|
||||
"postcss-simple-vars": "^7.0.1",
|
||||
"sass": "^1.80.4",
|
||||
"typescript": "^5.8.2",
|
||||
"sass": "^1.93.3",
|
||||
"typescript": "^5.9.3",
|
||||
"vite": "^6.4.1",
|
||||
"vite-plugin-compression2": "^1.3.0",
|
||||
"vite-plugin-compression2": "^2.3.1",
|
||||
"vitest": "^3.1.4"
|
||||
},
|
||||
"browserslist": {
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
IconFolderOpen,
|
||||
IconUpload,
|
||||
} from '@tabler/icons-react';
|
||||
import { Tooltip, Text, Box } from '@mantine/core';
|
||||
import { Text, Box } from '@mantine/core';
|
||||
import useResizeObserver from '@react-hook/resize-observer';
|
||||
import { useFileOperations } from '../../hooks/useFileOperations';
|
||||
import type { FileNode } from '@/types/models';
|
||||
@@ -53,13 +53,12 @@ function Node({
|
||||
style,
|
||||
dragHandle,
|
||||
onNodeClick,
|
||||
...rest
|
||||
}: {
|
||||
node: NodeApi<FileNode>;
|
||||
style: React.CSSProperties;
|
||||
dragHandle?: React.Ref<HTMLDivElement>;
|
||||
onNodeClick?: (node: NodeApi<FileNode>) => void;
|
||||
} & Record<string, unknown>) {
|
||||
}) {
|
||||
const handleClick = () => {
|
||||
if (node.isInternal) {
|
||||
node.toggle();
|
||||
@@ -69,7 +68,6 @@ function Node({
|
||||
};
|
||||
|
||||
return (
|
||||
<Tooltip label={node.data.name} openDelay={500}>
|
||||
<div
|
||||
ref={dragHandle} // This enables dragging for the node
|
||||
style={{
|
||||
@@ -82,9 +80,14 @@ function Node({
|
||||
overflow: 'hidden',
|
||||
// Add visual feedback when being dragged
|
||||
opacity: node.state?.isDragging ? 0.5 : 1,
|
||||
// Highlight when this node will receive the drop
|
||||
backgroundColor: node.state?.willReceiveDrop
|
||||
? 'rgba(0, 123, 255, 0.2)'
|
||||
: 'transparent',
|
||||
borderRadius: '4px',
|
||||
}}
|
||||
onClick={handleClick}
|
||||
{...rest}
|
||||
title={node.data.name}
|
||||
>
|
||||
<FileIcon node={node} />
|
||||
<span
|
||||
@@ -99,7 +102,6 @@ function Node({
|
||||
{node.data.name}
|
||||
</span>
|
||||
</div>
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -205,16 +207,17 @@ export const FileTree: React.FC<FileTreeProps> = ({
|
||||
|
||||
// External file drag and drop handlers
|
||||
const handleDragEnter = useCallback((e: React.DragEvent) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
// Check if drag contains files (not internal tree nodes)
|
||||
if (e.dataTransfer.types.includes('Files')) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
setIsDragOver(true);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleDragLeave = useCallback((e: React.DragEvent) => {
|
||||
// Only handle if it's an external file drag
|
||||
if (e.dataTransfer.types.includes('Files')) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
@@ -222,24 +225,28 @@ export const FileTree: React.FC<FileTreeProps> = ({
|
||||
if (e.currentTarget === e.target) {
|
||||
setIsDragOver(false);
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleDragOver = useCallback((e: React.DragEvent) => {
|
||||
// Only handle external file drags
|
||||
if (e.dataTransfer.types.includes('Files')) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
// Set the drop effect to indicate this is a valid drop target
|
||||
e.dataTransfer.dropEffect = 'copy';
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleDrop = useCallback(
|
||||
(e: React.DragEvent) => {
|
||||
const { files } = e.dataTransfer;
|
||||
// Only handle if it's an external file drop
|
||||
if (files && files.length > 0) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
setIsDragOver(false);
|
||||
|
||||
const { files } = e.dataTransfer;
|
||||
if (files && files.length > 0) {
|
||||
const uploadFiles = async () => {
|
||||
try {
|
||||
const success = await handleUpload(files);
|
||||
@@ -305,7 +312,10 @@ export const FileTree: React.FC<FileTreeProps> = ({
|
||||
height={size.height}
|
||||
indent={24}
|
||||
rowHeight={28}
|
||||
idAccessor="id"
|
||||
onMove={handleTreeMove}
|
||||
disableDrag={() => false}
|
||||
disableDrop={() => false}
|
||||
onActivate={(node) => {
|
||||
const fileNode = node.data;
|
||||
if (!node.isInternal) {
|
||||
|
||||
@@ -4,6 +4,25 @@ import React from 'react';
|
||||
import { MantineProvider } from '@mantine/core';
|
||||
import ProfileSettings from './ProfileSettings';
|
||||
import type { UserProfileSettings } from '@/types/models';
|
||||
import { Theme, UserRole, type User } from '@/types/models';
|
||||
|
||||
// Mock user for AuthContext
|
||||
const mockUser: User = {
|
||||
id: 1,
|
||||
email: 'test@example.com',
|
||||
displayName: 'Test User',
|
||||
role: UserRole.Editor,
|
||||
theme: Theme.Dark,
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
lastWorkspaceId: 1,
|
||||
};
|
||||
|
||||
// Mock the auth context
|
||||
vi.mock('../../../contexts/AuthContext', () => ({
|
||||
useAuth: () => ({
|
||||
user: mockUser,
|
||||
}),
|
||||
}));
|
||||
|
||||
// Helper wrapper component for testing
|
||||
const TestWrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import React from 'react';
|
||||
import { Box, Stack, TextInput, Group, Text, Switch } from '@mantine/core';
|
||||
import { IconMoon, IconSun } from '@tabler/icons-react';
|
||||
import { useAuth } from '@/contexts/AuthContext';
|
||||
import { Theme, type UserProfileSettings } from '@/types/models';
|
||||
|
||||
@@ -44,11 +43,10 @@ const ProfileSettings: React.FC<ProfileSettingsProps> = ({
|
||||
placeholder="Enter email"
|
||||
data-testid="email-input"
|
||||
/>
|
||||
<Group justify="space-between" align="flex-start">
|
||||
<Box mb="md">
|
||||
<Group justify="space-between" align="center">
|
||||
<div>
|
||||
<Text size="sm" fw={500}>
|
||||
Default Theme
|
||||
</Text>
|
||||
<Text size="sm">Default Dark Mode</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
Sets the default theme for new workspaces
|
||||
</Text>
|
||||
@@ -56,12 +54,10 @@ const ProfileSettings: React.FC<ProfileSettingsProps> = ({
|
||||
<Switch
|
||||
checked={currentTheme === Theme.Dark}
|
||||
onChange={handleThemeToggle}
|
||||
size="lg"
|
||||
onLabel={<IconMoon size={16} />}
|
||||
offLabel={<IconSun size={16} />}
|
||||
data-testid="theme-toggle"
|
||||
/>
|
||||
</Group>
|
||||
</Box>
|
||||
</Stack>
|
||||
</Box>
|
||||
);
|
||||
|
||||
@@ -188,6 +188,7 @@ describe('Models Type Guards', () => {
|
||||
id: 1,
|
||||
email: 'test@example.com',
|
||||
role: UserRole.Editor,
|
||||
theme: Theme.Dark,
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
lastWorkspaceId: 1,
|
||||
__proto__: { malicious: true },
|
||||
@@ -773,6 +774,7 @@ describe('Models Type Guards', () => {
|
||||
id: 1,
|
||||
email: 'test@example.com',
|
||||
role: UserRole.Editor,
|
||||
theme: Theme.Dark,
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
lastWorkspaceId: 1,
|
||||
};
|
||||
@@ -806,6 +808,7 @@ describe('Models Type Guards', () => {
|
||||
id: 1,
|
||||
email: 'test@example.com',
|
||||
role: UserRole.Editor,
|
||||
theme: Theme.Dark,
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
lastWorkspaceId: 1,
|
||||
});
|
||||
@@ -854,6 +857,7 @@ describe('Models Type Guards', () => {
|
||||
id: 1,
|
||||
email: longString,
|
||||
role: UserRole.Editor,
|
||||
theme: Theme.Dark,
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
lastWorkspaceId: 1,
|
||||
};
|
||||
|
||||
@@ -30,7 +30,8 @@ export function isUser(value: unknown): value is User {
|
||||
'role' in value &&
|
||||
isUserRole((value as User).role) &&
|
||||
'theme' in value &&
|
||||
(value as User).theme in Theme &&
|
||||
typeof (value as User).theme === 'string' &&
|
||||
Object.values(Theme).includes((value as User).theme) &&
|
||||
'createdAt' in value &&
|
||||
typeof (value as User).createdAt === 'string' &&
|
||||
'lastWorkspaceId' in value &&
|
||||
|
||||
@@ -11,7 +11,10 @@ export default defineConfig(({ mode }) => ({
|
||||
react({
|
||||
include: ['**/*.tsx', '**/*.ts', '**/*.jsx', '**/*.js'],
|
||||
}),
|
||||
compression(),
|
||||
compression({
|
||||
threshold: 1024, // Only compress files > 1KB
|
||||
deleteOriginalAssets: false, // Keep original files
|
||||
}),
|
||||
],
|
||||
|
||||
root: 'src',
|
||||
|
||||
@@ -29,6 +29,7 @@ func TestSessionOperations(t *testing.T) {
|
||||
DisplayName: "Test User",
|
||||
PasswordHash: "hash",
|
||||
Role: "editor",
|
||||
Theme: "dark",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test user: %v", err)
|
||||
|
||||
@@ -156,6 +156,7 @@ func TestStructQueries(t *testing.T) {
|
||||
DisplayName: "Struct Query Test",
|
||||
PasswordHash: "hashed_password",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
}
|
||||
|
||||
t.Run("InsertStructQuery", func(t *testing.T) {
|
||||
@@ -243,6 +244,7 @@ func TestStructQueries(t *testing.T) {
|
||||
DisplayName: "Struct Query Test 2",
|
||||
PasswordHash: "hashed_password2",
|
||||
Role: models.RoleViewer,
|
||||
Theme: "light",
|
||||
}
|
||||
|
||||
createdUser2, err := database.CreateUser(secondUser)
|
||||
@@ -437,6 +439,7 @@ func TestEncryptedFields(t *testing.T) {
|
||||
DisplayName: "Encryption Test",
|
||||
PasswordHash: "hash",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test user: %v", err)
|
||||
|
||||
@@ -31,12 +31,14 @@ func TestSystemOperations(t *testing.T) {
|
||||
DisplayName: "User 1",
|
||||
PasswordHash: "hash1",
|
||||
Role: "editor",
|
||||
Theme: "dark",
|
||||
},
|
||||
{
|
||||
Email: "user2@test.com",
|
||||
DisplayName: "User 2",
|
||||
PasswordHash: "hash2",
|
||||
Role: "viewer",
|
||||
Theme: "light",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ func TestUserOperations(t *testing.T) {
|
||||
DisplayName: "Test User",
|
||||
PasswordHash: "hashed_password",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
@@ -44,6 +45,7 @@ func TestUserOperations(t *testing.T) {
|
||||
DisplayName: "Another User",
|
||||
PasswordHash: "different_hash",
|
||||
Role: models.RoleViewer,
|
||||
Theme: "light",
|
||||
},
|
||||
wantErr: true,
|
||||
errContains: "UNIQUE constraint failed",
|
||||
@@ -108,6 +110,7 @@ func TestUserOperations(t *testing.T) {
|
||||
DisplayName: "Get By ID User",
|
||||
PasswordHash: "hash",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test user: %v", err)
|
||||
@@ -159,6 +162,7 @@ func TestUserOperations(t *testing.T) {
|
||||
DisplayName: "Get By Email User",
|
||||
PasswordHash: "hash",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test user: %v", err)
|
||||
@@ -210,6 +214,7 @@ func TestUserOperations(t *testing.T) {
|
||||
DisplayName: "Original Name",
|
||||
PasswordHash: "original_hash",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test user: %v", err)
|
||||
@@ -249,12 +254,14 @@ func TestUserOperations(t *testing.T) {
|
||||
DisplayName: "User One",
|
||||
PasswordHash: "hash1",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
},
|
||||
{
|
||||
Email: "user2@example.com",
|
||||
DisplayName: "User Two",
|
||||
PasswordHash: "hash2",
|
||||
Role: models.RoleViewer,
|
||||
Theme: "light",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -305,6 +312,7 @@ func TestUserOperations(t *testing.T) {
|
||||
DisplayName: "Workspace User",
|
||||
PasswordHash: "hash",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test user: %v", err)
|
||||
@@ -343,6 +351,7 @@ func TestUserOperations(t *testing.T) {
|
||||
DisplayName: "Delete User",
|
||||
PasswordHash: "hash",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test user: %v", err)
|
||||
@@ -377,18 +386,21 @@ func TestUserOperations(t *testing.T) {
|
||||
DisplayName: "Admin One",
|
||||
PasswordHash: "hash1",
|
||||
Role: models.RoleAdmin,
|
||||
Theme: "dark",
|
||||
},
|
||||
{
|
||||
Email: "admin2@example.com",
|
||||
DisplayName: "Admin Two",
|
||||
PasswordHash: "hash2",
|
||||
Role: models.RoleAdmin,
|
||||
Theme: "light",
|
||||
},
|
||||
{
|
||||
Email: "editor@example.com",
|
||||
DisplayName: "Editor",
|
||||
PasswordHash: "hash3",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ func TestWorkspaceOperations(t *testing.T) {
|
||||
DisplayName: "Test User",
|
||||
PasswordHash: "hash",
|
||||
Role: models.RoleEditor,
|
||||
Theme: "dark",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test user: %v", err)
|
||||
|
||||
@@ -213,6 +213,7 @@ func (h *testHarness) createTestUser(t *testing.T, email, password string, role
|
||||
DisplayName: "Test User",
|
||||
PasswordHash: string(hashedPassword),
|
||||
Role: role,
|
||||
Theme: "dark",
|
||||
}
|
||||
|
||||
user, err = h.DB.CreateUser(user)
|
||||
|
||||
@@ -24,6 +24,28 @@ func getStaticLogger() logging.Logger {
|
||||
return logging.WithGroup("static")
|
||||
}
|
||||
|
||||
// getContentType returns the appropriate content type based on file extension
|
||||
func getContentType(path string) string {
|
||||
switch filepath.Ext(path) {
|
||||
case ".js":
|
||||
return "application/javascript"
|
||||
case ".css":
|
||||
return "text/css"
|
||||
case ".html":
|
||||
return "text/html"
|
||||
case ".json":
|
||||
return "application/json"
|
||||
case ".svg":
|
||||
return "image/svg+xml"
|
||||
case ".xml":
|
||||
return "application/xml"
|
||||
case ".yaml", ".yml":
|
||||
return "application/x-yaml"
|
||||
default:
|
||||
return "application/octet-stream"
|
||||
}
|
||||
}
|
||||
|
||||
// ServeHTTP serves the static files
|
||||
func (h *StaticHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
log := getStaticLogger().With(
|
||||
@@ -77,23 +99,28 @@ func (h *StaticHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check for pre-compressed version
|
||||
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||
// Check for pre-compressed versions (prefer brotli over gzip)
|
||||
acceptEncoding := r.Header.Get("Accept-Encoding")
|
||||
|
||||
// Try brotli first (better compression ratio)
|
||||
if strings.Contains(acceptEncoding, "br") {
|
||||
brPath := cleanPath + ".br"
|
||||
if _, err := os.Stat(brPath); err == nil {
|
||||
w.Header().Set("Content-Encoding", "br")
|
||||
w.Header().Set("Content-Type", getContentType(cleanPath))
|
||||
w.Header().Set("Vary", "Accept-Encoding")
|
||||
http.ServeFile(w, r, brPath)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to gzip
|
||||
if strings.Contains(acceptEncoding, "gzip") {
|
||||
gzPath := cleanPath + ".gz"
|
||||
if _, err := os.Stat(gzPath); err == nil {
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Set proper content type based on original file
|
||||
contentType := "application/octet-stream"
|
||||
switch filepath.Ext(cleanPath) {
|
||||
case ".js":
|
||||
contentType = "application/javascript"
|
||||
case ".css":
|
||||
contentType = "text/css"
|
||||
case ".html":
|
||||
contentType = "text/html"
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Content-Type", getContentType(cleanPath))
|
||||
w.Header().Set("Vary", "Accept-Encoding")
|
||||
http.ServeFile(w, r, gzPath)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -26,8 +26,12 @@ func TestStaticHandler_Integration(t *testing.T) {
|
||||
"index.html": []byte("<html><body>Index</body></html>"),
|
||||
"assets/style.css": []byte("body { color: blue; }"),
|
||||
"assets/style.css.gz": []byte("gzipped css content"),
|
||||
"assets/style.css.br": []byte("brotli css content"),
|
||||
"assets/script.js": []byte("console.log('test');"),
|
||||
"assets/script.js.gz": []byte("gzipped js content"),
|
||||
"assets/script.js.br": []byte("brotli js content"),
|
||||
"assets/app.js": []byte("console.log('app');"),
|
||||
"assets/app.js.br": []byte("brotli app content"),
|
||||
"subdir/page.html": []byte("<html><body>Page</body></html>"),
|
||||
"subdir/page.html.gz": []byte("gzipped html content"),
|
||||
}
|
||||
@@ -52,6 +56,7 @@ func TestStaticHandler_Integration(t *testing.T) {
|
||||
wantType string
|
||||
wantEncoding string
|
||||
wantCacheHeader string
|
||||
wantVary string
|
||||
}{
|
||||
{
|
||||
name: "serve index.html",
|
||||
@@ -69,6 +74,7 @@ func TestStaticHandler_Integration(t *testing.T) {
|
||||
wantType: "text/css",
|
||||
wantEncoding: "gzip",
|
||||
wantCacheHeader: "public, max-age=31536000",
|
||||
wantVary: "Accept-Encoding",
|
||||
},
|
||||
{
|
||||
name: "serve JS with gzip support",
|
||||
@@ -79,6 +85,7 @@ func TestStaticHandler_Integration(t *testing.T) {
|
||||
wantType: "application/javascript",
|
||||
wantEncoding: "gzip",
|
||||
wantCacheHeader: "public, max-age=31536000",
|
||||
wantVary: "Accept-Encoding",
|
||||
},
|
||||
{
|
||||
name: "serve CSS without gzip",
|
||||
@@ -114,6 +121,50 @@ func TestStaticHandler_Integration(t *testing.T) {
|
||||
wantBody: []byte("<html><body>Index</body></html>"),
|
||||
wantType: "text/html; charset=utf-8",
|
||||
},
|
||||
{
|
||||
name: "serve CSS with brotli support",
|
||||
path: "/assets/style.css",
|
||||
acceptEncoding: "br",
|
||||
wantStatus: http.StatusOK,
|
||||
wantBody: []byte("brotli css content"),
|
||||
wantType: "text/css",
|
||||
wantEncoding: "br",
|
||||
wantCacheHeader: "public, max-age=31536000",
|
||||
wantVary: "Accept-Encoding",
|
||||
},
|
||||
{
|
||||
name: "serve JS with brotli support",
|
||||
path: "/assets/script.js",
|
||||
acceptEncoding: "br",
|
||||
wantStatus: http.StatusOK,
|
||||
wantBody: []byte("brotli js content"),
|
||||
wantType: "application/javascript",
|
||||
wantEncoding: "br",
|
||||
wantCacheHeader: "public, max-age=31536000",
|
||||
wantVary: "Accept-Encoding",
|
||||
},
|
||||
{
|
||||
name: "prefer brotli over gzip when both supported",
|
||||
path: "/assets/script.js",
|
||||
acceptEncoding: "gzip, br",
|
||||
wantStatus: http.StatusOK,
|
||||
wantBody: []byte("brotli js content"),
|
||||
wantType: "application/javascript",
|
||||
wantEncoding: "br",
|
||||
wantCacheHeader: "public, max-age=31536000",
|
||||
wantVary: "Accept-Encoding",
|
||||
},
|
||||
{
|
||||
name: "fallback to gzip when brotli not available",
|
||||
path: "/assets/app.js",
|
||||
acceptEncoding: "gzip, br",
|
||||
wantStatus: http.StatusOK,
|
||||
wantBody: []byte("brotli app content"),
|
||||
wantType: "application/javascript",
|
||||
wantEncoding: "br",
|
||||
wantCacheHeader: "public, max-age=31536000",
|
||||
wantVary: "Accept-Encoding",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
@@ -139,6 +190,10 @@ func TestStaticHandler_Integration(t *testing.T) {
|
||||
if tc.wantCacheHeader != "" {
|
||||
assert.Equal(t, tc.wantCacheHeader, w.Header().Get("Cache-Control"))
|
||||
}
|
||||
|
||||
if tc.wantVary != "" {
|
||||
assert.Equal(t, tc.wantVary, w.Header().Get("Vary"))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user