-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
59 lines (53 loc) · 2.07 KB
/
main.ts
File metadata and controls
59 lines (53 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { App, cors, csp, csrf, staticFiles } from 'fresh';
import { headers } from './middleware.ts';
import { ArcRateLimiter } from './lib/rate-limiting/src/arc-rate-limiter.ts';
import { ORIGIN, State } from './lib/types/common.ts';
import { NoteDatabase } from './lib/database/note-database.ts';
import { defaultLogger } from './lib/logging.ts';
const serverSecret = Deno.env.get('ARC_SECRET');
const databasePath = Deno.env.get('DATABASE_PATH');
let noteDatabase: NoteDatabase;
if (!serverSecret) {
throw new Error('ARC_SECRET environment variable is not set');
}
try {
noteDatabase = await new NoteDatabase(databasePath).init();
defaultLogger.log(`Database Path Source: ${databasePath ? 'env' : 'default'}`);
} catch (error) {
defaultLogger.error('Failed to initialize NoteDatabase', error);
throw error;
}
// Configure rate limiter: 15 requests per minute, 5 min block duration
const rateLimiter = new ArcRateLimiter({
maxRequests: 15,
windowMs: 60 * 1000,
blockDurationMs: 5 * 60 * 1000,
identifier: 'vailnote-rate-limiter',
serverSecret,
});
export { noteDatabase };
export const app = new App<State>()
.use(staticFiles())
.use(cors({
origin: ORIGIN,
allowHeaders: ['Content-Type', 'Authorization'],
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
maxAge: 86400,
}))
.use(csrf({
origin: ORIGIN,
}))
.use(csp())
.use(rateLimiter.middleware()) // 15 requests per minute, 5 min block
.use(headers({
'Cross-Origin-Resource-Policy': 'same-site',
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin',
'Permissions-Policy':
'geolocation=(), camera=(), microphone=(), payment=(), usb=(), bluetooth=(), magnetometer=(), gyroscope=(), accelerometer=()',
'Referrer-Policy': 'strict-origin-when-cross-origin',
'X-Content-Type-Options': 'nosniff',
'X-Frame-Options': 'DENY',
'Strict-Transport-Security': 'max-age=63072000; includeSubDomains; preload',
}))
.fsRoutes();