-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.f.ts
More file actions
56 lines (50 loc) · 1.75 KB
/
server.f.ts
File metadata and controls
56 lines (50 loc) · 1.75 KB
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
import { utf8 } from 'functionalscript/fs/text/module.f.js'
import { empty, length, type Vec } from 'functionalscript/fs/types/bit_vec/module.f.js'
import { pure } from 'functionalscript/fs/effects/module.f.js'
import {
createServer,
listen,
forever,
type NodeProgram,
type IncomingMessage,
readFile,
log,
readdir,
type IoResult,
type Dirent,
} from 'functionalscript/fs/effects/node/module.f.js'
import { htmlUtf8 } from 'functionalscript/fs/html/module.f.js'
import { concat } from 'functionalscript/fs/path/module.f.js'
export const listener = ({ url }: IncomingMessage) => {
const path = url.split('?')[0] ?? ''
const file = '.' + path
const dirLink = ({ name, isFile }: Dirent) => {
const href = '/' + concat(path)(name)
return [['a', { href }, name + (isFile ? '' : '/')], '\n'] as const
}
const dirPage = (v: readonly Dirent[]) => htmlUtf8
(['link', { rel: 'stylesheet', href: '/main.css' }])
(['pre', ...v.flatMap(dirLink)])
const orReadDir = (r: IoResult<Vec>) => r[0] === 'ok'
? pure(r)
: readdir(file, {})
.step(([s, v]) => pure([s, s === 'ok' ? dirPage(v) : empty] as const))
return log(`reading ${file}`)
.step(() => readFile(file))
.step(orReadDir)
.step(([s, v]) => {
const o = s === 'ok'
return pure({
status: o ? 200 : 404,
headers: {},
body: o ? v : utf8('404 not found'),
})
})
.step(x =>
log(`served: ${length(x.body) >> 3n} bytes`)
.step(() => pure(x))
)
}
export const main: NodeProgram = () => createServer(listener)
.step(server => listen(server, 3000))
.step(forever)