git ssb

1+

dinoworm ๐Ÿ› / catstack



Tree: e1b55c0cca1102ca828c984773c303b09b965ff0

Files: e1b55c0cca1102ca828c984773c303b09b965ff0 / app / proxy.js

1181 bytesRaw
1const http = require('http')
2const httpProxy = require('http-proxy')
3const Url = require('url')
4const startsWith = require('lodash').startsWith
5const assign = require('lodash').assign
6
7module.exports = { createServer }
8
9function createServer (config) {
10 const proxy = httpProxy.createProxyServer({
11 ignorePath: true
12 })
13
14 const server = http.createServer(function (req, res) {
15 const url = Url.parse(req.url)
16
17 if (matches(config.api, url.pathname)) {
18 proxy.web(req, res, { target: targetUrl(config.api, url.pathname) })
19 } else if (matches(config.static, url.pathname)) {
20 proxy.web(req, res, { target: targetUrl(config.static, url.pathname) })
21 } else {
22 proxy.web(req, res, { target: targetUrl(config.render, url.pathname) })
23 }
24 })
25
26 return server
27}
28
29function targetUrl (service, pathname) {
30 const url = assign(
31 {}, service.url,
32 { port: service.port },
33 { pathname: targetPath(service, pathname) }
34 )
35 return Url.format(url)
36}
37
38function targetPath (service, pathname) {
39 return pathname.slice(service.url.pathname.length - 1)
40}
41
42function matches (service, pathname) {
43 return startsWith(pathname, service.url.pathname)
44}
45

Built with git-ssb-web