-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
55 lines (51 loc) · 1.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
import { serve } from 'https://deno.land/std/http/server.ts'
import { Hono } from 'https://deno.land/x/hono/mod.ts'
import { watch, build } from "./build.ts"
import { mime } from "https://deno.land/x/[email protected]/mod.ts"
import { isurl, reqErr, blob2b64 } from "./utils/index.ts"
if(Deno.args.includes("--dev")){
watch()
}else{
await build()
}
const app = new Hono()
app.use('*',async(ctx,next)=>{
const _path=new URL(ctx.req.url).pathname
console.log(_path)
ctx.set("path",_path.at(-1)==="/" ? _path.slice(0,-1) : _path)
await next()
})
app.get('/proxy',async(ctx)=>{
const datas=ctx.req.query()
datas.url=decodeURIComponent(datas.url)
const url=datas.url
if(!isurl(url))
return reqErr(ctx,"Invalid data: 'url'")
const req=Object.assign({
url:"https://example.com"
},{
url:new URL(url),
});
const proxyRes=await fetch(req.url);
const proxyBlob=await proxyRes.blob();
const proxyB64=await blob2b64(proxyBlob);
return ctx.json({
body:proxyB64
});
})
app.get('/*',async(ctx)=>{
let filedata
const ext=(ctx.get("path")).split("/").at(-1).split(".").at(-1)
ctx.header('Content-Type',mime.getType(ext) || "text/html")
try{
filedata= await Deno.readFile(`./public${ctx.get("path")}`)
}catch{
try{
filedata= await Deno.readFile(`./public${ctx.get("path")}/index.html`)
}catch{
return ctx.notFound()
}
}
return ctx.body(filedata)
})
serve(app.fetch)