-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
159 lines (149 loc) · 4.28 KB
/
main.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
//https://deno.land/x/[email protected]/
import { proxy } from "https://deno.land/x/[email protected]/mod.ts";
import {
Application,
Context,
Middleware,
Router,
send,
} from "https://deno.land/x/[email protected]/mod.ts";
import { createScuttlebuttConfig } from "../scuttlesaurus/main.ts";
import SparqlStorer from "./SparqlStorer.ts";
import {
BlobId,
delay,
fromBase64,
fromFilenameSafeAlphabet,
parseFeedId,
path,
} from "../scuttlesaurus/util.ts";
import registerFollowees from "./registerFollowees.ts";
import DenoScuttlebuttHost from "../scuttlesaurus/DenoScuttlebuttHost.ts";
import FeedsStorage from "../scuttlesaurus/storage/FeedsStorage.ts";
function getRequiredEnvVar(name: string): string {
const value = Deno.env.get(name);
if (!value) {
throw new Error(`The environment variable "${name}" must be set.`);
}
console.debug(() => `${name} set to ${value}`);
return value;
}
const sparqlEndpointQuery = getRequiredEnvVar("SPARQL_ENDPOINT_QUERY");
const sparqlEndpointUpdate = getRequiredEnvVar("SPARQL_ENDPOINT_UPDATE");
const storer = new SparqlStorer(
sparqlEndpointQuery,
sparqlEndpointUpdate,
Deno.env.get("SPARQL_ENDPOINT_CREDENTIALS"),
);
const storeFeedsInFiles = Deno.env.get("FEEDS_STORAGE") === "FILES";
class TriceraHost extends DenoScuttlebuttHost {
createFeedsStorage(): FeedsStorage | undefined {
if (storeFeedsInFiles) {
return super.createFeedsStorage();
} else {
return storer;
}
}
}
const host = new TriceraHost(await createScuttlebuttConfig());
const portalOwner = Deno.env.get("SSB_PORTAL_OWNER");
const mainIdentity = portalOwner ? parseFeedId(portalOwner) : host.identity;
if (storeFeedsInFiles) {
storer.connectAgent(host.feedsAgent!);
}
const staticDir = path.join(
path.dirname(path.fromFileUrl(import.meta.url)),
"/static",
);
const hostRun = host.start();
function addCommonEndpoints(
{ application, router }: {
application: Application<
// deno-lint-ignore no-explicit-any
Record<string, any>
>;
router: Router<
// deno-lint-ignore no-explicit-any
Record<string, any>
>;
},
) {
router.all(
"/query",
proxy(sparqlEndpointQuery, {
filterReq: (req: { method: string }, _res: unknown) => {
return req.method !== "GET";
},
srcResHeaderDecorator: () =>
new Headers({ "Cache-Control": "max-age=30, public" }),
}),
);
/** the owner if one is set, otherwise scuttlesaurus identity */
router.get("/main-identity", (ctx: Context) => {
ctx.response.body = JSON.stringify({
feedId: mainIdentity,
});
});
router.get(
"/blob/sha256/:hash",
async (ctx: Context) => {
const base64hash = fromFilenameSafeAlphabet(
(ctx as unknown as { params: Record<string, string> }).params.hash,
);
const hash = fromBase64(base64hash);
const blobId = new BlobId(hash);
if (!host.blobsAgent) {
throw new Error("No BlobsAgent");
}
host.blobsAgent.want(blobId);
try {
const data = await host.blobsAgent.storage.getBlob(blobId);
ctx.response.body = data;
ctx.response.headers.append(
"Cache-Control",
"Immutable, max-age=604800, public",
);
} catch (_error) {
ctx.response.status = 404;
}
},
);
application.use(staticFiles(path.join(staticDir, "common")));
}
addCommonEndpoints(host.webEndpoints.access);
addCommonEndpoints(host.webEndpoints.control);
host.webEndpoints.access.application.use(
staticFiles(path.join(staticDir, "access")),
);
host.webEndpoints.control.application.use(
staticFiles(path.join(staticDir, "control")),
);
while (true) {
registerFollowees(mainIdentity, host, sparqlEndpointQuery);
//update every 15 minutes
await delay(15 * 60 * 1000);
}
//await hostRun;
console.info("Host terminated");
function staticFiles(
baseDir: string,
) {
const middleware: Middleware = async function (ctx, next) {
if (ctx.isUpgradable) {
await next();
} else {
const fsPath = path.join(baseDir, ctx.request.url.pathname);
try {
await Deno.stat(fsPath);
//file or diretory exists
await send(ctx, fsPath, {
root: "/",
index: "index.html",
});
} catch (_error) {
await next();
}
}
};
return middleware;
}