• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

secrets.js

Scheduled Pinned Locked Moved Unsolved Feature Requests
10 Posts 5 Posters 3.3k Views 4 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    rak
    last edited by Jun 8, 2018, 3:58 AM

    Hi guys,

    Yesterday I lost all my configuration bc my editor broke the file. Thats a pitty. It cost me a day to get it back working bc I use a lot of complicated modules e.g. like the openhab floorplan.

    That got me thinking.

    Config.js is typically not committed to git because of all the secret stuff like apiKeys for google, darksky, your lat/lon of your house which you might not want to share via github.

    How you guys are doing this? Having version control, but not publicly visible. Ok, I could pay github, or have a own server set up for my privat git. But sharing back with the community your own enhancements of the core system gets difficult.

    I thought about a simpler solution.

    What about splitting the configuration into two files:

    config.js

    var config = {
    ...
       { ...
    // instead of 
          apiKey1 = „my secret key“,
    // we do
          apiKey = this.secrets.apiKey1,
       }
    

    And we sum up all the secrets like lat/lon/apikeys in a dedicated file

    secrets.js

    var secrets = {
       apiKey1 = „MySuperSecretApiKey1“,
       Latitude = YouMustNotKnowWhereILive,
       ...
    }
    

    The secrets.js file must be read somehow by the core system and the this.secrets must be populated.

    And it would be downward compatible. If you dont have a secrets.js you are good to go as well.

    config.js could go in a public version control system and only secrets.js would stay in .gitignore.

    Is there an easy way to do this on my own? Some sorr of an „include secrets.js“ statement in javascript. Kind of bash like „source anotherBashScript“. I am new to JavaScript. Sorry if this is a silly question.

    Looking forward to your feedback.

    Regards
    Ralf

    C 1 Reply Last reply Jun 9, 2018, 2:41 AM Reply Quote 0
    • C Offline
      cowboysdude Module Developer @rak
      last edited by Jun 9, 2018, 2:41 AM

      @rak No offense but this is way more work then necessary. The current setup works great …the suggested thing to do is make a backup of your config.js before changing anything that way you never lose your work ;)

      1 Reply Last reply Reply Quote 0
      • R Offline
        rak
        last edited by Jun 9, 2018, 4:05 AM

        True. There is always an easy solution. Disadvantage is here you dont have versioning. GIT is a great tool. Why not using the advantage of it?

        C 1 Reply Last reply Jun 10, 2018, 2:16 AM Reply Quote 0
        • C Offline
          cowboysdude Module Developer @rak
          last edited by Jun 10, 2018, 2:16 AM

          @rak Well you’d have to take that up with the developers… so far this is the system that they implemented and it’s worked well so far :) You could always modify the config.js yourself to do what you want.

          1 Reply Last reply Reply Quote 0
          • M Offline
            mrmidi
            last edited by mrmidi Jun 10, 2018, 8:45 AM Jun 10, 2018, 8:45 AM

            @rak You may wish to use dotenv. You can either do this on a module level, or for your local install.

            https://www.npmjs.com/package/dotenv

            Here you would just make an .env file in your project root with something like:

            API_KEY=MySuperSecretApiKey1
            

            Then you should be able to update the config file with:

            var localConfig = require('dotenv').config();
            
            var config = {
            ...
               { ...
                  apiKey = process.env.API_KEY,
               }
            

            Just make sure to add the .env file to .gitignore so it won’t get committed via git.

            1 Reply Last reply Reply Quote 0
            • R Offline
              rak
              last edited by Jun 10, 2018, 6:04 PM

              @mrmidi Thank you very much. This goes into the desired direction.

              I have installed dotenv and added

              var localConfig = require('dotenv').config();
              

              If I do so starting the MM it shows missing config file.

              Any ideas?

              1 Reply Last reply Reply Quote 0
              • M Offline
                mrmidi
                last edited by Jun 10, 2018, 8:09 PM

                @rak This is most interesting :)

                I am actually working on testing this now. There is no real reason this should not have worked, but you are 100% right, it seems to tank the config.

                What’s really interesting is that I am tinkering around in the ./js/app.js file, and it’s actually not breaking when it’s being imported. The config file that comes in gets merged just fine and consoling it out, shows the correct dontenv getting replaced.

                This means somewhere else along the line, this code is breaking.

                Hopefully I can follow the config through the app and detect where that’s happening and post back ASAP.

                S 1 Reply Last reply Jun 10, 2018, 9:03 PM Reply Quote 0
                • M Offline
                  mrmidi
                  last edited by Jun 10, 2018, 8:58 PM

                  @rak OK, so after digging into this more, it looks like this won’t work without an update to the way the config.js file is loaded.

                  Currently, it is being loaded two ways.

                  1. As an exportable module that can be used via require() statements
                  2. As vanilla javascript

                  So the dotenv would have worked fine ( and DOES work fine ) when used in a module context. However, the vanilla JS cannot reference module data this way. The solution would be to dig more into the ./js/app.js and ./js/main.js files and look at how they are handling the loadConfig functions and see if there is not a way to tweak it for your purposes.

                  Sadly, there is not much more I can think of to help with that without spending several hours tinkering around.

                  1 Reply Last reply Reply Quote 0
                  • S Offline
                    strawberry 3.141 Project Sponsor Module Developer @mrmidi
                    last edited by Jun 10, 2018, 9:03 PM

                    @mrmidi I’m pretty sure the problem is, that the config file gets loaded in the browser. The browser doesn’t have require, it’s a nodejs feature.

                    https://github.com/MichMich/MagicMirror/search?q=CONFIG_FILE&unscoped_q=CONFIG_FILE

                    Please create a github issue if you need help, so I can keep track

                    1 Reply Last reply Reply Quote 0
                    • Y Offline
                      Ybbet
                      last edited by Jun 27, 2018, 7:32 PM

                      Hi!

                      I understand this issue. I use a gitlab private repository. I clone it in “magicmirror_config” in /home/pi/
                      And then I created a symlink im /home/pi/MagicMirror/config/ for my versioned config.js

                      And all my personalized files are in symlink where I need it.

                      That’s really easy and like that I can test on local device (my Mac) before put it on the raspberry Pi. :-)

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      • First post
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy