Started to implement server
This commit is contained in:
1 parent
1f2ea8b7e5
commit
17a1b4bed3
177 files changed
+29894
-74
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { Context, Router } from "https://deno.land/x/oak/mod.ts";
|
||||
import { FileService } from "./fileservice.ts";
|
||||
import { readerFromStreamReader } from "https://deno.land/std@0.189.0/streams/mod.ts";
|
||||
import { asyncIterableToArray } from "./utils.ts";
|
||||
|
||||
|
||||
|
||||
type Handler = (ctx: Context) => void;
|
||||
|
||||
|
||||
function listFileHandlerFactory(fileService: FileService): Handler {
|
||||
return async (ctx: Context) => {
|
||||
ctx.response.type = "application/json";
|
||||
ctx.response.body = await asyncIterableToArray(fileService.list());
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function createFileHandlerFactory(fileService: FileService): Handler {
|
||||
return async (ctx: Context) => {
|
||||
const untypedCtx: any = ctx;
|
||||
ctx.response.body = 'This is a post';
|
||||
const path = untypedCtx.request.url.searchParams.get('path');
|
||||
|
||||
const reqBody = await ctx.request.body({ type: "stream" }).value;
|
||||
const reader = readerFromStreamReader(reqBody.getReader())
|
||||
fileService.createFromReader(path, reader);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function getFileHandlerFactory(fileService: FileService): Handler {
|
||||
return (ctx: Context) => {
|
||||
const untypedCtx: any = ctx;
|
||||
const fileId = untypedCtx.params.file_id;
|
||||
// ctx.response.body = await fileService.getRawBodyByPath()
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
export function buildAPI(router: Router, fileService: FileService): Router {
|
||||
|
||||
router.get('/files', listFileHandlerFactory(fileService));
|
||||
router.get('/files/:file_id', getFileHandlerFactory(fileService));
|
||||
router.post('/files', createFileHandlerFactory(fileService));
|
||||
|
||||
return router;
|
||||
}
|
||||
Reference in new issue
Block a user