Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced BASE_URL environment variable #288

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

nergmada
Copy link

@nergmada nergmada commented Sep 6, 2021

Hi,

I've been doing some looking for Open Source privacy-first analytics services for a client project I am working on. I love the look of Ackee and in order to integrate it I've had to modify it a little.

Because our service runs on DigitalOcean's App Platform, we deploy in using a node droplet rather and a docker droplet. This is mostly to do with a desire to avoid using a reverse proxy. However, we encountered an issue when deploying on a URL other than root e.g.

www.mydomain.com/ackee

Ackee will deploy fine on www.mydomain.com however, because the HTML is hard coded to import index.js style.css etc. if these files are being served on a route other than the index route, the system falls over. More than that, I wasn't really in the mood to deploy an Nginx instance to wrap around this for the sake of rewriting the retrieval of 3 files.

For this reason, I've introduced a new environment variable BASE_URL which can essentially be expressed as

BASE_URL=/ackee

which will rewrite imports of index.js to point to /ackee/index.js instead. Similarly it also repoints /api requests to /ackee/api.

This change does not modify the serverless functions notion of it's route (e.g. src/server.js:96), on the basis that DO masks incoming requests e.g.

if I make a request to www.mydomain.com/ackee/api/getViews (I realise this isn't necessarily an actual endpoint, I'm just demonstrating), Ackee will get /api/getViews.

Depending on demand and use cases, it may be appropriate to introduce a SERVER_BASE_URL variable which similarly prepends the serverless instance's notion of its URL.

@vercel
Copy link

vercel bot commented Sep 6, 2021

Someone is attempting to deploy a commit to a Personal Account owned by @electerious on Vercel.

@electerious first needs to authorize it.

@ghost
Copy link

ghost commented Dec 15, 2021

Hello @nergmada ,
Nice one, I am also looking to integrate Ackee in an existing node app. Did it work for you as a separate node instance or is it running within your existing node instance (same starting point: index.js, app.js ...).
If it is the later case, It would really be very helpful, I don't know why this is not possible from the beginning, I'm expecting such an analytics app to be easily integrated, regardless of separation of concerns or whatever ...

thanks for your additions... I will wait for your confirmation to proceed with testing.

@nergmada
Copy link
Author

@bacloud14 so DO's App Platform is like an series of microservices all built on one domain but on different endpoints.

Not to get into the finer points of what I did but essentially we have lots of small node instances running on a domain such as www.example.com

  • client app, being served on /
  • a content management system, being served on /cms
  • an analytics service (Ackee), being served on /analytics

Each of these is an individual "server" running it's own node instance. It's kind of like kubernetes, but it's more online and gui based config.

If you can clarify your question a bit more about how you'd want to run it as a single a service?

Off the top of my head if you're running say an express server, on a single machine. I'd put your express server on say port 2000, your Ackee server on say port 3000 and then if you want express to forward all requests for /analytics on to Ackee do something like

app.use('/analytics/*', functionToForwardReqToLocalhost3000)

Note that your function would need to rewrite the request path to work with my code e.g.

/analytics/api/getViews would need to be rewritten as /api/getViews, not that it would be very hard.

Then set BASE_URL in Ackee to www.yourdomain.com/analytics or whatever path you sent for "/analytics/*"

-Adam

@ghost
Copy link

ghost commented Dec 15, 2021

I thought it would be possible for Ackee somehow to be a middleware inside an existing app. Without http messaging between the app being monitored and Ackee. That would probably ease the process of integration. Check this simple middleware for instance.
Thank you so much for giving me insights on how you are doing things.

Good luck

@ghost
Copy link

ghost commented Dec 15, 2021

Another example for analytics is express-keenio but they offer the service (DB and dashboard I imagine) on the cloud, so...
Now I see Tic seem to be self hosted and a middleware so let's see...

@@ -29,7 +29,7 @@ const scripts = () => {
return js(filePath, {
optimize: config.isDevelopmentMode === false,
nodeGlobals: config.isDevelopmentMode === true,
replace: { 'process.env.NODE_ENV': JSON.stringify(config.isDevelopmentMode === true ? 'development' : 'production') },
replace: { 'process.env.NODE_ENV': JSON.stringify(config.isDevelopmentMode === true ? 'development' : 'production'), 'process.env.BASE_URL': config.baseUrl },
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this up to the index function. The last argument of the layout function accepts environment variables that you can access via window.env in the UI scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants