mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-17 12:42:16 +01:00
new feature: users are now able to provide more file types to convert by adding entries in their configuration file (#3)
* new feature: users are now able to provide more file types to convert by adding entries in their configuration file * removing README blueprint: was not working correctly * improving code coverage of config package
This commit is contained in:
94
app/config/parser.go
Normal file
94
app/config/parser.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// ParseFile instantiates the application's configuration using the given YAML file.
|
||||
func ParseFile(configurationFilePath string) error {
|
||||
fileConfig, err := readFile(configurationFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
WithPort(fileConfig.Port)
|
||||
|
||||
if err := WithLogsLevel(fileConfig.Logs.Level); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WithLogsFormatter(fileConfig.Logs.Formatter); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// handles merge command first...
|
||||
cmd, err := NewCommand(fileConfig.Commands.Merge.Template, fileConfig.Commands.Merge.Timeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
WithCommand(".pdf", cmd)
|
||||
|
||||
// ...then conversion commands!
|
||||
for _, command := range fileConfig.Commands.Conversions {
|
||||
cmd, err := NewCommand(command.Template, command.Timeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, ext := range command.Extensions {
|
||||
if err := WithCommand(ext, cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type (
|
||||
// fileConfig gathers all data coming from the configuration file gotenberg.yml.
|
||||
fileConfig struct {
|
||||
Port string `yaml:"port"`
|
||||
Logs struct {
|
||||
Level string `yaml:"level"`
|
||||
Formatter string `yaml:"formatter"`
|
||||
} `yaml:"logs"`
|
||||
Commands struct {
|
||||
Merge *mergeCommand `yaml:"merge"`
|
||||
Conversions []*conversionCommand `yaml:"conversions,omitempty"`
|
||||
} `yaml:"commands"`
|
||||
}
|
||||
|
||||
// mergeCommand gathers all data regarding the... merge command.
|
||||
mergeCommand struct {
|
||||
Template string `yaml:"template"`
|
||||
Timeout int `yaml:"timeout"`
|
||||
}
|
||||
|
||||
// conversionCommand gathers all data regarding a conversion command.
|
||||
conversionCommand struct {
|
||||
Template string `yaml:"template"`
|
||||
Timeout int `yaml:"timeout"`
|
||||
Extensions []string `yaml:"extensions"`
|
||||
}
|
||||
)
|
||||
|
||||
// readFile instantiates a fileConfig instance by reading
|
||||
// the given YAML file.
|
||||
func readFile(configurationFilePath string) (*fileConfig, error) {
|
||||
c := &fileConfig{}
|
||||
|
||||
data, err := ioutil.ReadFile(configurationFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(data, &c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
Reference in New Issue
Block a user