godoc for config package + typo in godoc of app package

This commit is contained in:
Julien Neuhart
2018-03-30 13:09:22 +02:00
parent 4fef665078
commit 4a93528f7e
2 changed files with 50 additions and 5 deletions

View File

@@ -13,7 +13,7 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
// App gathers all data required to start the application. // App gathers all data required for the application.
type App struct { type App struct {
// version is the application version as defined in the main package. // version is the application version as defined in the main package.
version string version string

View File

@@ -1,3 +1,9 @@
/*
Package config contains all the logic allowing us to instantiate the application's configuration.
The application's configuration is loaded from a YAML file named gotenberg.yml.
It should be located where the user starts the application from the CLI.
*/
package config package config
import ( import (
@@ -9,27 +15,47 @@ import (
) )
type ( type (
// AppConfig gathers all data required to instantiate the application.
AppConfig struct { AppConfig struct {
// Port is the port which the application will listen to.
Port string Port string
// Logs contains the logging configuration.
Logs struct { Logs struct {
Level logrus.Level // Level is the level of messages which will be logged.
Level logrus.Level
// Formatter defines the logging format when a TTY is not attached.
Formatter logrus.Formatter Formatter logrus.Formatter
} }
// CommandsConfig is... an instance of CommandsConfig.
CommandsConfig *CommandsConfig CommandsConfig *CommandsConfig
} }
// CommandsConfig gathers all commands' configurations as defined
// by the user in the gotenberg.yml
CommandsConfig struct { CommandsConfig struct {
HTML *CommandConfig // HTML is the command's configuration for converting
// an HTML file to PDF.
HTML *CommandConfig
// Office is the command's configuration for converting
// an Office document to PDF.
Office *CommandConfig Office *CommandConfig
Merge *CommandConfig // Merge is the command's configuration for merging
// multiple PDF files into one PDF file.
Merge *CommandConfig
} }
// CommandConfig is a command's configuration.
CommandConfig struct { CommandConfig struct {
Timeout int // Timeout is the duration in seconds after which the command's process will be killed
// if it does not finish before.
Timeout int
// Template is the data-driven template of the command.
Template *template.Template Template *template.Template
} }
) )
// NewAppConfig instantiates the application's configuration.
// If something bad happens here, the application should not start.
func NewAppConfig() (*AppConfig, error) { func NewAppConfig() (*AppConfig, error) {
fileConfig, err := loadFileConfig() fileConfig, err := loadFileConfig()
if err != nil { if err != nil {
@@ -82,6 +108,7 @@ func NewAppConfig() (*AppConfig, error) {
return c, nil return c, nil
} }
// fileConfig gathers all data coming from the configuration file gotenberg.yml.
type fileConfig struct { type fileConfig struct {
Port string `yaml:"port"` Port string `yaml:"port"`
Logs struct { Logs struct {
@@ -107,6 +134,8 @@ type fileConfig struct {
// configurationFilePath is our default configuration file to parse. // configurationFilePath is our default configuration file to parse.
const configurationFilePath = "gotenberg.yml" const configurationFilePath = "gotenberg.yml"
// loadFileConfig instantiates a fileConfig instance by loading
// the configuration file gotenberg.yml.
func loadFileConfig() (*fileConfig, error) { func loadFileConfig() (*fileConfig, error) {
c := &fileConfig{} c := &fileConfig{}
@@ -122,6 +151,8 @@ func loadFileConfig() (*fileConfig, error) {
return c, nil return c, nil
} }
// levels associates logging levels as defined in the configuration file gotenberg.yml
// with its counterpart from the logrus library.
var levels = map[string]logrus.Level{ var levels = map[string]logrus.Level{
"DEBUG": logrus.DebugLevel, "DEBUG": logrus.DebugLevel,
"INFO": logrus.InfoLevel, "INFO": logrus.InfoLevel,
@@ -131,12 +162,17 @@ var levels = map[string]logrus.Level{
"PANIC": logrus.PanicLevel, "PANIC": logrus.PanicLevel,
} }
// wrongLoggingLevelError is raised when the logging level defined by the user
// is not applicable.
type wrongLoggingLevelError struct{} type wrongLoggingLevelError struct{}
func (e *wrongLoggingLevelError) Error() string { func (e *wrongLoggingLevelError) Error() string {
return "Accepted values for logging level: DEBUG, INFO, WARN, ERROR, FATAL, PANIC" return "Accepted values for logging level: DEBUG, INFO, WARN, ERROR, FATAL, PANIC"
} }
// getLoggingLevelFromFileConfig returns a logrus level if a matching was found
// with the one defined by ther user.
// If no match, throws a wrongLoggingLevelError.
func getLoggingLevelFromFileConfig(c *fileConfig) (logrus.Level, error) { func getLoggingLevelFromFileConfig(c *fileConfig) (logrus.Level, error) {
l, ok := levels[c.Logs.Level] l, ok := levels[c.Logs.Level]
if !ok { if !ok {
@@ -146,17 +182,24 @@ func getLoggingLevelFromFileConfig(c *fileConfig) (logrus.Level, error) {
return l, nil return l, nil
} }
// levels associates logging formats as defined in the configuration file gotenberg.yml
// with its counterpart from the logrus library.
var formatters = map[string]logrus.Formatter{ var formatters = map[string]logrus.Formatter{
"text": &logrus.TextFormatter{}, "text": &logrus.TextFormatter{},
"json": &logrus.JSONFormatter{}, "json": &logrus.JSONFormatter{},
} }
// wrongLoggingFormatError is raised when the logging format defined by the user
// is not applicable.
type wrongLoggingFormatError struct{} type wrongLoggingFormatError struct{}
func (e *wrongLoggingFormatError) Error() string { func (e *wrongLoggingFormatError) Error() string {
return "Accepted value for logging format: text, json" return "Accepted value for logging format: text, json"
} }
// getLoggingLevelFromFileConfig returns a logrus Formatter if a matching was found
// with the format defined by ther user.
// If no match, throws a wrongLoggingFormatError.
func getLoggingFormatterFromFileConfig(c *fileConfig) (logrus.Formatter, error) { func getLoggingFormatterFromFileConfig(c *fileConfig) (logrus.Formatter, error) {
f, ok := formatters[c.Logs.Format] f, ok := formatters[c.Logs.Format]
if !ok { if !ok {
@@ -166,6 +209,8 @@ func getLoggingFormatterFromFileConfig(c *fileConfig) (logrus.Formatter, error)
return f, nil return f, nil
} }
// getCommandTemplate is a simple helper for parsing a command template as defined by the user.
// If the user gives us a wrong template, throws an error.
func getCommandTemplate(command string, commandName string) (*template.Template, error) { func getCommandTemplate(command string, commandName string) (*template.Template, error) {
t, err := template.New(commandName).Parse(command) t, err := template.New(commandName).Parse(command)
if err != nil { if err != nil {