github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/app/app.go (about)

     1  package app
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	"github.com/mundipagg/boleto-api/api"
     8  	"github.com/mundipagg/boleto-api/certificate"
     9  	"github.com/mundipagg/boleto-api/config"
    10  	"github.com/mundipagg/boleto-api/env"
    11  	"github.com/mundipagg/boleto-api/healthcheck"
    12  	"github.com/mundipagg/boleto-api/log"
    13  	"github.com/mundipagg/boleto-api/mock"
    14  	"github.com/mundipagg/boleto-api/usermanagement"
    15  )
    16  
    17  //Params this struct contains all execution parameters to run application
    18  type Params struct {
    19  	DevMode    bool
    20  	MockMode   bool
    21  	DisableLog bool
    22  }
    23  
    24  //NewParams returns new Empty pointer to ExecutionParameters
    25  func NewParams() *Params {
    26  	return new(Params)
    27  }
    28  
    29  //Run starts boleto api Application
    30  func Run(params *Params) {
    31  	env.Config(params.DevMode, params.MockMode, params.DisableLog)
    32  
    33  	if config.Get().MockMode {
    34  		go mock.Run("9091")
    35  		time.Sleep(2 * time.Second)
    36  	}
    37  
    38  	log.Install()
    39  
    40  	start := time.Now()
    41  
    42  	healthcheck.ExecuteOnStartup()
    43  
    44  	installCertificates()
    45  
    46  	usermanagement.LoadUserCredentials()
    47  
    48  	props := getLoadDependenciesLogProp(start)
    49  	go log.CreateLog().InfoWithBasic("Load Dependencies with success", "Information", props)
    50  
    51  	api.InstallRestAPI()
    52  }
    53  
    54  func installCertificates() {
    55  	l := log.CreateLog()
    56  	l.Operation = "InstallCertificates"
    57  
    58  	if config.Get().MockMode {
    59  		certificate.LoadMockCertificates()
    60  		return
    61  	}
    62  
    63  	vaultCertificates := []string{config.Get().CertificateICPName, config.Get().CertificateSSLName, config.Get().CitibankCertificateSSLName, config.Get().SantanderCertificateSSLName}
    64  
    65  	err := certificate.InstanceStoreCertificatesFromAzureVault(config.Get().VaultName, vaultCertificates...)
    66  	if err == nil {
    67  		l.InfoWithBasic("Success in load certificates from azureVault", "LoadFromAzureVault", nil)
    68  	} else {
    69  		l.ErrorWithBasic("Error in load certificates from azureVault", "LoadFromAzureVault", err)
    70  		time.Sleep(10 * time.Second)
    71  		os.Exit(1)
    72  	}
    73  
    74  	blobCertificates := []string{config.Get().AzureStorageOpenBankSkName, config.Get().AzureStorageJPMorganPkName, config.Get().AzureStorageJPMorganCrtName, config.Get().AzureStorageJPMorganSignCrtName}
    75  
    76  	err = certificate.InstanceStoreCertificatesFromAzureBlob(blobCertificates...)
    77  	if err != nil {
    78  		l.ErrorWithBasic("Error loading open bank secret key from blob", "LoadFromAzureBlob", err)
    79  		time.Sleep(10 * time.Second)
    80  		os.Exit(1)
    81  	}
    82  
    83  }
    84  
    85  func getLoadDependenciesLogProp(start time.Time) map[string]interface{} {
    86  	props := make(map[string]interface{})
    87  	props["totalElapsedTimeInMilliseconds"] = time.Since(start).Milliseconds()
    88  	props["Operation"] = "RunApp"
    89  	return props
    90  }