|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +require('rubico/global') |
| 4 | +const http = require('http') |
| 5 | + |
| 6 | +/** |
| 7 | + * @name runserver |
| 8 | + * |
| 9 | + * @synopsis |
| 10 | + * ```coffeescript [specscript] |
| 11 | + * module http |
| 12 | + * |
| 13 | + * runserver(options? { |
| 14 | + * port?: number, |
| 15 | + * }) -> { server http.Server } |
| 16 | + * ``` |
| 17 | + */ |
| 18 | +function runserver(options = {}) { |
| 19 | + const { port = 8080 } = options |
| 20 | + |
| 21 | + const userTable = { |
| 22 | + async getById(userId) { |
| 23 | + return { |
| 24 | + id: userId, |
| 25 | + name: `User ${userId}`, |
| 26 | + createTime: 1, |
| 27 | + birthdate: '2020-01-01', |
| 28 | + profilePictureUrl: 'https://rubico.land/assets/rubico-logo.png', |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + function healthCheckHandler(request, response) { |
| 34 | + response.writeHead(200, { |
| 35 | + 'Content-Type': 'text/plain', |
| 36 | + }) |
| 37 | + response.end('ok') |
| 38 | + } |
| 39 | + |
| 40 | + function optionsHandler(request, response) { |
| 41 | + response.writeHead(204, { |
| 42 | + 'Access-Control-Allow-Origin': '*', |
| 43 | + 'Access-Control-Allow-Methods': '*', |
| 44 | + 'Access-Control-Allow-Headers': '*', |
| 45 | + 'Access-Control-Max-Age': '86400', |
| 46 | + }) |
| 47 | + response.end() |
| 48 | + } |
| 49 | + |
| 50 | + async function getUserHandler(request, response) { |
| 51 | + const userId = request.url.match(/\d+/)[0] |
| 52 | + |
| 53 | + // validate |
| 54 | + if (isNaN(Number(userId))) { |
| 55 | + const error = new Error('Bad Request') |
| 56 | + error.code = 400 |
| 57 | + throw error |
| 58 | + } |
| 59 | + |
| 60 | + // userTable is a theoretical client for a database |
| 61 | + const user = await userTable.getById(userId) |
| 62 | + |
| 63 | + // handle not found |
| 64 | + if (user == null) { |
| 65 | + const error = new Error('Not Found') |
| 66 | + error.code = 404 |
| 67 | + throw error |
| 68 | + } |
| 69 | + |
| 70 | + // ensure no private user information is exposed |
| 71 | + const publicUser = { |
| 72 | + id: user.id, |
| 73 | + name: user.name, |
| 74 | + birthdate: user.birthdate, |
| 75 | + profilePictureUrl: user.profilePictureUrl, |
| 76 | + createTime: user.createTime, |
| 77 | + } |
| 78 | + |
| 79 | + // send back the user resource in the response body |
| 80 | + response.writeHead(200, { |
| 81 | + 'Access-Control-Allow-Origin': '*', |
| 82 | + 'Content-Type': 'application/json', |
| 83 | + }) |
| 84 | + response.end(JSON.stringify({ |
| 85 | + user: publicUser, |
| 86 | + })) |
| 87 | + } |
| 88 | + |
| 89 | + function notFoundHandler(request, response) { |
| 90 | + response.writeHead(404, { |
| 91 | + 'Content-Type': 'text/plain', |
| 92 | + }) |
| 93 | + response.end('Not Found') |
| 94 | + } |
| 95 | + |
| 96 | + function errorHandler(request, response) { |
| 97 | + console.error(error) |
| 98 | + if (typeof error.code != 'number') { |
| 99 | + error.code = 500 |
| 100 | + } |
| 101 | + response.writeHead(error.code, { |
| 102 | + 'Access-Control-Allow-Origin': '*', |
| 103 | + 'Content-Type': 'text/plain', |
| 104 | + }) |
| 105 | + response.end(error.message) |
| 106 | + } |
| 107 | + |
| 108 | + const combinedHandler = tryCatch( |
| 109 | + switchCase([ |
| 110 | + request => request.url.startsWith('/health'), |
| 111 | + healthCheckHandler, |
| 112 | + |
| 113 | + request => request.method == 'OPTIONS', |
| 114 | + optionsHandler, |
| 115 | + |
| 116 | + request => request.method == 'GET' && /^\/user\/\d+$/.test(request.url), |
| 117 | + getUserHandler, |
| 118 | + |
| 119 | + notFoundHandler, |
| 120 | + ]), |
| 121 | + |
| 122 | + errorHandler |
| 123 | + ) |
| 124 | + |
| 125 | + const server = http.createServer(combinedHandler) |
| 126 | + |
| 127 | + server.listen(port) |
| 128 | + |
| 129 | + return { server } |
| 130 | +} |
| 131 | + |
| 132 | +module.exports = runserver |
0 commit comments