From 02f1231e7deae2f111d5003059fe5c85b0a4a6b2 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Mon, 3 Jun 2019 15:05:09 +0200 Subject: [PATCH] New environment variables : DISABLE_HEALTHCHECK_LOGGING and DEFAULT_LISTEN_PORT (#78) * skip logging when healthcheck url is called; closes #74 (#75) Signed-off-by: Casey Kuhlman * Configure listen port via environment variable (#77) * skip logging when healthcheck url is called; closes #74 Signed-off-by: Casey Kuhlman * skip logging when healthcheck url is called; closes #74 Signed-off-by: Casey Kuhlman * adds the ability to establish the listen port via env var Signed-off-by: Casey Kuhlman * minor refactoring of @compleatang work * fixing typo --- .../docs/content/03-environment-variables.md | 19 ++++++++-- cmd/gotenberg/main.go | 35 +++++++++++++++---- docs/index.html | 20 +++++++++++ internal/app/api/api.go | 23 +++++++----- internal/app/api/middleware.go | 14 ++++++++ 5 files changed, 93 insertions(+), 18 deletions(-) diff --git a/build/docs/content/03-environment-variables.md b/build/docs/content/03-environment-variables.md index 1a915e04..3f081d56 100644 --- a/build/docs/content/03-environment-variables.md +++ b/build/docs/content/03-environment-variables.md @@ -13,7 +13,6 @@ It takes the strings `"0"` or `"1"` as value. > If Google Chrome is disabled, the following conversions will **not** be available anymore: > [HTML](#html), [URL](#url) and [Markdown](#markdown) - ## Disable LibreOffice (unoconv) You may also disable LibreOffice (unoconv) with `DISABLE_UNOCONV`. @@ -30,4 +29,20 @@ You may customize this timeout thanks to the environment variable `DEFAULT_WAIT_ It takes a string representation of a float as value (e.g `"2.5"` for 2.5 seconds). > The default timeout may also be overridden per request thanks to the form field `waitTimeout`. -> See the [timeout section](#timeout). \ No newline at end of file +> See the [timeout section](#timeout). + +## Disable logging on healthcheck + +By default, the API will add a log entry when the [healthcheck endpoint](#ping) is called. + +You may turn off this logging so as to avoid unnecessary entries in your logs with the environment variable `DISABLE_HEALTHCHECK_LOGGING`. + +This environment variable operates in the same manner as the `DISABLE_GOOGLE_CHROME` and `DISABLE_UNOCONV` variables operate in that it accepts the strings `"0"` or `"1"` as values. + +## Default listen port + +By default, the API will listen on port `3000`. For most use cases this is perfectly fine, but at times there may be cases where you need to change this due to port conflicts. + +You may customize this port location with the environment variable `DEFAULT_LISTEN_PORT`. + +This environment variable accepts any string that can be turned into a port number (e.g., the string `"0"` up to the string `"65535"`). \ No newline at end of file diff --git a/cmd/gotenberg/main.go b/cmd/gotenberg/main.go index e5e19324..0d7d7cea 100644 --- a/cmd/gotenberg/main.go +++ b/cmd/gotenberg/main.go @@ -20,9 +20,11 @@ import ( var version = "snapshot" const ( - defaultWaitTimeoutEnvVar = "DEFAULT_WAIT_TIMEOUT" - disableGoogleChromeEnvVar = "DISABLE_GOOGLE_CHROME" - disableUnoconvEnvVar = "DISABLE_UNOCONV" + defaultWaitTimeoutEnvVar = "DEFAULT_WAIT_TIMEOUT" + defaultListenPortEnvVar = "DEFAULT_LISTEN_PORT" + disableGoogleChromeEnvVar = "DISABLE_GOOGLE_CHROME" + disableUnoconvEnvVar = "DISABLE_UNOCONV" + disableHealthcheckLoggingEnvVar = "DISABLE_HEALTHCHECK_LOGGING" ) func mustParseEnvVar() *api.Options { @@ -35,6 +37,18 @@ func mustParseEnvVar() *api.Options { } opts.DefaultWaitTimeout = defaultWaitTimeout } + if v, ok := os.LookupEnv(defaultListenPortEnvVar); ok { + defaultListener, err := strconv.ParseUint(os.Getenv(defaultListenPortEnvVar), 10, 64) + if err != nil { + notify.ErrPrint(fmt.Errorf("%s: wrong value: want uint got %v", defaultListenPortEnvVar, err)) + os.Exit(1) + } + if defaultListener > 65535 { + notify.ErrPrint(fmt.Errorf("%s: wrong value: want uint < 65535 got %v", defaultListenPortEnvVar, defaultListener)) + os.Exit(1) + } + opts.DefaultListenPort = v + } if v, ok := os.LookupEnv(disableGoogleChromeEnvVar); ok { if v != "1" && v != "0" { notify.ErrPrint(fmt.Errorf("%s: wrong value: want \"0\" or \"1\" got %v", disableGoogleChromeEnvVar, v)) @@ -49,6 +63,13 @@ func mustParseEnvVar() *api.Options { } opts.EnableUnoconvEndpoints = v != "1" } + if v, ok := os.LookupEnv(disableHealthcheckLoggingEnvVar); ok { + if v != "1" && v != "0" { + notify.ErrPrint(fmt.Errorf("%s: wrong value: want \"0\" or \"1\" got %v", disableHealthcheckLoggingEnvVar, v)) + os.Exit(1) + } + opts.EnableHealthcheckLogging = v != "1" + } return opts } @@ -70,9 +91,9 @@ func mustStartProcesses(opts *api.Options) []pm2.Process { return processes } -func mustStartAPI(srv *echo.Echo) { - notify.Print("http server started on port 3000") - if err := srv.Start(":3000"); err != nil { +func mustStartAPI(srv *echo.Echo, port string) { + notify.Printf("http server started on port %v", port) + if err := srv.Start(fmt.Sprintf(":%v", port)); err != nil { if err != http.ErrServerClosed { notify.ErrPrint(err) os.Exit(1) @@ -110,7 +131,7 @@ func main() { processes := mustStartProcesses(opts) // run our API in a goroutine so that it doesn't block.s go func() { - mustStartAPI(srv) + mustStartAPI(srv, opts.DefaultListenPort) }() quit := make(chan os.Signal, 1) // we'll accept graceful shutdowns when quit via SIGINT (Ctrl+C) diff --git a/docs/index.html b/docs/index.html index 114bbd93..bcf2fd81 100755 --- a/docs/index.html +++ b/docs/index.html @@ -255,6 +255,26 @@ Gotenberg API is available at http://localhost:3 See the timeout section.

+

Disable logging on healthcheck

+ +

By default, the API will add a log entry when the healthcheck endpoint is called.

+ +

You may turn off this logging so as to avoid unnecessary entries in your logs with the environment variable DISABLE_HEALTHCHECK_LOGGING.

+ +

This environment variable operates in the same manner as the DISABLE_GOOGLE_CHROME and DISABLE_UNOCONV variables operate in that it accepts the strings "0" or "1" as values.

+ +

Default listen port

+ +

By default, the API will listen on port 3000. For most use cases this is perfectly fine, but at times there may be cases where you need to change this due to port conflicts.

+ +

You may customize this port location with the environment variable DEFAULT_LISTEN_PORT.

+ +

This environment variable accepts any string that can be turned into a port number (e.g., the string "0" up to the string "65535").

+
diff --git a/internal/app/api/api.go b/internal/app/api/api.go index 8015a071..1edd6063 100644 --- a/internal/app/api/api.go +++ b/internal/app/api/api.go @@ -2,23 +2,28 @@ package api import ( "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" ) +const pingEndpoint = "/ping" + // Options allows to customize the behaviour // of the API. type Options struct { - DefaultWaitTimeout float64 - EnableChromeEndpoints bool - EnableUnoconvEndpoints bool + DefaultWaitTimeout float64 + DefaultListenPort string + EnableChromeEndpoints bool + EnableUnoconvEndpoints bool + EnableHealthcheckLogging bool } // DefaultOptions returns default options. func DefaultOptions() *Options { return &Options{ - DefaultWaitTimeout: 10, - EnableChromeEndpoints: true, - EnableUnoconvEndpoints: true, + DefaultWaitTimeout: 10, + DefaultListenPort: "3000", + EnableChromeEndpoints: true, + EnableUnoconvEndpoints: true, + EnableHealthcheckLogging: true, } } @@ -27,8 +32,8 @@ func New(opts *Options) *echo.Echo { api := echo.New() api.HideBanner = true api.HidePort = true - api.Use(middleware.Logger()) - api.GET("/ping", func(c echo.Context) error { return nil }) + api.Use(handleLogging(opts.EnableHealthcheckLogging)) + api.GET(pingEndpoint, func(c echo.Context) error { return nil }) g := api.Group("/convert") g.Use(handleContext(opts)) g.Use(handleError()) diff --git a/internal/app/api/middleware.go b/internal/app/api/middleware.go index bdf92fcb..a3329f6d 100644 --- a/internal/app/api/middleware.go +++ b/internal/app/api/middleware.go @@ -6,8 +6,22 @@ import ( "strings" "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" ) +func handleLogging(enableHealthcheckLogging bool) echo.MiddlewareFunc { + if enableHealthcheckLogging { + // default logging middleware. + return middleware.Logger() + } + // middleware for skipping logging when the ping endpoint is called. + return middleware.LoggerWithConfig(middleware.LoggerConfig{ + Skipper: func(c echo.Context) bool { + return c.Request().URL.Path == pingEndpoint + }, + }) +} + func handleContext(opts *Options) echo.MiddlewareFunc { // middleware for extending default context with our // custom constext.