-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcold.ts
More file actions
208 lines (192 loc) · 5.71 KB
/
cold.ts
File metadata and controls
208 lines (192 loc) · 5.71 KB
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
import {
existsSync,
handleDirectoryLookup,
handleDOIPrefix,
handleFunders,
handleGroups,
handleJournals,
handlePeople,
handleReports,
handleSubjects,
handleThesisOption,
OptionsProcessor,
path,
serveDir,
} from "./deps.ts";
import { handleBrowserAPI } from "./browser_api.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);
* });
* ```
*/
/**
* basePathFromUrl extracts the pathname from a URL string, returning "" if the
* URL is invalid or the path is just "/". Used to derive the base path prefix
* that Apache passes through when COLD is mounted at a sub-path like /cold.
*/
function basePathFromUrl(baseUrl: string): string {
try {
const p = new URL(baseUrl).pathname.replace(/\/$/, "");
return p === "" ? "" : p;
} catch {
return "";
}
}
export function ColdReadWriteHandler(
req: Request,
options: { debug: boolean; htdocs: string; baseUrl: string; apiUrl: string },
): Response | Promise<Response> {
const fullPathname = new URL(req.url).pathname;
const basePath = basePathFromUrl(options.baseUrl);
// Strip the deployment base path so internal routing always sees paths like /people, /api, etc.
const pathname = basePath && fullPathname.startsWith(basePath)
? fullPathname.slice(basePath.length) || "/"
: fullPathname;
const htdocs: string = path.normalize(options.htdocs);
if (options.debug) console.log("debugging request", req);
if (options.debug) console.log("debugging 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);
}
// FIXME: This is the journals vocabulary, really need to rename this at some point.
if (pathname.startsWith("/journals")) {
return handleJournals(req, options);
}
if (pathname.startsWith("/thesis_options")) {
return handleThesisOption(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 (pathname.startsWith("/api")) {
return handleBrowserAPI(req, options);
}
if (options.debug) {
console.log(
"debugging: 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,
urlRoot: basePath.replace(/^\//, ""), // strip /cold prefix when mapping to htdocs
enableCors: false,
});
}
//
// 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();