-
Notifications
You must be signed in to change notification settings - Fork 0
/
back.js
130 lines (109 loc) · 4.25 KB
/
back.js
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
import { Application, Router, send } from "https://deno.land/x/oak/mod.ts";
import { DB } from "https://deno.land/x/sqlite/mod.ts";
import { oakCors } from "https://deno.land/x/cors/mod.ts";
import { decode as base64decode } from "https://deno.land/std/encoding/base64.ts";
// #Configulations#
const g_nPort = 8111;
const g_sAdminName = "admin";
const g_sAdminPass = "d8c93l=K";
const g_sFrontURL = "http://localhost:8110/11C87255-878A-4F04-94B6-490FDE1E9BE6";
async function doAuth(ctx, next){
let authed = false;{
const auth = ctx.request.headers.get("Authorization");
if(auth){
const userpass = (new TextDecoder().decode(base64decode( auth.split(" ")[1] ))).split(":");
authed = ( userpass[0]===g_sAdminName && userpass[1]===g_sAdminPass );
}
}
if(authed){
await next();
}else{
ctx.response.status = 401;
ctx.response.headers.set("WWW-Authenticate", 'Basic');
}
}
const router = new Router();{
router.get('/photo/:id-:filename', async function(ctx){
await send(ctx, `${ctx.params.id}-${ctx.params.filename}`, {
root: './entry-photo',
});
});
router.get('/video/:id-:filename', async function(ctx){
await send(ctx, `${ctx.params.id}-${ctx.params.filename}`, {
root: './entry-video',
});
});
router.get("/front-url", async function(ctx){
ctx.response.body = g_sFrontURL;
});
router.get("/entries", async function(ctx){
let r = null;
const db = new DB("_.db");{
r = db.queryEntries("select id,sTitle,sComment,sPhotoFile,sVideoFile,(select count(*) from TBallot where pEntry=TEntry.id) as nBallots,(select count(*) from TBallot where pEntry=TEntry.id and sEmail is not NULL) as nBallotsM from TEntry order by nBallots DESC");
db.close();
}
ctx.response.body = r;
});
router.get("/delete", async function(ctx){
const id = ctx.request.url.searchParams.get("id");
const db = new DB("_.db");{
db.query("delete from TEntry where id=?", [id]);
db.close();
}
ctx.response.redirect("./");
});
router.post("/entry", async function(ctx){
const body = await ctx.request.body({ type: 'form-data'});
const form = await body.value.read({ maxFileSize: 104_857_600});
const v = form.fields;
const db = new DB("_.db");
db.query("BEGIN");
try{
db.query("INSERT INTO TEntry (sTitle,sComment) VALUES (?,?)", [v.sTitle, v.sComment]);
const id = db.lastInsertRowId;
for(const f of form.files){
if(f.name === "fPhoto"){
Deno.renameSync(f.filename, `./entry-photo/${id}-${f.originalName}`);
db.query("UPDATE TEntry set sPhotoFile=? where id=?", [f.originalName, id]);
}else
if(f.name === "fVideo"){
Deno.renameSync(f.filename, `./entry-video/${id}-${f.originalName}`);
db.query("UPDATE TEntry set sVideoFile=? where id=?", [f.originalName, id]);
}else
{}
}
db.query("COMMIT");
}catch(e){
ctx.response.body = `check your some inputs.(${e})`;
return;
}
ctx.response.redirect("./");
});
}
const app = new Application();{
app.use(async function(ctx, next){
console.log(`--- ${new Date()} - ${ctx.request.method} ${ctx.request.url.pathname}`);
await next();
});
app.use(doAuth);
app.use(oakCors());
app.use(router.routes());
app.use(router.allowedMethods());
app.use(async function(ctx){
if(ctx.request.method === "GET"){
try{
await send(ctx, ctx.request.url.pathname, {
root: './www-back',
index: "index.html",
});
}catch(e){}
}
});
console.log('running on port ', g_nPort);
await app.listen({
port: g_nPort,
// secure: true,
// certFile: "server_crt.pem",
// keyFile: "server_key.pem",
});
}