Implement org file server
This commit is contained in:
@@ -11,10 +11,14 @@ import std/[
|
||||
sugar,
|
||||
]
|
||||
import fp/[
|
||||
map,
|
||||
option,
|
||||
maybe,
|
||||
resultM,
|
||||
]
|
||||
import fusion/matching
|
||||
import ./env
|
||||
import ./errors
|
||||
import ./utils
|
||||
# (GET, (scheme: "", username: "", password: "", hostname: "", port: "", path: "/", query: "", anchor: "", opaque: false, isIpv6: false), {"accept": @["*/*"], "host": @["localhost:45201"], "user-agent": @["curl/7.82.0"]})
|
||||
|
||||
@@ -55,6 +59,44 @@ proc sendStaticFile(env: Env, path: seq[string]): NimHttpResponse =
|
||||
})
|
||||
)
|
||||
|
||||
proc sendOrgFile(env: Env, id: orgId, path: seq[string]): NimHttpResponse =
|
||||
type errT = tuple[err: orgEnvErr, id: orgId, path: string]
|
||||
type StringResult = Result[orgPath, errT]
|
||||
type FileResult = Result[string, errT]
|
||||
|
||||
let content = env.orgEnv
|
||||
.get(id)
|
||||
.fold(
|
||||
() => StringResult.err((invalidWorkspaceId, id, "")),
|
||||
(x: orgPath) => StringResult.ok(x),
|
||||
)
|
||||
.map((x: orgPath) => $x.joinPath(path.join("/")))
|
||||
.flatMap((path: orgPath) => (
|
||||
catch(readFile(path)).fold(
|
||||
_ => FileResult.err((fileNotFound, id, path)),
|
||||
(content: string) => FileResult.ok(content),
|
||||
)
|
||||
))
|
||||
.tapErr((err: errT) => logErr(env, err[0], &"{err[1]}, path: {err[2]}"))
|
||||
|
||||
content.fold(
|
||||
(e: errT) => (
|
||||
code: Http404,
|
||||
content: "File not Found",
|
||||
headers: newHttpHeaders({
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
),
|
||||
(content: string) => (
|
||||
code: Http200,
|
||||
content: content,
|
||||
headers: newHttpHeaders({
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
proc handleRoute(env: Env, req: Request): NimHttpResponse =
|
||||
# Handle main route
|
||||
if req.url.path == "/":
|
||||
@@ -65,9 +107,11 @@ proc handleRoute(env: Env, req: Request): NimHttpResponse =
|
||||
|
||||
# Router
|
||||
return (
|
||||
case path:
|
||||
of ["dist", .._]:
|
||||
case (req.reqMethod, path):
|
||||
of (HttpGet, ["dist", .._]):
|
||||
sendStaticFile(env, path)
|
||||
of (HttpGet, ["org", @id, all @rest]):
|
||||
sendOrgFile(env, id, rest)
|
||||
else:
|
||||
sendNotFound(env, path)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user