R
Railway•16mo ago
raii6926

Error loading .env file

I tried the free trial to deploy my golang code then I encountered that problem in the deploy logs. even though I've added the environment variable in the railway. is there anything i missed?
38 Replies
Percy
Percy•16mo ago
Project ID: N/A
Brody
Brody•16mo ago
screenshot of your service variables please
raii6926
raii6926OP•16mo ago
is that what you mean?
Brody
Brody•16mo ago
please remove all database related variables and add them back with variable references https://docs.railway.app/develop/variables#reference-variables and another screenshot when youve done that please
raii6926
raii6926OP•16mo ago
like this? and the value just like that?
Brody
Brody•16mo ago
correct, keep doing that for all of them make sure to cancel the popup in the bottom right corner
raii6926
raii6926OP•16mo ago
like this?
raii6926
raii6926OP•16mo ago
but i dont understand what u mean
Brody
Brody•16mo ago
now wrap your godotenv code in this if block
// only load the .env file when running locally
// check for a RAILWAY_ENVIRONMENT, if not found, code is running locally
if _, exists := os.LookupEnv("RAILWAY_ENVIRONMENT"); exists == false {
// your godotenv code here
}
// only load the .env file when running locally
// check for a RAILWAY_ENVIRONMENT, if not found, code is running locally
if _, exists := os.LookupEnv("RAILWAY_ENVIRONMENT"); exists == false {
// your godotenv code here
}
reason being, you do not need to load an .env file on railway, as railway injects all the variables into the environment for you once you've implemented that code, show me a screenshot of that code in your code
raii6926
raii6926OP•16mo ago
wait thats my code, so what should i do is copy line of 35 til 38 paste to 41 ? or copying line of 44 til 49 paste to 41 ?
Brody
Brody•16mo ago
one sec, ill type it out for you
// only load the .env file when running locally
// check for a RAILWAY_ENVIRONMENT, if not found, code is running locally
if _, exists := os.LookupEnv("RAILWAY_ENVIRONMENT"); exists == false {
if err := godotenv.Load(); err != nil {
log.Fatal("error loading .env file:", err)
}
}
// only load the .env file when running locally
// check for a RAILWAY_ENVIRONMENT, if not found, code is running locally
if _, exists := os.LookupEnv("RAILWAY_ENVIRONMENT"); exists == false {
if err := godotenv.Load(); err != nil {
log.Fatal("error loading .env file:", err)
}
}
and show me a screenshot when you have implemented this code block please
raii6926
raii6926OP•16mo ago
like this?
Brody
Brody•16mo ago
full screenshot please
raii6926
raii6926OP•16mo ago
Brody
Brody•16mo ago
looks good
raii6926
raii6926OP•16mo ago
so should i run again im my terminal?
Brody
Brody•16mo ago
nothing changes locally, push the code to github side note: i see you're building the dsn string manually, i highly recommend this package to parse the MYSQL_URL variable and return a dsn string https://github.com/xo/dburl
raii6926
raii6926OP•16mo ago
i see, thank you so much
raii6926
raii6926OP•16mo ago
but its already working
raii6926
raii6926OP•16mo ago
thank you 🙂 i have spent my time for solve this with my self mildpanic thank youu again
Brody
Brody•16mo ago
no problem! have you generated a domain?
raii6926
raii6926OP•16mo ago
no, im sorry but idk a lot about this actually. because this for my assignments in college. so how can i get it?
Brody
Brody•16mo ago
go to your service settings and click generate domain
raii6926
raii6926OP•16mo ago
but the domain u mean its the link url ? like this one http://localhost:8000/users/ ?
Brody
Brody•16mo ago
thats a local address
raii6926
raii6926OP•16mo ago
yes i mean, get the domain for public ?
Brody
Brody•16mo ago
raii6926
raii6926OP•16mo ago
i see i get it
Brody
Brody•16mo ago
i suspect there is another issue?
raii6926
raii6926OP•16mo ago
yes it cant
raii6926
raii6926OP•16mo ago
why?
Brody
Brody•16mo ago
1. use https 2. set a service variable PORT = 8080
raii6926
raii6926OP•16mo ago
u mean that one i have to change to be 8080?
Brody
Brody•16mo ago
raii6926
raii6926OP•16mo ago
like that?
Brody
Brody•16mo ago
indeed
raii6926
raii6926OP•16mo ago
it works thank u so much
Brody
Brody•16mo ago
no problem 🙂
Want results from more Discord servers?
Add your server