forked from juliendorra/esquisse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
82 lines (65 loc) · 2.24 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
import { serve } from "https://deno.land/std/http/server.ts";
import { basicAuth } from "./auth.ts";
import { contentType } from "https://deno.land/std/media_types/mod.ts";
import { tryGenerate as callStability } from "./stability.js";
import { callGPT } from "./gpt.js";
const handler = async (request: Request): Promise<Response> => {
const url = new URL(request.url);
const { pathname } = url;
const isAuthenticated = await basicAuth(request);
if (!pathname.startsWith("/public/")) {
if (!isAuthenticated) {
return new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="Esquisse"', } });
}
}
if (pathname.startsWith("/stability") || pathname.startsWith("/chatgpt")) {
return await handleJsonEndpoints(request);
}
if (pathname === "/") {
const file = await Deno.readFile(`./static/index.html`);
const type = contentType("html") || "text/plain";
return new Response(file, {
headers: { "content-type": type },
});
}
// Fall back to serving static files
try {
const file = await Deno.readFile(`./static${pathname}`);
const type = contentType(pathname.slice(pathname.lastIndexOf(".") + 1)) || "text/plain";
return new Response(file, {
headers: { "content-type": type },
});
} catch (e) {
return new Response('Not Found', { status: 404 });
}
};
async function handleJsonEndpoints(request: Request): Promise<Response> {
const url = new URL(request.url);
const { pathname } = url;
let response;
const body = await request.json();
console.log(body);
if (pathname.startsWith("/stability")) {
response = await callStability(
body.data + " " + body.transform,
"",
"",
body.qualityEnabled,
3);
return new Response(response, {
headers: { "content-type": "image/png" },
});
}
if (pathname.startsWith("/chatgpt")) {
response = await callGPT(
body.data,
body.transform,
body.qualityEnabled);
return new Response(JSON.stringify(response), {
headers: { "content-type": "application/json" },
});
}
}
const port = 8080;
console.log(`HTTP webserver running. Access it at: http://localhost:${port}/`);
await serve(handler, { port: +(Deno.env.get("PORT") ?? 8000) });