-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
140 lines (123 loc) · 3.41 KB
/
server.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
import type { PaletteSource } from "./src/types.ts";
import { Hono } from "@hono/hono";
import { serveStatic } from "@hono/hono/serve-static";
import { readFile } from "node:fs/promises";
import img2mcstructure, { createPalette } from "./src/mcstructure/mod.ts";
import createFunction from "./src/mcfunction/mod.ts";
import { img2mcaddon } from "./mod.ts";
export default function main(defaultDb: PaletteSource) {
const app = new Hono();
app.post("/v1/structure", async (ctx) => {
const { img, axis, db } = await ctx.req.json();
try {
const data = await img2mcstructure(
img,
createPalette(db ?? defaultDb),
axis,
);
return new Response(data, {
headers: {
"Content-Disposition": 'attachment; filename="img.mcstructure"',
"Content-Type": "application/octet-stream",
"Access-Control-Allow-Origin": "*",
},
});
} catch (err) {
return new Response(err.message, { status: 500 });
}
});
app.post("/v1/fill", async (ctx) => {
const { img, axis, db } = await ctx.req.json();
try {
const data = await createFunction(img, db ?? defaultDb, [0, 0, 0]);
return new Response(data, {
headers: {
"Content-Disposition": 'attachment; filename="img.mcfunction"',
"Content-Type": "text/plain",
"Access-Control-Allow-Origin": "*",
},
});
} catch (err) {
return new Response(err.message, { status: 500 });
}
});
app.post("/v1/addon", async (ctx) => {
const {
img,
gridSize,
resolution,
axis,
mer,
normal,
frames = 1,
} = await ctx.req.json();
const pbr = mer || normal;
try {
const data = await img2mcaddon(
img,
gridSize,
resolution,
axis,
pbr,
frames,
);
return new Response(data, {
headers: {
"Content-Disposition": 'attachment; filename="img.mcaddon"',
"Content-Type": "application/octet-stream",
"Access-Control-Allow-Origin": "*",
},
});
} catch (err) {
return new Response(err.message, { status: 500 });
}
});
app.get("/db/:dbName", async (ctx) => {
const { dbName } = ctx.req.param();
let db: string;
try {
db = await readFile(`db/${dbName}.json`, "utf-8");
} catch (err) {
return new Response(err.message, { status: 404 });
}
try {
const palette = createPalette(JSON.parse(db));
return new Response(JSON.stringify(palette), {
headers: {
"Content-Type": "application/json",
},
});
} catch (err) {
return new Response(err.message, { status: 500 });
}
});
app.get("/", (c) =>
c.html(
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image to Minecraft</title>
<link rel="stylesheet" href="static/style.css" />
</head>
<body>
<div id="app"></div>
<script src="static/bundle.js"></script>
</body>
</html>`,
));
app.get(
"static/*",
serveStatic({
root: "./",
rewriteRequestPath(path) {
return path.replace(/^\/static\//, "dist/");
},
getContent(path, c) {
return readFile(path, "utf-8");
},
}),
);
return app;
}