-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
271 lines (247 loc) · 8.31 KB
/
api.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import {
Application,
existsSync,
join,
parse,
parseFlags,
path,
Router,
send,
Status,
} from "./deps.ts";
import { start_up } from "./manifest.ts";
import {
EndpointNotFoundError,
FileNotFoundError,
InvalidInputParameterError,
KONotFoundError,
} from "./exceptions.ts";
import { set_collection_path } from "./load.ts";
import jsonld from "https://cdn.skypack.dev/jsonld";
let manifest: { [key: string]: string }[] = [{}];
let routing_dictionary: {
[key: string]: {
data: {
someProperty: string;
};
function: (input: Record<string, string>) => void;
};
} = {};
const collection_path = set_collection_path();
const app = new Application();
const router = new Router();
//load and install kos
const activation_data = await start_up();
if (activation_data !== undefined) {
routing_dictionary = activation_data.routing_dictionary;
manifest = activation_data.manifest;
}
// Add middleware for redirecting root to /doc
app.use(async (ctx, next) => {
if (ctx.request.url.pathname === "/") {
ctx.response.redirect("/docs");
} else {
await next();
}
});
// Add middleware to serve static files from the "public" directory. Needed for /kos/:path*/doc.
app.use(async (ctx, next) => {
const staticPath = "./public";
if (ctx.request.url.pathname.startsWith("/public")) {
const path = ctx.request.url.pathname.substring(7); // Remove "/public" prefix
await send(ctx, path, {
root: staticPath,
});
} else {
await next();
}
});
// Add Middleware for custom error handling
app.use(async (ctx, next) => {
try {
await next();
} catch (error) {
if (
error instanceof EndpointNotFoundError ||
error instanceof InvalidInputParameterError ||
error instanceof KONotFoundError ||
error instanceof FileNotFoundError
) {
ctx.response.status = error.status;
ctx.response.body = {
title: error.name,
detail: error.message,
};
} else {
// Handle other exceptions here
ctx.response.status = Status.InternalServerError;
ctx.response.body = "An unexpected error occurred.";
}
}
});
//provide access to OpenAPI documentation using swagger editor for the app APIs at /doc
router.get("/docs", async (ctx) => {
const openapi = await Deno.readTextFile("public/openapi.json");
const html = await Deno.readTextFile("public/index.html");
ctx.response.type = "text/html";
ctx.response.body = html.replace("{{apiDoc}}", openapi);
});
//Get list of activated endpoints
router.get("/endpoints", (ctx) => {
ctx.response.body = Object.keys(routing_dictionary).map((key) => ({
"@id": key,
...routing_dictionary[key],
}));
});
//Get specific endpoint
router.get("/endpoints/:endpoint_path*", (ctx) => {
const capturedPath = ctx.params.endpoint_path || "";
if (capturedPath in routing_dictionary) {
ctx.response.body = {
"@id": capturedPath,
...routing_dictionary[capturedPath],
};
} else {
throw new EndpointNotFoundError(
`KeyError: Key '${capturedPath}' not found.`,
);
}
});
//Execute the function for a specific endpoint
router.post("/endpoints/:endpoint_path*", async (ctx) => {
const capturedPath = await ctx.params.endpoint_path || "";
if (!(capturedPath in routing_dictionary)) {
throw new EndpointNotFoundError(
`KeyError: Key '${capturedPath}' not found.`,
);
}
try {
const input = await ctx.request.body().value;
ctx.response.body = {
"result": routing_dictionary[capturedPath].function(input),
"info": { endpoint: routing_dictionary[capturedPath], "inputs": input },
};
} catch (error) {
throw new InvalidInputParameterError(error.message);
}
});
//Provide access to service.yaml for each ko at /kos/{ko_id}/service
router.get("/kos/:ko_id*/service", async (ctx) => {
const openapiSpec = await readService(ctx.params.ko_id);
ctx.response.type = "application/yaml"; //Serve the requested OpenAPI specification as YAML
ctx.response.body = openapiSpec;
});
//Get list of KOs
router.get("/kos", (ctx) => {
for (const item of manifest) {
if (item["status"] == "activated") {
item["documentation"] = ctx.request.url + "/" + item["@id"] + "/doc"; //join method only works for path and joining for URL is done using + in Deno
}
}
ctx.response.body = manifest;
});
//Provide access to OpenAPI documentation using swagger editir for each ko at /kos/{ko_id}/doc
router.get("/kos/:ko_id*/doc", async (ctx) => {
const openapiSpec = await readService(ctx.params.ko_id);
const apiDoc = parse(openapiSpec);
const html = await Deno.readTextFile("public/index.html");
ctx.response.type = "text/html";
ctx.response.body = html.replace("{{apiDoc}}", JSON.stringify(apiDoc));
});
//Get specific KO
router.get("/kos/:ko_id*", (ctx) => {
const capturedPath = ctx.params.ko_id || "";
const koIndex = manifest.findIndex((item) => item["@id"] === capturedPath);
if (koIndex == -1) {
throw new KONotFoundError(`KeyError: Key '${capturedPath}' not found.`);
}
if (manifest[koIndex]["status"] == "activated") {
manifest[koIndex]["documentation"] = ctx.request.url + "/doc"; //join method only works for path and joining for URL is done using + in Deno
}
ctx.response.body = manifest[koIndex];
});
// Add the router as middleware
app.use(router.routes());
app.use(router.allowedMethods());
// Start the server
const { args } = Deno;
const argPort = parseFlags(args).port;
const port = argPort ? Number(argPort) : 3000;
console.info(`Server is running on http://localhost:${port}`);
await app.listen({ port });
async function readService(ko_id: string | undefined ) {
const capturedPath = ko_id || "";
const koIndex = manifest.findIndex((item) => item["@id"] === capturedPath);
if (koIndex == -1) {
throw new KONotFoundError(`KeyError: Key '${capturedPath}' not found.`);
}
const service_specification = manifest[koIndex]["hasServiceSpecification"] ||
"service.yaml";
let specPath = join(
collection_path,
manifest[koIndex]["local_url"],
service_specification,
);
if (manifest[koIndex]["koio:kgrid"] == "2") { //KO model kgrid version 2's specific code to read services
type Data = {
"@id": string;
"@type"?: string | string[];
serviceSpec?: string;
dependsOn?: string;
implementedBy: [{ "@id": string; "@type": string }];
};
const services: Data[] =
manifest[koIndex]["koio:hasService"] as unknown as Data[];
for (const service of services) {
if (service["@type"] === "API") {
const implementations = service["implementedBy"];
for (const implementation in implementations) {
if (
implementations[implementation]["@type"] &&
implementations[implementation]["@type"].includes(
"https://kgrid.org/specs/activationSpec.html#object",
) && implementations[implementation]["@type"].includes("javascript")
) {
// load context
let context = { "@context": manifest[koIndex]["@context"] };
if (String(manifest[koIndex]["@context"]).includes(".jsonld")) {
const response = await fetch(manifest[koIndex]["@context"]);
if (response.ok) {
const fileContent = await response.text();
context = JSON.parse(fileContent);
} else {
console.error(
`Error fetching the context file. Status code: ${response.status}`,
);
continue;
}
}
// add @base to context
context["@context"]["@base"] = path.join(
collection_path,
manifest[koIndex]["local_url"],
" ",
);
// exoand the service using the context
await jsonld.expand({ "@context": context["@context"], ...service })
.then((expanded) => {
// use resolved hasServiceSpcification
specPath =
expanded[0]["http://kgrid.org/koio#hasServiceSpcification"][
0
]["@id"];
});
break;
}
}
}
}
}
if (!existsSync(specPath)) {
throw new FileNotFoundError(
`FileNotFoundError: file '${service_specification}' not found.`,
);
}
const openapiSpec = Deno.readTextFileSync(specPath);
return openapiSpec;
}