124 lines
3.3 KiB
Go
124 lines
3.3 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 SatuDataDB *gorm.DB
|
|
var DBsimrs *gorm.DB
|
|
var err error
|
|
|
|
func ConnectDB() {
|
|
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")
|
|
|
|
hostDBsimrs := os.Getenv("SIMRS_HOST")
|
|
userNameDBsimrs := os.Getenv("SIMRS_USERNAME")
|
|
passwordDBsimrs := os.Getenv("SIMRS_PASSWORD")
|
|
dbNameDBsimrs := os.Getenv("SIMRS_NAME")
|
|
portDBsimrs := os.Getenv("SIMRS_PORT")
|
|
|
|
satuData := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Jakarta", hostSatuData, userNameSatuData, passwordSatuData, dbNameSatuData, portSatuData)
|
|
|
|
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")
|
|
}
|
|
|
|
dbSimrs := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Jakarta",
|
|
hostDBsimrs, userNameDBsimrs, passwordDBsimrs, dbNameDBsimrs, portDBsimrs)
|
|
|
|
DB, err = gorm.Open(postgres.Open(dbSimrs), &gorm.Config{})
|
|
if err != nil {
|
|
log.Fatal("Failed to connect to DBsimrs database: ", err)
|
|
} else {
|
|
log.Println("Successfully connected to the DBsimrs database")
|
|
}
|
|
}
|
|
|
|
func SetHeader(isIcare bool, cfg config.ConfigBpjs) *config.Header {
|
|
|
|
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")
|
|
|
|
if isIcare {
|
|
cfg.User_key = os.Getenv("USER_KEY_ICARE")
|
|
}
|
|
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))
|
|
|
|
header := &config.Header{
|
|
ConsID: cfg.Cons_id,
|
|
SecretKey: cfg.Secret_key,
|
|
UserKey: cfg.User_key,
|
|
TimeStamp: fmt.Sprint(tstamp),
|
|
XSignature: X_signature,
|
|
}
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
func SetHeaderDev(b bool, cfg config.ConfigBpjs) *config.Header {
|
|
|
|
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_DEV")
|
|
cfg.User_key = os.Getenv("USER_KEY_DEV")
|
|
cfg.Secret_key = os.Getenv("SECRET_KEY_DEV")
|
|
|
|
// 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))
|
|
|
|
header := &config.Header{
|
|
ConsID: cfg.Cons_id,
|
|
SecretKey: cfg.Secret_key,
|
|
UserKey: cfg.User_key,
|
|
TimeStamp: fmt.Sprint(tstamp),
|
|
XSignature: X_signature,
|
|
}
|
|
|
|
return header
|
|
|
|
}
|