Hi I deploy my application based on laravel 4 to fortrabbit. I try setting local and production environment
in bootstrap/start.php I modify
$env = $app->detectEnvironment(function () {
return getenv('LARAVEL_ENV') ?: 'local';
});
on fortrabbit i define env_var
LARAVEL_ENV to prod
but if i try in fortrabbit
php artisan env
i obtain local instead of prod
what is wrong in my code?
After setting the environment variable in your Fortrabbit's dashboard, you need to write this in your start.php file:
$env = $app->detectEnvironment(function () {
return isset($_SERVER['LARAVEL_ENV'])
? $_SERVER['LARAVEL_ENV']
: 'prod';
});
Note that it is better to fallback to the production environment in case there is no environment variable, since you don't want to mistakenly show debug logs on your production app.
Related
How can we pass the Username, Password from the CLI, ie Ubuntu terminal in cypress-cucumber-preprocessor. I have tried running my test using the following npx cypress-tags run command but it is not working. Could someone suggest what could be the problem here ? I would like to login with the user/ password passing from CLI
Cypress version: 8.7.0
CYPRESS_baseUrl=https://one.bookpro.com/main/ npx cypress-tags run -e 'PRO_USERNAME=CypTest2,PRO_PASSWORD=CypTest123' -e TAGS='#login' GLOB='tests/cypress/integration/**/*.feature' --headless --browser chrome
My .env file has the following
CYPRESS_PRO_USERNAME=CypTest1
CYPRESS_PRO_PASSWORD=CypTest123
If you add the package cypress-dotenv
npm install --save-dev dotenv cypress-dotenv
and set up cypress/plugins/index like this
const tagify = require('cypress-tags');
const dotenvPlugin = require('cypress-dotenv');
module.exports = (on, config) => {
on('file:preprocessor', tagify(config));
config = dotenvPlugin(config)
return config
};
you can automatically get items added in the .env file (check under Setting menu in the Cypress runner).
Same should work with cypress run I think.
Note If you want to have .env settings as default, but override on the command line sometimes, the above will not do it.
You need to merge the config as follows
module.exports = (on, config) => {
on('file:preprocessor', tagify(config));
const fromDotEnv = dotenvPlugin(config)
config = {
...fromDotEnv, // from the .env file
...config // CLI -e PRO_USERNAME=CypTest2,PRO_PASSWORD=CypTest123
}
return config
};
Also, I had to remove the ' around the PRO_USERNAME=CypTest2,PRO_PASSWORD=CypTest123 but that may be an OS issue.
I have deployed my app on Heroku and on the start of my app I check a config file and in there I want to access a config var I have created on Heroku API_KEY to define my Firebase config:
module.exports = {
fireConfig: {
apiKey: process.env.API_KEY,
authDomain: "my-app.firebaseapp.com",
databaseURL: "https://my-app.firebaseio.com",
projectId: "my-project-id",
storageBucket: "my-app.appspot.com",
messagingSenderId: "my-messaging-sender-id"
}
};
this process.env.API_KEY is undefined. Should I use a different way to access it?
You can define environment variables in your nuxt.config.js file, e.g.
export default {
env: {
firebaseApiKey: process.env.API_KEY || 'default value'
}
}
Here we use the API_KEY environment variable, if it's available, and assign that to firebaseApiKey.
If that environment variable isn't set, e.g. maybe in development (you can set it there too if you want), we fall back to 'default value'. Please don't put your real API key in 'default value'. You could use a separate throwaway Firebase account key here or just omit it (take || 'default value' right out) and rely on the environment variable being set.
These will be processed at build time and then made available using the name you give them, e.g.
module.exports = {
fireConfig: {
apiKey: process.env.firebaseApiKey, # Not API_KEY
// ...
};
I'm trying to run browserstack behind the firewall.
I tried to run this command on terminal:
RK$ ./BrowserStackLocal --key <key> --force-local
BrowserStackLocal v7.0
You can now access your local server(s) in our remote browser.
Press Ctrl-C to exit
I opened another terminal and I ran the command
npm run test:functional:cr:mobile
I get the following error:
1) Run sample test flow page:
Uncaught WebDriverError: [browserstack.local] is set to true but local testing through BrowserStack is not connected.
This is my config.js
'use strict'
import webdriver from 'selenium-webdriver'
let driver
module.exports = {
getDriverConfiguration: function (testTitle, browserName) {
var capabilities = {
'browserName': process.env.BROWSER || 'Chrome',
'realMobile': 'true',
'os': 'android',
'deviceName': process.env.DEVICE || 'Samsung Galaxy S8',
'browserstack.user': 'USER',
'browserstack.key': 'KEY',
'browserstack.debug': 'true',
'build': 'Build for mobile testing',
'browserstack.local' : 'true',
'browserstack.localIdentifier' : 'Test123'
}
driver = new webdriver.Builder().withCapabilities(capabilities).usingServer('http://hub-cloud.browserstack.com/wd/hub').build()
driver.manage().deleteAllCookies()
return driver
}
}
I enabled browserstack.local to true but I still get this error.
Not sure where I'm going wrong.
Please kindly help.
The error [browserstack.local] is set to true but local testing through BrowserStack is not connected. is returned if your BrowserStackLocal connection (the one you established using ./BrowserStackLocal --key --force-local) is disconnected.
I would suggest you use the following approach instead, to avoid the additional step and easily manage your local testing connection:
npm install browserstack-local
Once you have installed the browserstack-local module, use the following code snippet as reference to modify your code and start browserstack-local from your code itself(before the line driver = new webdriver.Builder().withCapabilities(capabilities).usingServer('http://hub-cloud.browserstack.com/wd/hub').build()), instead of starting it from a separate terminal window:
var browserstack = require('browserstack-local');
//creates an instance of Local
var bs_local = new browserstack.Local();
// replace <browserstack-accesskey> with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
var bs_local_args = { 'key': '<browserstack-accesskey>', 'forceLocal': 'true' };
// starts the Local instance with the required arguments
bs_local.start(bs_local_args, function() {
console.log("Started BrowserStackLocal");
});
// check if BrowserStack local instance is running
console.log(bs_local.isRunning());
// stop the Local instance
bs_local.stop(function() {
console.log("Stopped BrowserStackLocal");
});
I have a React app which makes API requests to a Laravel backend.
My app is hosted on Heroku (I do not know if it changes something for my question).
I would like to differentiate a production and a local environment for these requests. I do it as follows.
In my "welcome.blade.php", I add this meta tag:
<meta name="app_env" content="<?php echo env("APP_ENV") ?>" />
The APP_ENV contains either "production" or "local".
In my React app, I have this script:
export let urlApi = (document.querySelector("[name=app_env]").content === "production" ) ?
"https://laravel-react.herokuapp.com/api"
:
"http://localhost/laravel_react/public/api"
;
And I import this function in each component which needs it:
import { urlApi } from './../findUrlApi';
// .....
return fetch(`${urlApi}/products`,{
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer "+localStorage.getItem("token")
}
})
It works fine.
But my question is, is it a good practice? (I am a beginner in React).
I don't think that it is a good practice. Environment variables (like APP_ENV and API URLs) should not reside in the source code.
However, you could store them like usual in Laravel .env file but by prefixing the key with MIX_. For example: MIX_API_URL=http://localhost. Every MIX_* variables in .env file will be exposed to your React application. Then, you could get MIX_API_URL value from your React application by calling process.env.MIX_API_URL.
Updated Laravel .env file
...
MIX_APP_ENV=production (or local) # Should be same as APP_ENV
MIX_API_LOCAL_URL=http://localhost/laravel_react/public/api
MIX_API_PRODUCTION_URL=https://laravel-react.herokuapp.com/api
In React components that need it
const { MIX_APP_ENV, MIX_API_LOCAL_URL, MIX_API_PRODUCTION_URL } = process.env;
const apiUrl = MIX_APP_ENV === 'local'? MIX_API_LOCAL_URL: MIX_API_PRODUCTION_URL;
return fetch(apiUrl + '/products', { ... });
If calling process.env.MIX_API_URL does not work and you are running npm run watch, try restarting npm run watch and hard reload your browser.
Reference
Laravel Documentation - Compiling Assets (Mix) - Environment
Variables
In running kue-scheduler on heroku with the heroku redis plugin, while I can get kue jobs to work, it seems that kue-scheduler is requiring certain configuration of redis not allowed for in the heroku redis environment. Has anyone had success running kue-scheduler in an Heroku environment. Here is the start of my index.js file:
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');
var kue = require('kue-scheduler')
var queue = kue.createQueue({redis:
'redis://h:***************#ec2-**-19-83-130.compute-1.amazonaws.com:23539'
});
var job = queue.create('test', {
title: 'Hello world'
, to: 'j#example.com'
, template: 'welcome-email'
}).save( function(err){
if( !err ) console.log( job.id );
});
job.log('$Job %s run', job.id);
queue.every('30 seconds', job);
queue.process('test', function(job, done){
test_function(job.data.title, done);
});
function test_function(title, done) {
console.log('Ran test function with title %s', title)
// email send stuff...
done();
}
And here is the error.
2016-07-21T00:46:26.445297+00:00 app[web.1]: /app/node_modules/parse-server/lib/ParseServer.js:410
2016-07-21T00:46:26.445299+00:00 app[web.1]: throw err;
2016-07-21T00:46:26.445300+00:00 app[web.1]: ^
2016-07-21T00:46:26.445417+00:00 app[web.1]: ReplyError: ERR unknown command 'config'
2016-07-21T00:46:26.445419+00:00 app[web.1]: at parseError (/app/node_modules/redis-parser/lib/parser.js:161:12)
2016-07-21T00:46:26.445420+00:00 app[web.1]: at parseType (/app/node_modules/redis-parser/lib/parser.js:222:14)
2016-07-21T00:46:26.466188+00:00 app[web.1]:
The issue is that heroku redis doesn't allow config options on its redis infrastructure from what I can tell.
If someone has had success, grateful for any suggestions.
managed to solve this by:
var queue = kue.createQueue(
{redis: 'redis://xxxxxxxxxxxxx#ec2-50-19-83-130.compute-1.amazonaws.com:23539',
skipConfig: true
});
Just need the skipConfig parameter
I was having the same problem and was unable to get kue-scheduler working on Heroku-Redis. To solve, I instead used the Heroku Add-on Redis Cloud.
This allows you to set the required Redis flag notify-keyspace-events which isn't modifiable on the regular Heroku-Redis add-on. To set this flag:
Add Redis Cloud heroku add-on
Go to heroku settings page
Reveal Config Vars in Config Variables
Copy REDISCLOUD_URL, it should be something like redis://rediscloud:PASSWORD#xxx.redislabs.com:PORT_NUMBER
In terminal enter redis-cli -h xxx.redislabs.com -p PORT_NUMBER -a PASSWORD with variables from REDISCLOUD_URL
Once connected, enter config set notify-keyspace-events Ex
You can verify it is set correctly by entering config get notify-keyspace-events
Make sure to update your javascript code to point to your new REDISCLOUD_URL when calling kue.createQueue()
credit to #josephktcheung for their work though here: https://github.com/lykmapipo/kue-scheduler/issues/46