summaryrefslogtreecommitdiff
path: root/lib/server.ts
blob: 9885bf3437d6dcdb303cff9b06501735bdc9672a (plain)
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
import {
    FourOhFourActivityImpl,
    HealthCheckActivityImpl,
    type HealthChecker,
    type IFourOhFourActivity,
    type IHealthCheckActivity,
    type ITraceable,
    PenguenoRequest,
    Server,
    type ServerTrace,
} from '@emprespresso/pengueno';
import { healthCheck as _healthCheck, EmailActivityImpl, IEmailActivity, Inbox, Outbox } from './index';

export class UptimeServer implements Server {
    constructor(
        healthCheck: HealthChecker = _healthCheck,
        private readonly healthCheckActivity: IHealthCheckActivity = new HealthCheckActivityImpl(healthCheck),
        private readonly emailActivity: IEmailActivity = new EmailActivityImpl(
            (a) => Outbox.from(a),
            (b) => Inbox.from(b),
        ),
        private readonly fourOhFourActivity: IFourOhFourActivity = new FourOhFourActivityImpl(),
    ) {}

    public serve(req: ITraceable<PenguenoRequest, ServerTrace>) {
        const url = new URL(req.get().req.url);
        if (url.pathname === '/health') {
            return this.healthCheckActivity.checkHealth(req);
        }
        if (url.pathname === '/email') {
            return this.emailActivity.runMail(req);
        }
        return this.fourOhFourActivity.fourOhFour(req);
    }
}