-
Notifications
You must be signed in to change notification settings - Fork 0
/
cold.ts
227 lines (201 loc) · 5.64 KB
/
cold.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
import {
appInfo,
existsSync,
handleDirectoryLookup,
handleDOIPrefix,
handleFunders,
handleGroups,
handleISSN,
handlePeople,
handleReports,
handleSubjects,
OptionsProcessor,
path,
serveDir,
} from "./deps.ts";
/**
* helpText assembles the help information for COLD UI.
*
* @param {[k: string]: string} helpOpt holds the help options defined for the app.
*/
function helpText(helpOpt: { [k: string]: string }): string {
const app_name = appInfo.appName;
const version = appInfo.version;
const release_date = appInfo.releaseDate;
const release_hash = appInfo.releaseHash;
const txt: string[] = [
`%${app_name}(1) user manual | ${version} ${release_date} ${release_hash}
% R. S.Doiel
% ${release_date} ${release_hash}
# NAME
${app_name}
# SYNOPSIS
${app_name} [OPTIONS]
# DESCRIPTION
${app_name} provides the admin interface for cold. Cold is implemented using dataset collections
for object persistence and relies on datasetd for JSON API to each collection.
# OPTIONS
`,
];
for (let attr in helpOpt) {
const msg = helpOpt[attr];
txt.push(`${attr}
: ${msg}
`);
}
txt.push(`
# EXAMPLE
${app_name} is setup to run at <http://localhost:8111>. The static content hosted in
the "/var/www/html/cold/app" directory. The datasetd service is setup to run at
<http://localhost:8112> supporting the people, groups and vocabularies dataset
collections.
~~~shell
${app_name} -port=8111 -htdocs=/var/www/html/cold/app \
-apiUrl=http://localhost:8112
~~~
`);
return txt.join("\n");
}
/**
* ColdReadWriteHandler is a function for handling and dispatching http requests.
*
* @param {Request} req holds the http request recieved from the http server
* @param {debug: boolean, htdocs: string, apiUrl: string} options holds program options that are made available
* to additional COLD UI handlers.
* @returns {Response}
*
* @example
* ```
* const options = {
* debug: true,
* htdocs: "./htdocs"
* baseUrl: "https://localhost:8000/cold"
* };
*
* const server = Deno.serve({
* hostname: "localhost",
* port: options.port,
* }, (req: Request) => {
* return ColdReadWriteHandler(req, options);
* });
* ```
*/
export function ColdReadWriteHandler(
req: Request,
options: { debug: boolean; htdocs: string; baseUrl: string; apiUrl: string },
): Response | Promise<Response> {
const pathname = new URL(req.url).pathname;
const htdocs: string = path.normalize(options.htdocs);
if (options.debug) console.log("DEBUG request", req);
if (options.debug) console.log("DEBUG options", options);
// Handle the various dataset collections management pages.
if (pathname.startsWith("/people")) {
return handlePeople(req, options);
}
if (pathname.startsWith("/groups")) {
return handleGroups(req, options);
}
if (pathname.startsWith("/funders")) {
return handleFunders(req, options);
}
if (pathname.startsWith("/subjects")) {
return handleSubjects(req, options);
}
if (pathname.startsWith("/issn")) {
return handleISSN(req, options);
}
if (pathname.startsWith("/doi_prefix")) {
return handleDOIPrefix(req, options);
}
if (pathname.startsWith("/reports")) {
return handleReports(req, options);
}
if (pathname.startsWith("/directory_api")) {
return handleDirectoryLookup(req, options);
}
if (options.debug) {
console.log(
"DEBUG: Handle the request for a static files or assets -> " + pathname,
);
}
// NOTE: If there isn't a specific handler implemented then assume you're
// requesting a static asset.
return serveDir(req, {
fsRoot: htdocs,
});
}
//
// Main function
//
function main() {
const op: OptionsProcessor = new OptionsProcessor();
const defaultPort: number = 8111;
const defaultHtdocs: string = "./htdocs";
const defaultbaseUrl: string = "://";
const defaultApiUrl: string = "http://localhost:8112";
op.booleanVar("help", false, "display help");
op.booleanVar("license", false, "display license");
op.booleanVar("version", false, "display version");
op.booleanVar("debug", false, "turn on debug logging");
op.numberVar(
"port",
defaultPort,
`set the port number, default ${defaultPort}`,
);
op.stringVar(
"htdocs",
defaultHtdocs,
`set the static content directory, default ${defaultHtdocs}`,
);
op.stringVar(
"baseUrl",
defaultbaseUrl,
`set the browser's base path reference, default ${defaultbaseUrl}`,
);
op.stringVar(
"apiUrl",
defaultApiUrl,
`set the url to the datasetd API provided for cold`,
);
op.parse(Deno.args);
const options = op.options;
const args = op.args;
if (options.help) {
console.log(helpText(op.help));
Deno.exit(0);
}
if (options.license) {
console.log(appInfo.licenseText);
Deno.exit(0);
}
if (options.version) {
console.log(`${appInfo.appName} ${appInfo.version} ${appInfo.releaseHash}`);
Deno.exit(0);
}
// Make sure we have a valid static content directory set.
if (!existsSync(options.htdocs)) {
console.log(`Cannot find htdocs ${options.htdocs}, aborting`);
Deno.exit(1);
}
console.log(`Starting COLD UI HTTP service at http://localhost:${options.port}
Relies on JSON API at ${options.apiUrl}
Static content directory is ${options.htdocs}
Browser base url set to ${options.baseUrl}
`);
const server = Deno.serve(
{
hostname: "localhost",
port: options.port,
},
(req: Request): Response | Promise<Response> => {
return ColdReadWriteHandler(req, {
debug: options.debug,
htdocs: options.htdocs,
baseUrl: options.baseUrl,
apiUrl: options.apiUrl,
});
},
);
}
// Run main()
if (import.meta.main) main();