-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcold.ts
177 lines (162 loc) · 4.54 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
import {
existsSync,
handleDirectoryLookup,
handleDOIPrefix,
handleFunders,
handleGroups,
handleISSN,
handlePeople,
handleReports,
handleSubjects,
OptionsProcessor,
path,
serveDir,
} from "./deps.ts";
import { coldHelpText, fmtHelp } from "./helptext.ts";
import { licenseText, releaseDate, releaseHash, version } from "./version.ts";
const appName: string = "cold";
/**
* 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(
fmtHelp(coldHelpText, appName, version, releaseDate, releaseHash),
);
Deno.exit(0);
}
if (options.license) {
console.log(licenseText);
Deno.exit(0);
}
if (options.version) {
console.log(`${appName} ${version} ${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();