electerious / Ackee

Self-hosted, Node.js based analytics tool for those who care about privacy.
https://ackee.electerious.com
MIT License
4.2k stars 351 forks source link

Introduced BASE_URL environment variable #288

Open nergmada opened 2 years ago

nergmada commented 2 years ago

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[bot] commented 2 years ago

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

@electerious first needs to authorize it.

ghost commented 2 years ago

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 commented 2 years ago

@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

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 commented 2 years ago

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 commented 2 years ago

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...