-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
151 lines (122 loc) · 4.49 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
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
import { factory } from "https://deno.land/x/oak/middleware/etag.ts";
import { basicAuth } from "./lib/auth.ts";
import {
handleStability, handleChatGPT,
handleLoad, handleLoadVersion, handleLoadVersions,
handleLoadResult,
handlePersist, handlePersistImage,
handleClone,
handleRecover,
handlePersistResult, handleListApps,
handleListResults,
handleListUsers, handleBulkCreateUsers
} from './routes/api.ts';
import { handleUserFacingURLs, handleStaticFiles } from './routes/user-facing-and-static.ts';
import { renderResult, renderUserResults } from "./routes/result-renderer.ts";
import { renderCommunity } from "./routes/community-renderer.ts"
import { serveThumbnail } from "./routes/thumbnail.ts";
import { serveImportedImage } from "./routes/imported-image.ts";
import { handleListUsedApps } from './routes/api.ts';
import { handleListMostUsedApps, handleListLastActiveUsers, handleListMostActiveUsers } from './routes/dashboard.ts';
const router = new Router();
// JSON Endpoints
router
.post('/stability', handleStability)
.post('/chatgpt', handleChatGPT)
.post('/persist-image', handlePersistImage)
.post('/persist-result', handlePersistResult)
.post('/persist', handlePersist)
.post('/clone', handleClone)
.post('/recover', handleRecover)
.post('/list-apps', handleListApps)
.get('/list-used-apps', handleListUsedApps)
.get('/list-results', handleListResults)
.post('/load-versions', handleLoadVersions)
.post('/load-version', handleLoadVersion)
.post('/load-result', handleLoadResult)
.post('/load', handleLoad)
;
// Dashboard routes
router
.get('/list-most-used-apps', handleListMostUsedApps)
.get('/list-last-active-users', handleListLastActiveUsers)
.get('/list-most-active-users', handleListMostActiveUsers)
.get('/dashboard', handleUserFacingURLs)
;
// User Facing URLs
router
.get('/app', handleUserFacingURLs)
.get('/app/:id', handleUserFacingURLs)
.get('/apps', handleUserFacingURLs)
.get('/apps/:user', handleUserFacingURLs)
.get('/community', renderCommunity)
.get("/result/:id", renderResult)
.get("/results", renderUserResults)
.get("/thumbnail/:id", serveThumbnail)
.get("/imported-image/:hash", serveImportedImage)
.get('/admin', handleUserFacingURLs)
.get('/', handleUserFacingURLs)
;
// Admin Endpoints
router
.get('/list-users', handleListUsers)
.post('/bulk-create-users', handleBulkCreateUsers);
const app = new Application();
// Authentication Middleware
const routePrefixes = [
"/app", "/apps", "/imported-image",
];
const exactRoutes = [
'/',
'/stability', '/chatgpt',
'/persist', '/persist-result', '/persist-image', '/clone', '/recover',
'/load', '/load-version', '/load-versions', '/load-result',
"/app", "/apps", "/list-apps", "/list-used-apps",
"/community",
"/results", '/list-results',
"/admin", "/dashboard",
"/list-users", "/bulk-create-users",
"/list-most-used-apps", "/list-last-active-users", "/list-most-active-users",
];
app.use(async (ctx, next) => {
const path = ctx.request.url.pathname;
const isAuthRoute =
routePrefixes.some(prefix => path.startsWith(prefix + "/"))
|| exactRoutes.some(route => path === route);
if (isAuthRoute) {
const authResult = await basicAuth(ctx.request);
if (!authResult.isAuthenticated || !authResult.username) {
ctx.response.status = 401;
ctx.response.body = 'Unauthorized';
ctx.response.headers.set('WWW-Authenticate', 'Basic realm="Esquisse"');
return;
}
ctx.state.user = authResult; // { isAuthenticated, isAdmin, username}
// sending the authenticated username back to the client, as validation
// needed as we use the browser basic auth, which keeps username out of JavaScript reach
ctx.response.headers.set("X-username", authResult.username);
}
await next();
});
app.use(factory());
app.use(router.routes());
app.use(router.allowedMethods());
// Fallback Middleware for Static Files
app.use(async (ctx, next) => {
const path = ctx.request.url.pathname;
const isRoutePath =
routePrefixes.some(prefix => path.startsWith(prefix + "/"))
|| exactRoutes.some(route => path === route);
if (!isRoutePath) {
await handleStaticFiles(ctx);
} else {
await next();
}
});
app.addEventListener("error", (evt) => {
console.error(evt.error);
});
const port = Deno.env.get("PORT") ? parseInt(Deno.env.get("PORT")) : 8000;
console.log(`HTTP webserver running. Access it at: http://localhost:${port}/`);
await app.listen({ port });