Skip to content

Commit

Permalink
base64 decode map added for app secret
Browse files Browse the repository at this point in the history
  • Loading branch information
somnathbm committed Nov 24, 2024
1 parent b073c8f commit 3163bcf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,29 @@ import (
"go.mongodb.org/mongo-driver/v2/mongo/options"

"hms_patient_mgmt_svc/models"
"hms_patient_mgmt_svc/utils"
)

// Get mongo collection for 1 minute connection pool
func get_db_collection() (*mongo.Collection, *mongo.Client) {
_, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
client, err := mongo.Connect(options.Client().ApplyURI(os.Getenv("APP_DB_URI")))
var base64_encoded_data = map[string]string{
"APP_DB_URI": os.Getenv("APP_DB_URI"),
"APP_DB_NAME": os.Getenv("APP_DB_NAME"),
"COLLECTION_NAME": os.Getenv("COLLECTION_NAME"),
}

base64_decoded_data, base64_err := utils.DecodeBase64(base64_encoded_data)
if base64_err != nil {
// fmt.Println("base64 decode error", base64_err.Error())
}

client, err := mongo.Connect(options.Client().ApplyURI(base64_decoded_data["APP_DB_URI"]))
if err != nil {
panic(err)
}
collection := client.Database(os.Getenv("APP_DB_NAME")).Collection(os.Getenv("COLLECTION_NAME"))
collection := client.Database(base64_decoded_data["APP_DB_NAME"]).Collection(base64_decoded_data["COLLECTION_NAME"])
return collection, client
}

Expand Down
11 changes: 5 additions & 6 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package main

import (
"hms_patient_mgmt_svc/api"

"github.com/joho/godotenv"
// "github.com/joho/godotenv"
)

func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}
// err := godotenv.Load()
// if err != nil {
// panic(err)
// }
api.Api()
}
18 changes: 18 additions & 0 deletions src/utils/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import (
"encoding/base64"
)

func DecodeBase64(base64_encode_data map[string]string) (map[string]string, error) {
var decoded_map map[string]string = make(map[string]string)

for k, v := range base64_encode_data {
data, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return nil, err
}
decoded_map[k] = string(data)
}
return decoded_map, nil
}

0 comments on commit 3163bcf

Please sign in to comment.