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

NEXT_PUBLIC_API_URI Env Variable not loading correctly #43

Open
Iulian-Dragomirescu opened this issue Aug 9, 2024 · 22 comments
Open

NEXT_PUBLIC_API_URI Env Variable not loading correctly #43

Iulian-Dragomirescu opened this issue Aug 9, 2024 · 22 comments
Labels
question Further information is requested self-hosting Issues related to self-hosting Plunk

Comments

@Iulian-Dragomirescu
Copy link

Hello, I tried this open source project today, but I encountered a problem with the request made by the UI to the Backend over HTTPS

my env file for api is:

API_URI=https://plunk.exemple/api
APP_URI=https://plunk.exemple.ro

but in the request made by the UI to the Backend it is like this:
https://plunk.exemple.ro/plunk.exemple.ro/api/users/@me is 404 Not Found

@driaug
Copy link
Member

driaug commented Aug 9, 2024

What is your .env file for the dashboard?

Also, is this for running Plunk locally on your machine?

@driaug driaug added the question Further information is requested label Aug 9, 2024
@Iulian-Dragomirescu
Copy link
Author

Is not locally, i use coolify on a VPS
The env is managed by coolify with

API_URI=https://plunk.exemple/api
APP_URI=https://plunk.exemple.ro

AWS... etc

This is the docker-compose file

version: '3'
services:
  plunk:
    image: driaug/plunk
    ports:
      - '3001:5000'
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    environment:
      REDIS_URL: 'redis://redis:6379'
      DATABASE_URL: 'postgresql://postgres:${POSTGRES_PASSWORD}@db:5432/postgres'
      JWT_SECRET: '${JWT_SECRET}'
      AWS_REGION: '${AWS_REGION}'
      AWS_ACCESS_KEY_ID: '${AWS_ACCESS_KEY_ID}'
      AWS_SECRET_ACCESS_KEY: '${AWS_SECRET_ACCESS_KEY}'
      AWS_SES_CONFIGURATION_SET: '${AWS_SES_CONFIGURATION_SET}'
      NEXT_PUBLIC_API_URI: '${API_URI}'
      API_URI: '${API_URI}'
      APP_URI: '${APP_URI}'
      DISABLE_SIGNUPS: '${DISABLE_SIGNUPS}'
    entrypoint:
      - /app/entry.sh
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: '${POSTGRES_PASSWORD}'
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres
    volumes:
      - 'postgres_data:/var/lib/postgresql/data'
    healthcheck:
      test:
        - CMD-SHELL
        - 'pg_isready -U postgres -d postgres'
      interval: 10s
      retries: 5
      timeout: 10s
  redis:
    image: redis

@driaug
Copy link
Member

driaug commented Aug 9, 2024

I believe it might be because your API_URI is not a valid URL.
Everything else looks fine.

@angelhodar
Copy link

@driaug I am facing the same issue trying to deploy it self hosted with dokploy via docker image. I have tried multiple combinations and the problem is that the request made to the backend is as described by @Iulian-Dragomirescu. Also there is a variable called NEXT_PUBLIC_API_URI that is used but not described in the docs. I have seen the variables usage in the code and what makes sense for me is this, or at least it doesnt crash the server:

NEXT_PUBLIC_API_URI=/api
API_URI=https://emails.angelhodar.com/api
APP_URI=emails.angelhodar.com

With this config, if I try to create an account from the /auth/signup page it makes the request to:

https://emails.angelhodar.com/auth/emails.angelhodar.com/api/auth/signup

Are we missing something?

@jaaneh
Copy link

jaaneh commented Aug 9, 2024

image
Also unfortunately seeing the same issue with signing up. I'm on Railway. The request is sent to https://<subdomain>.up.railway.app/api/auth/signup and https://<subdomain>.up.railway.app/api/users/@me but receives 404

env is set to

API_URI="https://<subdomain>.up.railway.app/api"
APP_URI="https://<subdomain>.up.railway.app"

FWIW, the confirmation from SNS topic also never gets logged, so I assume that's also being 404'd.

@Iulian-Dragomirescu
Copy link
Author

Iulian-Dragomirescu commented Aug 9, 2024

now, https://plunk.exemple.ro/plunk.exemple.ro/api/users/@me no longer appears, and it is a correct format ( https://plunk.exemple.ro/api/auth/signup), but it is 404.. same as @jaaneh

update: it appeared twice, again ;)) the url https://plunk.exemple.ro/plunk.exemple.ro/api/users/@me without do anything, redeploy etc

@driaug
Copy link
Member

driaug commented Aug 10, 2024

I myself always test using the Docker Compose setup provided in the docs.

This maps everything appropriately since API_URI and NEXT_PUBLIC_API_URI should be the same.
My setup looks like this and runs flawlessly.

API_URI=https://plunk.example.com/api
APP_URI=https://plunk.example.com
version: '3'
services:
  plunk:
    image: "driaug/plunk"
    ports:
      - "3000:3000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    environment:
      REDIS_URL: '${REDIS_URL:-redis://redis:6379}'
      DATABASE_URL: '${DATABASE_URL:-postgresql://postgres:postgres@db:5432/postgres}'
      JWT_SECRET: '${JWT_SECRET}'
      AWS_REGION: '${AWS_REGION}'
      AWS_ACCESS_KEY_ID: '${AWS_ACCESS_KEY_ID}'
      AWS_SECRET_ACCESS_KEY: '${AWS_SECRET_ACCESS_KEY}'
      AWS_SES_CONFIGURATION_SET: '${AWS_SES_CONFIGURATION_SET}'
      NEXT_PUBLIC_API_URI: '${API_URI}'
      API_URI: '${API_URI}'
      APP_URI: '${APP_URI}'
      DISABLE_SIGNUPS: 'False'
    entrypoint: [ "/app/entry.sh" ]
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres -d postgres" ]
      interval: 10s
      retries: 5
      timeout: 10s
  redis:
    image: redis

@driaug
Copy link
Member

driaug commented Aug 10, 2024

image Also unfortunately seeing the same issue with signing up. I'm on Railway. The request is sent to https://<subdomain>.up.railway.app/api/auth/signup and https://<subdomain>.up.railway.app/api/users/@me but receives 404

env is set to

API_URI="https://<subdomain>.up.railway.app/api"
APP_URI="https://<subdomain>.up.railway.app"

FWIW, the confirmation from SNS topic also never gets logged, so I assume that's also being 404'd.

Are you deploying this manually or are you using the provided template for Railway?

https://railway.app/template/T2xefJ

@Iulian-Dragomirescu
Copy link
Author

I myself always test using the Docker Compose setup provided in the docs.

This maps everything appropriately since API_URI and NEXT_PUBLIC_API_URI should be the same. My setup looks like this and runs flawlessly.

API_URI=https://plunk.example.com/api
APP_URI=https://plunk.example.com
version: '3'
services:
  plunk:
    image: "driaug/plunk"
    ports:
      - "3000:3000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    environment:
      REDIS_URL: '${REDIS_URL:-redis://redis:6379}'
      DATABASE_URL: '${DATABASE_URL:-postgresql://postgres:postgres@db:5432/postgres}'
      JWT_SECRET: '${JWT_SECRET}'
      AWS_REGION: '${AWS_REGION}'
      AWS_ACCESS_KEY_ID: '${AWS_ACCESS_KEY_ID}'
      AWS_SECRET_ACCESS_KEY: '${AWS_SECRET_ACCESS_KEY}'
      AWS_SES_CONFIGURATION_SET: '${AWS_SES_CONFIGURATION_SET}'
      NEXT_PUBLIC_API_URI: '${API_URI}'
      API_URI: '${API_URI}'
      APP_URI: '${APP_URI}'
      DISABLE_SIGNUPS: 'False'
    entrypoint: [ "/app/entry.sh" ]
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres -d postgres" ]
      interval: 10s
      retries: 5
      timeout: 10s
  redis:
    image: redis

Using the same Docker Compose setup, the only modifications I made were to the port mapping 3001:3000 and the API_URI and APP_URI environment variables. However, I am still encountering the same issue

@angelhodar
Copy link

angelhodar commented Aug 11, 2024

I myself always test using the Docker Compose setup provided in the docs.

This maps everything appropriately since API_URI and NEXT_PUBLIC_API_URI should be the same. My setup looks like this and runs flawlessly.

API_URI=https://plunk.example.com/api
APP_URI=https://plunk.example.com
version: '3'
services:
  plunk:
    image: "driaug/plunk"
    ports:
      - "3000:3000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    environment:
      REDIS_URL: '${REDIS_URL:-redis://redis:6379}'
      DATABASE_URL: '${DATABASE_URL:-postgresql://postgres:postgres@db:5432/postgres}'
      JWT_SECRET: '${JWT_SECRET}'
      AWS_REGION: '${AWS_REGION}'
      AWS_ACCESS_KEY_ID: '${AWS_ACCESS_KEY_ID}'
      AWS_SECRET_ACCESS_KEY: '${AWS_SECRET_ACCESS_KEY}'
      AWS_SES_CONFIGURATION_SET: '${AWS_SES_CONFIGURATION_SET}'
      NEXT_PUBLIC_API_URI: '${API_URI}'
      API_URI: '${API_URI}'
      APP_URI: '${APP_URI}'
      DISABLE_SIGNUPS: 'False'
    entrypoint: [ "/app/entry.sh" ]
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres -d postgres" ]
      interval: 10s
      retries: 5
      timeout: 10s
  redis:
    image: redis

If I put the variables like this as you said:

NEXT_PUBLIC_API_URI="https://emails.angelhodar.com/api"
API_URI="https://emails.angelhodar.com/api"
APP_URI="https://emails.angelhodar.com"

Then the issue still, persists, you can test it here: https://emails.angelhodar.com/auth/signup

This are the server logs when booting:

image

@driaug
Copy link
Member

driaug commented Aug 11, 2024

If I put the variables like this as you said:

NEXT_PUBLIC_API_URI="https://emails.angelhodar.com/api"
API_URI="https://emails.angelhodar.com/api"
APP_URI="https://emails.angelhodar.com"

Then the issue still, persists, you can test it here: https://emails.angelhodar.com/auth/signup

This are the server logs when booting:

image

This is actually fairly interesting. Since the automated tasks on the backend are hitting the right URL, I believe it might have to do with the way we are replacing the NEXT_PUBLIC_API_URI on startup. Could you try repulling in the container or recreating the deployment now that you have the correct environment variables ready?

@angelhodar
Copy link

If I put the variables like this as you said:

NEXT_PUBLIC_API_URI="https://emails.angelhodar.com/api"
API_URI="https://emails.angelhodar.com/api"
APP_URI="https://emails.angelhodar.com"

Then the issue still, persists, you can test it here: https://emails.angelhodar.com/auth/signup
This are the server logs when booting:
image

This is actually fairly interesting. Since the automated tasks on the backend are hitting the right URL, I believe it might have to do with the way we are replacing the NEXT_PUBLIC_API_URI on startup. Could you try repulling in the container or recreating the deployment now that you have the correct environment variables ready?

I have repulled and its the same problem. One thing that I dont understand is this line in the Dockerfile:

ARG NEXT_PUBLIC_API_URI=PLUNK_API_URI

@driaug
Copy link
Member

driaug commented Aug 11, 2024

I have repulled and its the same problem. One thing that I dont understand is this line in the Dockerfile:

ARG NEXT_PUBLIC_API_URI=PLUNK_API_URI

Reason we need this is because Next.JS will take all NEXT_PUBLIC environment variables and hard-code them in the build files. This is not the behaviour we want since the environment variable is not yet available during build, build being the Github Action that builds and pushes to Docker Hub, so we need to replace the value on startup. PLUNK_API_URI is a placeholder we use to replace that with your actual environment variable.

@killion
Copy link
Contributor

killion commented Aug 13, 2024

If I put the variables like this as you said:

NEXT_PUBLIC_API_URI="https://emails.angelhodar.com/api"
API_URI="https://emails.angelhodar.com/api"
APP_URI="https://emails.angelhodar.com"

Then the issue still, persists, you can test it here: https://emails.angelhodar.com/auth/signup
This are the server logs when booting:
image

This is actually fairly interesting. Since the automated tasks on the backend are hitting the right URL, I believe it might have to do with the way we are replacing the NEXT_PUBLIC_API_URI on startup. Could you try repulling in the container or recreating the deployment now that you have the correct environment variables ready?

I think you are on the right track with this. I'm having the same issue deploying to AWS ECS. I think the sed command sets the api uri to...

let o = "plunk.example.com/api"
          , i = r.env.NEXT_PUBLIC_AWS_REGION
          , a = ["/auth/signup", "/auth/login", "/auth/reset", "/unsubscribe/[id]", "/subscribe/[id]"]

when it should be...

let o = "https://plunk.example.com/api"
          , i = r.env.NEXT_PUBLIC_AWS_REGION
          , a = ["/auth/signup", "/auth/login", "/auth/reset", "/unsubscribe/[id]", "/subscribe/[id]"]

@killion
Copy link
Contributor

killion commented Aug 14, 2024

Sorry, please ignore my previous post. It was because I had set the URI without https:// when I first setup Plunk and after I corrected it either Cloudflare or AWS cached it.

@driaug driaug changed the title Issue with https request to the api NEXT_PUBLIC_API_URI Env Variable not loading correctly Aug 15, 2024
@driaug driaug added the self-hosting Issues related to self-hosting Plunk label Aug 15, 2024
@Lermatroid
Copy link

Hey, I seem to be running into the same issue here. Has anyone found a fix?

@killion
Copy link
Contributor

killion commented Aug 18, 2024

For me the problem was that I hadn't set a full http url when I first setup the app and then it was aggressively cached by my server. Once I cleared the cache and set both the API_URI and APP_URI to a full http://blah.com url it worked.

If you look at the _next/static/chunks/pages/_app-<manifest hash>.js file for your API_URI you can see if it is set to something that you don't expect.

I added a check to the PR I pushed up that checks your env vars and throws an exception if either of those isn't set to something that starts with http. Because the code calls for those strings to start with that explicitly.

Hope that helps!

@jaaneh
Copy link

jaaneh commented Aug 23, 2024

Just revisiting this now as I had the same issue on a new deployment of Plunk.

It seems like Railway attaches the autogenerated domain to port 5000, changing this manually to 3000 and redeploying the service solved it for me. Unsure if this is the actual cause, but that at least did it for me.

Also tagging you in case you're deploying on Railway as well @Lermatroid

@Lermatroid
Copy link

Yep, that was the source of my error as well, however I forgot to post a followup comment about it. Thanks!

@ihadabs
Copy link

ihadabs commented Sep 4, 2024

I created a simple Dockerfile that uses a local PostgreSQL and Redis setup.

I build and run the container with the following command:

docker build -t my-plunk . && docker run --env-file .env -p 3000:3000 -it my-plunk

However, when I run it, I encounter this issue:
CleanShot 2024-09-04 at 20 30 58@2x

Notes:

  1. It seems like the application is running on port 5000
  2. But I’m also seeing this message: success [HTTPS] Ready on 4000. I'm not sure what this means or why it's happening.
  3. From my MacBook I can access the app on 3000 but can not sign up.
    CleanShot 2024-09-04 at 20 40 20@2x

Here’s the Dockerfile for reference:

FROM driaug/plunk:1.0.3

####################################################################################################
### General
ENV JWT_SECRET '<passed>'
ENV DISABLE_SIGNUPS 'False'

####################################################################################################
### Domain
ENV PORT 3000
ENV APP_URI 'http://localhost:3000'
ENV API_URI 'http://localhost:3000/api'
ENV NEXT_PUBLIC_API_URI 'http://localhost:3000/api'

####################################################################################################
### AWS
ENV AWS_REGION '<passed>'
ENV AWS_ACCESS_KEY_ID '<passed>'
ENV AWS_SECRET_ACCESS_KEY '<passed>'
ENV AWS_SES_CONFIGURATION_SET '<passed>'

####################################################################################################
### DB & Caching
ENV REDIS_URL '<passed>'
ENV DATABASE_URL '<passed>'


EXPOSE $PORT

@amosbastian
Copy link

amosbastian commented Sep 5, 2024

I had the same problem, and it was also caused by my environment variables not starting with https

@angelhodar
Copy link

angelhodar commented Sep 7, 2024

I can confirm the issue is gone when I have completely removed and restarted the project, it seems to be a problem with my hosting functionality and cache. The correct format for the variables is this one:

APP_URI 'http://localhost:3000'
API_URI 'http://localhost:3000/api'
NEXT_PUBLIC_API_URI 'http://localhost:3000/api'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested self-hosting Issues related to self-hosting Plunk
Projects
None yet
Development

No branches or pull requests

8 participants