Add server, routes, views, CLI, CSS, and integration tests
- Server factory with Fastify plugins (JWT, cookie, multipart, formbody, static) - Auth middleware: requireAuth preHandler (401 for API, redirect for pages) - Auth API routes: POST /api/v1/auth/login, POST /api/v1/auth/logout - File API routes: GET/POST /api/v1/files, DELETE /api/v1/files/:id - Page routes: /, /login, /logout, /upload, /files, /files/:id/delete, /f/:id, /f/:id/raw - HTML views: layout, login, upload, file-list, file-view, not-found - CLI register-user script - public/style.css dark theme - Test helpers: createTestApp, loginAs, buildMultipart - Integration tests for auth API, file API, and page routes (51 tests passing) - Update CLAUDE.md with red/green TDD and commit instructions Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
46
src/server.ts
Normal file
46
src/server.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import Fastify from 'fastify';
|
||||
import fastifyCookie from '@fastify/cookie';
|
||||
import fastifyJwt from '@fastify/jwt';
|
||||
import fastifyMultipart from '@fastify/multipart';
|
||||
import fastifyFormbody from '@fastify/formbody';
|
||||
import fastifyStatic from '@fastify/static';
|
||||
import { join } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import type Database from 'better-sqlite3';
|
||||
import type { Config } from './config.ts';
|
||||
import { createLogger } from './middleware/logging.ts';
|
||||
import { authApiRoutes } from './routes/api/v1/auth.ts';
|
||||
import { filesApiRoutes } from './routes/api/v1/files.ts';
|
||||
import { pageRoutes } from './routes/pages.ts';
|
||||
|
||||
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
||||
|
||||
interface ServerDeps {
|
||||
config: Config;
|
||||
db: Database.Database;
|
||||
}
|
||||
|
||||
export function createServer({ config, db }: ServerDeps) {
|
||||
const app = Fastify({ logger: false, trustProxy: config.trustProxy });
|
||||
const logger = createLogger(config.logFile);
|
||||
|
||||
app.register(fastifyCookie);
|
||||
app.register(fastifyJwt, {
|
||||
secret: config.jwtSecret,
|
||||
cookie: { cookieName: 'token', signed: false },
|
||||
});
|
||||
app.register(fastifyFormbody);
|
||||
app.register(fastifyMultipart, { limits: { fileSize: config.maxFileSize } });
|
||||
app.register(fastifyStatic, {
|
||||
root: join(__dirname, '..', 'public'),
|
||||
prefix: '/public/',
|
||||
});
|
||||
|
||||
const deps = { db, config, logger };
|
||||
|
||||
app.register(authApiRoutes, { prefix: '/api/v1/auth', deps });
|
||||
app.register(filesApiRoutes, { prefix: '/api/v1/files', deps });
|
||||
app.register(pageRoutes, { prefix: '/', deps });
|
||||
|
||||
return app;
|
||||
}
|
||||
Reference in New Issue
Block a user