File size: 1,939 Bytes
a18e5eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as http from 'http'
import { WormLog } from './storage/worm-log'
import { sealOutput } from './agent/seal'

export const worm = new WormLog(process.env.DATA_DIR ?? './data')

const PORT = parseInt(process.env.PORT ?? '3000', 10)

function json(res: http.ServerResponse, status: number, body: unknown): void {
  const payload = JSON.stringify(body)
  res.writeHead(status, { 'Content-Type': 'application/json' })
  res.end(payload)
}

function readBody(req: http.IncomingMessage): Promise<string> {
  return new Promise((resolve, reject) => {
    let body = ''
    req.on('data', chunk => { body += chunk })
    req.on('end', () => resolve(body))
    req.on('error', reject)
  })
}

export const server = http.createServer(async (req, res) => {
  const url = new URL(req.url ?? '/', `http://localhost:${PORT}`)

  if (req.method === 'GET' && url.pathname === '/health') {
    return json(res, 200, { status: 'ok', tip: worm.tip(), ts: new Date().toISOString() })
  }

  if (req.method === 'GET' && url.pathname === '/chain/verify') {
    const valid = worm.verifyChain()
    return json(res, 200, sealOutput('SEALFORGE_CORE', { chainValid: valid, tip: worm.tip() }))
  }

  if (req.method === 'POST' && url.pathname === '/events') {
    try {
      const raw = await readBody(req)
      const payload = raw ? JSON.parse(raw) : {}
      const entry = worm.append('event', payload)
      return json(res, 201, entry)
    } catch {
      return json(res, 400, { error: 'invalid_json' })
    }
  }

  if (req.method === 'GET' && url.pathname === '/events') {
    const limit = parseInt(url.searchParams.get('limit') ?? '100', 10)
    const offset = parseInt(url.searchParams.get('offset') ?? '0', 10)
    return json(res, 200, worm.getAll(limit, offset))
  }

  json(res, 404, { error: 'not_found' })
})

export function startServer(): void {
  server.listen(PORT, () => {
    console.log(`[SEALFORGE] Running on port ${PORT}`)
  })
}