77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package config
|
|
|
|
import (
|
|
"bridging-rssa/models/config"
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var DB *gorm.DB
|
|
var localDB *gorm.DB
|
|
var SatuDataDB *gorm.DB
|
|
var err error
|
|
|
|
func ConnectDB() {
|
|
hostDB := os.Getenv("DB_HOST")
|
|
usernameDB := os.Getenv("DB_USERNAME")
|
|
passwordDB := os.Getenv("DB_PASSWORD")
|
|
dbName := os.Getenv("DB_NAME")
|
|
portDB := os.Getenv("DB_PORT")
|
|
|
|
hostSatuData := os.Getenv("SATUDATA_HOST")
|
|
userNameSatuData := os.Getenv("SATUDATA_USERNAME")
|
|
passwordSatuData := os.Getenv("SATUDATA_PASSWORD")
|
|
dbNameSatuData := os.Getenv("SATUDATA_NAME")
|
|
portSatuData := os.Getenv("SATUDATA_PORT")
|
|
|
|
local := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Jakarta", hostDB, usernameDB, passwordDB, dbName, portDB)
|
|
|
|
satuData := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Jakarta", hostSatuData, userNameSatuData, passwordSatuData, dbNameSatuData, portSatuData)
|
|
|
|
localDB, err = gorm.Open(postgres.Open(local), &gorm.Config{})
|
|
if err != nil {
|
|
log.Fatal("Failed to connect to Satu Data database: ", err)
|
|
} else {
|
|
log.Println("Successfully connected to the database")
|
|
}
|
|
|
|
SatuDataDB, err = gorm.Open(postgres.Open(satuData), &gorm.Config{})
|
|
if err != nil {
|
|
log.Fatal("Failed to connect to Satu Data database: ", err)
|
|
} else {
|
|
log.Println("Successfully connected to the database")
|
|
}
|
|
}
|
|
|
|
func SetHeader(cfg config.ConfigBpjs) (string, string, string, string, string) {
|
|
|
|
timenow := time.Now().UTC()
|
|
time := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
tstamp := timenow.Unix() - time.Unix()
|
|
|
|
cfg.Cons_id = os.Getenv("CONS_ID")
|
|
cfg.User_key = os.Getenv("USER_KEY")
|
|
cfg.Secret_key = os.Getenv("SECRET_KEY")
|
|
|
|
secret := []byte(cfg.Secret_key)
|
|
message := []byte(cfg.Cons_id + "&" + fmt.Sprint(tstamp))
|
|
hash := hmac.New(sha256.New, secret)
|
|
hash.Write(message)
|
|
// to lowercase hexits
|
|
hex.EncodeToString(hash.Sum(nil))
|
|
// to base64
|
|
X_signature := base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
|
|
|
return cfg.Cons_id, cfg.Secret_key, cfg.User_key, fmt.Sprint(tstamp), X_signature
|
|
|
|
}
|