summaryrefslogtreecommitdiff
path: root/server.mjs
blob: 24754427fbb332ad09798c1bcee669d01032c9c2 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict'

import { DatabaseSync } from 'node:sqlite'
import { createServer } from 'node:http'

const initializeDatabase = () => {
  const db = new DatabaseSync(':memory:')

  db.exec(`
    CREATE TABLE eagles(
      id INTEGER PRIMARY KEY,
      mac_address TEXT,
      key TEXT
    ) STRICT
  `)

  return db
}

const indexController = (req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' })
  res.end('It works!\n')
}

const getTimeControlller = (req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' })
  res.end(JSON.stringify({ time: Math.floor(Date.now() / 1000) }))
}

const eagleController = (req, res) => {
  const [, query] = req.url.split('?')
  const params = new URLSearchParams(query)

  switch (params.get('path')) {
    case 'eagle/GET/genId':
      eagleGenIdController(req, res)
      break
    default:
      notFoundController(req, res)
      break
  }
}

const eagleGenIdController = (req, res) => {
  const [, query] = req.url.split('?')
  const params = new URLSearchParams(query)
  const { mac, key } = JSON.parse(params.get('params') || '{}')

  if (mac === undefined || mac === null
    || key === undefined || key === null) {
    res.writeHead(400, { 'Content-Type': 'application/json' })
    res.end('{}\n')
    return
  }

  const record = database
    .prepare('INSERT INTO eagles(mac_address, key) VALUES(?, ?)')
    .run(mac, key)

  res.writeHead(200, { 'Content-Type': 'application/json' })
  res.end(JSON.stringify({
    status: 200,
    data: {
      eagleId: record.lastInsertRowid
    }
  }))
}

const exchangeController = (req, res) => {
  const [, query] = req.url.split('?')
  const params = new URLSearchParams(query)
  const device = params.get('device')

  if (device === null) {
    res.writeHead(422)
    res.end()
    return
  }

  const record = database
    .prepare('SELECT * FROM eagles WHERE id = ?')
    .get(device)

  if (record === undefined) {
    res.writeHead(404)
    res.end()
    return
  }

  res.writeHead(200, { 'Content-Type': 'application/json' })
  res.end(JSON.stringify({ key: record.key }))
}

const notFoundController = (req, res) => {
  res.writeHead(404)
  res.end()
}

const server = createServer((req, res) => {
  const routes = [
    { method: 'GET', path: '/', handler: indexController },
    { method: 'GET', path: '/gettime', handler: getTimeControlller },
    { method: 'GET', path: '/eagle', handler: eagleController },
    { method: 'GET', path: '/exchange', handler: exchangeController },
  ]

  const [path] = req.url.split('?')
  for (const route of routes) {
    if (req.method === route.method && path === route.path) {
      return route.handler(req, res)
    }
  }

  notFoundController(req, res)
})

const hostname = process.env.HOSTNAME || '0.0.0.0'
const port = process.env.PORT || 80
const database = initializeDatabase()

server.listen(port, hostname, () => {
  console.log(`Listening on ${hostname}:${port}`)
})