forked from hoodiehq/hoodie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
public.js
138 lines (127 loc) · 3.43 KB
/
public.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
module.exports.register = register
module.exports.register.attributes = {
name: 'hoodie-public',
dependencies: 'inert'
}
var fs = require('fs')
var path = require('path')
function register (server, options, next) {
var app = path.join(options.config.paths.public, 'index.html')
var hoodieVersion
try {
hoodieVersion = require('hoodie/package.json').version
} catch (err) {
hoodieVersion = 'development'
}
var hoodiePublicPath = path.join(require.resolve('../../package.json'), '..', 'public')
var accountPublicPath = path.join(require.resolve('@hoodie/account/package.json'), '..', 'public')
var storePublicPath = path.join(require.resolve('@hoodie/store/package.json'), '..', 'public')
var adminPublicPath = path.join(require.resolve('@hoodie/admin/package.json'), '..', 'dist')
server.route([{
method: 'GET',
path: '/{p*}',
handler: {
directory: {
path: options.config.paths.public,
listing: false,
index: true
}
}
}, {
method: 'GET',
path: '/hoodie/{p*}',
handler: {
directory: {
path: hoodiePublicPath,
listing: false,
index: true
}
}
}, {
method: 'GET',
path: '/hoodie/account/{p*}',
handler: {
directory: {
path: accountPublicPath,
listing: false,
index: true
}
}
}, {
method: 'GET',
path: '/hoodie/store/{p*}',
handler: {
directory: {
path: storePublicPath,
listing: false,
index: true
}
}
}, {
method: 'GET',
path: '/hoodie/admin/{p*}',
handler: {
directory: {
path: adminPublicPath,
listing: false,
index: true
}
}
}, {
method: 'GET',
path: '/hoodie/info.json',
handler: function (request, reply) {
reply({
hoodie: true,
name: options.config.name,
version: hoodieVersion
})
}
}, {
method: 'GET',
path: '/hoodie/client.js',
handler: {
file: path.join(options.config.paths.data, 'client.js')
}
}, {
method: 'GET',
path: '/hoodie/client.min.js',
handler: {
file: path.join(options.config.paths.data, 'client.min.js')
}
}])
// serve app whenever an html page is requested
// and no other document is available
// TODO: do not serve app when request.path starts with `/hoodie/`
server.ext('onPostHandler', function (request, reply) {
var response = request.response
if (!response.isBoom) {
return reply.continue()
}
var is404 = response.output.statusCode === 404
var isHTML = /text\/html/.test(request.headers.accept)
var isAccountPath = /^\/hoodie\/account\//.test(request.path)
var isAdminPath = /^\/hoodie\/admin\//.test(request.path)
var isStorePath = /^\/hoodie\/store\//.test(request.path)
var isHoodiePath = /^\/hoodie\//.test(request.path)
// We only care about 404 for html requests...
if (!is404 || !isHTML) {
return reply.continue()
}
if (isAccountPath) {
return reply(fs.createReadStream(path.join(accountPublicPath, 'index.html')))
}
if (isAdminPath) {
return reply(fs.createReadStream(path.join(adminPublicPath, 'index.html')))
}
if (isStorePath) {
return reply(fs.createReadStream(path.join(storePublicPath, 'index.html')))
}
if (isHoodiePath) {
return reply.continue()
}
// Serve index.html
reply(fs.createReadStream(app))
})
return next()
}