Skip to content

Commit b45ac7b

Browse files
authored
Merge pull request #7 from eli0shin/custom-domain
add support for custom domain name
2 parents 3401018 + 739c513 commit b45ac7b

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,12 @@ To request data from a service the `iFC` inter-function communicator can be used
287287
It is simply a http client wrapped in a function that will automatically find and make a call to a service given standard http parameters.
288288
289289
Additional documentation can be found in `iFC.js`
290+
291+
## Custom domains
292+
293+
Firebase supports custom domains for functions via firebase hosting. The official firebase docs are available here: <https://firebase.google.com/docs/hosting/functions>.
294+
There is a bug (maybe?) in the firebase runtime that effects functions using a custom domain.
295+
This framework uses an express router under the hood for all http functions. When a request comes in through the default firebase url to the `/` path of a service with a `basePath` of `users` the request path is `/`. This matches the configured route. When the same function is reached via a firebase hosting rule that directs `<your-domain>/users` to the `users` function the request path is `/users`.
296+
We cannot check the domain of the request because the request domain is the same whether the requests originates from a custom domain or the default one. In order to provide support for both use-cases we have to mount the service's express.js router to 2 paths: `/` and `/<service-basePath>`, in this case `/` and `/users`.
297+
For many projects this will present no issue but in the case of a route that looks like this `/users/users` the behavior may be unpredictable.
298+
Hopefully in the future this issue will be fixed in the firebase runtime by the firebase team. Until then we must live with the constraints that this provides.

src/parseRoutes.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ module.exports = (
3333
);
3434
}
3535

36+
const router = new express.Router();
37+
3638
routes.forEach(
3739
({
3840
path,
@@ -44,7 +46,7 @@ module.exports = (
4446
middleware: routeMiddleware = [],
4547
}) => {
4648
if (ignoreBody) {
47-
app[method](
49+
router[method](
4850
`${path}`,
4951
validatePrivilege(privilege),
5052
...middleware,
@@ -53,7 +55,7 @@ module.exports = (
5355
handleRequest(privilege, toExecute)
5456
);
5557
} else if (method === 'post' && (routeSchema || postSchema || schema)) {
56-
app[method](
58+
router[method](
5759
`${path}`,
5860
validatePrivilege(privilege),
5961
...middleware,
@@ -65,7 +67,7 @@ module.exports = (
6567
handleRequest(privilege, toExecute)
6668
);
6769
} else if (method === 'put' && (routeSchema || schema)) {
68-
app[method](
70+
router[method](
6971
`${path}`,
7072
validatePrivilege(privilege),
7173
...middleware,
@@ -76,7 +78,7 @@ module.exports = (
7678
handleRequest(privilege, toExecute)
7779
);
7880
} else {
79-
app[method](
81+
router[method](
8082
`${path}`,
8183
validatePrivilege(privilege),
8284
...middleware,
@@ -89,6 +91,9 @@ module.exports = (
8991
}
9092
);
9193

94+
app.use(`/${service.basePath}`, router);
95+
app.use('/', router);
96+
9297
return app;
9398
};
9499

0 commit comments

Comments
 (0)