How I hid my keys from you

My GitHub repo wasn't available yet for a simple motive: my DB hostname, user and password were explicitly written in my code. So I had to find a way to hide these keys while still having an open-source code.

Just remember, this isn't some pro hacker move and I am a cyber security idiot for now, but it works. My keys are still in my machine and will never go to Github.

I added a new guy to my repo called godotenv. What it does is simply create environment keys and values from a .env file. So I created my .env:


So now, All I have to do is get these keys in our ConnectToDb function.

func ConnectToDb() {
   if err := godotenv.Load(); err != nil {
      log.Fatal("Error loading .env file")

   dbHost := os.Getenv("DB_HOST")
   dbUser := os.Getenv("DB_USER")
   dbPassword := os.Getenv("DB_PASSWORD")
   dbName := os.Getenv("DB_NAME")

   dsn := "host=" + dbHost + " user=" + dbUser + " password=" + dbPassword + " dbname=" + dbName + " port=5432 sslmode=disable"
   DB, err = gorm.Open(postgres.Open(dsn))
   if err != nil {
      log.Panic("Error connecting to database")
   err := DB.AutoMigrate(&models.Activity{})
   if err != nil {
      log.Panic("Error auto migrating Activity model")

But I can't just put in another file and upload it to Github. So I just added any .env files to my gitignore and it won't ever be accidentally pushed to my public repository.

Is it the most advanced privacy setting of all time? No. But remember that this isn't a senior systems engineer, but a guy trying to learn new stuff. I will most definitely come back to this and create a better solution than this, I promise. But for now, this allows me to open my repository for the world without getting vicious bills from AWS.

Well, I'll see you in the next post. Stay curious, everyone!

Check out my project repository!