-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
executable file
·99 lines (94 loc) · 2.47 KB
/
server.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env -S deno serve --env-file --allow-env --watch-hmr --port 7770 --allow-net
// allow-net: api.crossref.org,akvaplanists.deno.dev,api.deno.com,api.cristin.no
import "./cron.ts";
import type { Pub } from "./pub/types.ts";
import type { CrossrefWork } from "./crossref/types.ts";
import { type Route, route } from "@std/http";
import {
crossrefWork,
doiPub,
getKv,
hdlPub,
nvaPub,
pathParam,
publicationMetadataFromNva,
send404,
send405,
streamKvListValues,
} from "./server_handlers.ts";
const routes: Route[] = [
{
pattern: new URLPattern({ pathname: "/akvaplanist/:id" }),
handler: (_request, _info, result) =>
streamKvListValues(
{ prefix: ["akvaplanist", pathParam(result, "id") as string] },
result,
),
},
{
pattern: new URLPattern({ pathname: "/by/:id" }),
handler: (_request, _info, result) =>
streamKvListValues(
{ prefix: ["by", pathParam(result, "id") as string] },
result,
),
},
{
pattern: new URLPattern({ pathname: "/crossref" }),
handler: (_request, _info, result) =>
streamKvListValues<CrossrefWork>({ prefix: ["crossref"] }, result),
},
{
pattern: new URLPattern({
pathname: "/crossref(/https\:\/\/doi\.org)?/:doi(10\.*)",
}),
handler: crossrefWork,
},
{
pattern: new URLPattern({ pathname: "/pub" }),
handler: (_request, _info, result) =>
streamKvListValues<Pub>({ prefix: ["pub"] }, result),
},
{
pattern: new URLPattern({
pathname: "/(pub|doi)?(/https\:\/\/doi\.org)?/10\.:prefix/:suffix*",
}),
handler: doiPub,
},
{
pattern: new URLPattern({
pathname:
"/pub/(hdl|https\:\/\/hdl\.handle\.net)/:prefix([0-9]+)/:suffix([0-9]+)",
}),
handler: hdlPub,
},
{
pattern: new URLPattern({
pathname:
"/pub(/nva|/https\:\/\/api\.nva\.unit\.no\/publication|/https\:\/\/api\.test\.nva\.aws\.unit\.no\/publication)/:id",
}),
handler: nvaPub,
},
{
pattern: new URLPattern({ pathname: "/nva" }),
handler: (_request, _info, result) =>
streamKvListValues<Pub>({ prefix: ["nva"] }, result),
},
{
pattern: new URLPattern({
pathname: "/nva/:id",
}),
handler: publicationMetadataFromNva,
},
];
export default {
fetch(request) {
switch (request.method) {
case "GET":
case "HEAD":
return route(routes, send404)(request);
default:
return send405();
}
},
} satisfies Deno.ServeDefaultExport;