Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
eb6a01b
Add config model
akkahshh24 May 12, 2025
4f2f17f
Add config package to load config from file
akkahshh24 May 12, 2025
bd4716f
Clean key names in yaml file
akkahshh24 May 12, 2025
74f40b9
Set log level using config
akkahshh24 May 12, 2025
ce7e9da
Pass config to espress service to create storage adapters
akkahshh24 May 12, 2025
1b52e61
Move config package to service/internal/config
akkahshh24 May 13, 2025
11100ec
Add config constants
akkahshh24 May 13, 2025
07ae542
Take config options from env
akkahshh24 May 13, 2025
f876bad
Pass config to template storage adapter
akkahshh24 May 13, 2025
c435d17
Add few keys in config file
akkahshh24 May 13, 2025
4182e13
Change config model
akkahshh24 May 13, 2025
fdb494e
Add config model
akkahshh24 May 12, 2025
0ea8db3
Add config package to load config from file
akkahshh24 May 12, 2025
80e754e
Clean key names in yaml file
akkahshh24 May 12, 2025
f3e7e75
Set log level using config
akkahshh24 May 12, 2025
8ad2a6b
Pass config to espress service to create storage adapters
akkahshh24 May 12, 2025
d876833
Move config package to service/internal/config
akkahshh24 May 13, 2025
5e79330
Add config constants
akkahshh24 May 13, 2025
51eae0a
Take config options from env
akkahshh24 May 13, 2025
051b9ef
Pass config to template storage adapter
akkahshh24 May 13, 2025
6161ada
Add few keys in config file
akkahshh24 May 13, 2025
46e1260
Change config model
akkahshh24 May 13, 2025
8e521b2
Fix db host and port
akkahshh24 May 22, 2025
1368b7e
Delete unused viperpkg
akkahshh24 May 22, 2025
20c1ebd
Move config model to internal/pkg/config
akkahshh24 May 22, 2025
284445a
Move config logic to internal/pkg/config
akkahshh24 May 22, 2025
f18c11d
Rectify config package import path
akkahshh24 May 22, 2025
8685410
Merge branch 'feature/config_service_dev' of https://github.com/akkah…
akkahshh24 Jun 1, 2025
5b99095
use CertificateConfig struct from lib
akkahshh24 Jun 3, 2025
35cbf9a
Add credential store to load signing credentials at startup
akkahshh24 Jun 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions service/configs/espressoconfig.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
app:
log_level: "debug"
server_port: 8081
enable_ui: true
rod_browser_bin: "/opt/homebrew/bin/chromium"

template_storage:
storage_type: "mysql"

Expand All @@ -9,37 +15,37 @@ browser:

workerpool:
worker_count: 6
worker_timeout: 310 # milliseconds
worker_timeout_ms: 310 # milliseconds

s3:
endpoint: "http://localstack:4566"
debug: false
region: "us-west-2"
forcePathStyle: true
uploaderConcurrency: 5
force_path_style: true
uploader_concurrency: 5
# 5MB chunks
uploaderPartSize: 5
downloaderConcurrency: 5
uploader_part_size_mb: 5
downloader_concurrency: 5
# 5MB chunks
downloaderPartSize: 5242880
retryMaxAttempts: 3
downloader_part_size_mb: 5242880
retry_max_attempts: 3
bucket: "local-bucket"
useCustomTransport: false
use_custom_transport: false

aws:
accessKeyID: "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
secretAccessKey: "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
sessionToken: ""
access_key_id: "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
secret_access_key: "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
session_token: ""

digital_certificates:
certificates:
cert1:
cert_filepath: "./inputfiles/certificates/cert.pem"
key_filepath: "./inputfiles/certificates/key_pkcs8_encrypted.pem"
cert_file_path: "./inputfiles/certificates/cert.pem"
key_file_path: "./inputfiles/certificates/key_pkcs8_encrypted.pem"
key_password: "test"
cert2:
cert_filepath: "./certificates/certificate2.pem"
key_filepath: "./certificates/pirvatekey2.key"
key_password: "password2"

mysql:
dsn: "pdf_user:pdf_password@tcp(mysql:3306)/pdf_templates?parseTime=true"
db:
host: "mysql"
port: 3306
username: "pdf_user"
password: "pdf_password"
database: "pdf_templates"
19 changes: 5 additions & 14 deletions service/controller/pdf_generation/pdf_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/Zomato/espresso/service/internal/pkg/httppkg"
"github.com/Zomato/espresso/service/internal/service/generateDoc"
svcUtils "github.com/Zomato/espresso/service/utils"
"github.com/spf13/viper"
)

func (s *EspressoService) GeneratePDF(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -56,7 +55,7 @@ func (s *EspressoService) GeneratePDF(w http.ResponseWriter, r *http.Request) {
generatePdfReq.SignParams = req.SignParams
}

err = generateDoc.GeneratePDF(ctx, generatePdfReq, s.TemplateStorageAdapter, s.FileStorageAdapter)
err = generateDoc.GeneratePDF(ctx, generatePdfReq, s.TemplateStorageAdapter, s.FileStorageAdapter, s.CredentialStore)
if err != nil {
svcUtils.Logger.Error(ctx, "error in generating pdf :: %v", err, nil)
httppkg.RespondWithError(w, "Failed to generate PDF: "+err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -143,7 +142,7 @@ func (s *EspressoService) GeneratePDFStream(w http.ResponseWriter, r *http.Reque
if pdfReq.SignPdf {
generatePdfReq.SignParams = &generateDoc.SignParams{
SignPdf: true,
CertConfigKey: "digital_certificates.cert1", // certificate details are stored in config file
CertConfigKey: "cert1", // certificate details are stored in config file
}
}

Expand All @@ -155,16 +154,8 @@ func (s *EspressoService) GeneratePDFStream(w http.ResponseWriter, r *http.Reque
httppkg.RespondWithError(w, "Failed to get file storage adapter: "+err.Error(), http.StatusExpectationFailed)
return
}
templateStorageAdapter, err := templatestore.TemplateStorageAdapterFactory(&templatestore.StorageConfig{
StorageType: "mysql",
MysqlDSN: viper.GetString("mysql.dsn"),
})
if err != nil {
svcUtils.Logger.Error(ctx, "error in getting file storage adapter :: %v", err, nil)
httppkg.RespondWithError(w, "Failed to get file storage adapter: "+err.Error(), http.StatusExpectationFailed)
return
}
err = generateDoc.GeneratePDF(ctx, generatePdfReq, &templateStorageAdapter, &fileStorageAdapter)

err = generateDoc.GeneratePDF(ctx, generatePdfReq, s.TemplateStorageAdapter, &fileStorageAdapter, s.CredentialStore)
if err != nil {
svcUtils.Logger.Error(ctx, "error in generating pdf stream:: %v", err, nil)
httppkg.RespondWithError(w, "Failed to generate PDF stream: "+err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -243,7 +234,7 @@ func (s *EspressoService) SignPDF(w http.ResponseWriter, r *http.Request) {
httppkg.RespondWithError(w, err.Error(), http.StatusBadRequest)
return
}
err := generateDoc.SignPDF(ctx, signPDFDto, s.FileStorageAdapter)
err := generateDoc.SignPDF(ctx, signPDFDto, s.FileStorageAdapter, s.CredentialStore)
if err != nil {
svcUtils.Logger.Error(ctx, "error in signing pdf :: : %v", err, nil)
httppkg.RespondWithError(w, "Failed to sign PDF: "+err.Error(), http.StatusInternalServerError)
Expand Down
65 changes: 33 additions & 32 deletions service/controller/pdf_generation/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,63 @@ import (
"fmt"
"log"
"net/http"
"os"

"github.com/Zomato/espresso/lib/s3"
"github.com/Zomato/espresso/lib/certmanager"
"github.com/Zomato/espresso/lib/templatestore"
"github.com/spf13/viper"
"github.com/Zomato/espresso/service/internal/pkg/config"
)

type EspressoService struct {
TemplateStorageAdapter *templatestore.StorageAdapter
FileStorageAdapter *templatestore.StorageAdapter
CredentialStore *certmanager.CredentialStore
}

func NewEspressoService() (*EspressoService, error) {
templateStorageType := viper.GetString("template_storage.storage_type")
if os.Getenv("ENABLE_UI") == "true" && templateStorageType != templatestore.StorageAdapterTypeMySQL {
func NewEspressoService(config config.Config) (*EspressoService, error) {
templateStorageType := config.TemplateStorageConfig.StorageType

if config.AppConfig.EnableUI && templateStorageType != templatestore.StorageAdapterTypeMySQL {
return nil, fmt.Errorf("UI requires MySQL as template storage adapter, got: %s", templateStorageType)
}

mySqlDsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true",
config.DBConfig.Username,
config.DBConfig.Password,
config.DBConfig.Host,
config.DBConfig.Port,
config.DBConfig.Database,
)

templateStorageAdapter, err := templatestore.TemplateStorageAdapterFactory(&templatestore.StorageConfig{
StorageType: templateStorageType,
// for s3 storage only
S3Config: &s3.Config{
Endpoint: viper.GetString("s3.endpoint"),
Region: viper.GetString("s3.region"),
Bucket: viper.GetString("s3.bucket"),
Debug: viper.GetBool("s3.debug"),
ForcePathStyle: viper.GetBool("s3.forcePathStyle"),
UploaderConcurrency: viper.GetInt("s3.uploaderConcurrency"),
UploaderPartSize: viper.GetInt64("s3.uploaderPartSize"),
DownloaderConcurrency: viper.GetInt("s3.downloaderConcurrency"),
DownloaderPartSize: viper.GetInt64("s3.downloaderPartSize"),
RetryMaxAttempts: viper.GetInt("s3.retryMaxAttempts"),
UseCustomTransport: viper.GetBool("s3.useCustomTransport"),
},
// for s3 storage only
AwsCredConfig: &s3.AwsCredConfig{
AccessKeyID: viper.GetString("aws.accessKeyID"),
SecretAccessKey: viper.GetString("aws.secretAccessKey"),
SessionToken: viper.GetString("aws.sessionToken"),
},
MysqlDSN: viper.GetString("mysql.dsn"), // for mysql adapter
StorageType: templateStorageType,
S3Config: &config.S3Config, // for s3 storage only
AwsCredConfig: &config.AWSConfig, // for s3 storage only
MysqlDSN: mySqlDsn, // for mysql adapter
})
if err != nil {
return nil, err
}

fileStorageAdapter, err := templatestore.TemplateStorageAdapterFactory(&templatestore.StorageConfig{
StorageType: viper.GetString("file_storage.storage_type"),
StorageType: config.FileStorageConfig.StorageType,
})
if err != nil {
return nil, err
}

return &EspressoService{TemplateStorageAdapter: &templateStorageAdapter, FileStorageAdapter: &fileStorageAdapter}, nil
credentialStore, err := certmanager.NewCredentialStore(config.CertConfig)
if err != nil {
return nil, err
}

return &EspressoService{
TemplateStorageAdapter: &templateStorageAdapter,
FileStorageAdapter: &fileStorageAdapter,
CredentialStore: credentialStore,
}, nil
}
func Register(mux *http.ServeMux) {
espressoService, err := NewEspressoService()
func Register(mux *http.ServeMux, config config.Config) {
espressoService, err := NewEspressoService(config)
if err != nil {
log.Fatalf("Failed to initialize PDF service: %v", err)
}
Expand Down
43 changes: 43 additions & 0 deletions service/internal/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package config

import (
"fmt"

"github.com/spf13/viper"
)

func Load() (Config, error) {
viper.AutomaticEnv()

viper.SetDefault(CONFIG_FILE_NAME, "espressoconfig")
viper.SetDefault(CONFIG_FILE_TYPE, "yaml")
viper.SetDefault(CONFIG_FILE_PATH, "/app/espresso/configs")

viper.SetConfigName(viper.GetString(CONFIG_FILE_NAME)) // File name without extension
viper.SetConfigType(viper.GetString(CONFIG_FILE_TYPE)) // File type

// Search paths relative to where the binary runs in container
viper.AddConfigPath(CONFIG_FILE_PATH) // Main config path in container
viper.AddConfigPath("../../configs") // For local development
viper.AddConfigPath("./configs") // Fallback path for local development

err := viper.ReadInConfig()
if err != nil {
return Config{}, err
}

var config Config
err = viper.Unmarshal(&config)
if err != nil {
return Config{}, err
}

// TODO: Why are these fields set in the Dockerfile and not in the config.yaml file?
config.AppConfig.EnableUI = viper.GetBool(ENABLE_UI)
config.AppConfig.RodBrowserBin = viper.GetString(ROD_BROWSER_BIN)
if config.AppConfig.RodBrowserBin == "" {
return Config{}, fmt.Errorf("environment variable %s not set", ROD_BROWSER_BIN)
}

return config, nil
}
10 changes: 10 additions & 0 deletions service/internal/pkg/config/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

const (
CONFIG_FILE_NAME = "CONFIG_FILE_NAME"
CONFIG_FILE_TYPE = "CONFIG_FILE_TYPE"
CONFIG_FILE_PATH = "CONFIG_FILE_PATH"

ENABLE_UI = "ENABLE_UI"
ROD_BROWSER_BIN = "ROD_BROWSER_BIN"
)
46 changes: 46 additions & 0 deletions service/internal/pkg/config/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

import (
"github.com/Zomato/espresso/lib/certmanager"
"github.com/Zomato/espresso/lib/s3"
)

type Config struct {
AppConfig AppConfig `mapstructure:"app"`
TemplateStorageConfig StorageConfig `mapstructure:"template_storage"`
FileStorageConfig StorageConfig `mapstructure:"file_storage"`
BrowserConfig BrowserConfig `mapstructure:"browser"`
WorkerPoolConfig WorkerPoolConfig `mapstructure:"workerpool"`
S3Config s3.Config `mapstructure:"s3"`
AWSConfig s3.AwsCredConfig `mapstructure:"aws"`
CertConfig map[string]certmanager.CertificateConfig `mapstructure:"certificates"`
DBConfig DBConfig `mapstructure:"db"`
}

type AppConfig struct {
LogLevel string `mapstructure:"log_level"`
ServerPort int `mapstructure:"server_port"`
EnableUI bool `mapstructure:"enable_ui"`
RodBrowserBin string `mapstructure:"rod_browser_bin"`
}

type StorageConfig struct {
StorageType string `mapstructure:"storage_type"`
}

type BrowserConfig struct {
TabPool int `mapstructure:"tab_pool"`
}

type WorkerPoolConfig struct {
WorkerCount int `mapstructure:"worker_count"`
WorkerTimeoutMs int `mapstructure:"worker_timeout_ms"`
}

type DBConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Database string `mapstructure:"database"`
}
20 changes: 0 additions & 20 deletions service/internal/pkg/viperpkg/viperpkg.go

This file was deleted.

Loading