Compare commits

..

1 Commits
5.0.2 ... 5.1.0

Author SHA1 Message Date
Julien Neuhart
02f1231e7d 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 <casey@monax.io>

* Configure listen port via environment variable (#77)

* skip logging when healthcheck url is called; closes #74

Signed-off-by: Casey Kuhlman <casey@monax.io>

* skip logging when healthcheck url is called; closes #74

Signed-off-by: Casey Kuhlman <casey@monax.io>

* adds the ability to establish the listen port via env var

Signed-off-by: Casey Kuhlman <casey@monax.io>

* minor refactoring of @compleatang work

* fixing typo
2019-06-03 15:05:09 +02:00
5 changed files with 93 additions and 18 deletions

View File

@@ -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).
> 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"`).

View File

@@ -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)

View File

@@ -255,6 +255,26 @@ Gotenberg API is available at <a href="http://localhost:3000">http://localhost:3
See the <a href="#timeout">timeout section</a>.</p>
</blockquote>
<h2 class="Heading"><a class="Anchor" aria-hidden="true" id="environment_variables.disable_logging_on_healthcheck" href="#environment_variables.disable_logging_on_healthcheck">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>Disable logging on healthcheck</h2>
<p>By default, the API will add a log entry when the <a href="#ping">healthcheck endpoint</a> is called.</p>
<p>You may turn off this logging so as to avoid unnecessary entries in your logs with the environment variable <code>DISABLE_HEALTHCHECK_LOGGING</code>.</p>
<p>This environment variable operates in the same manner as the <code>DISABLE_GOOGLE_CHROME</code> and <code>DISABLE_UNOCONV</code> variables operate in that it accepts the strings <code>&#34;0&#34;</code> or <code>&#34;1&#34;</code> as values.</p>
<h2 class="Heading"><a class="Anchor" aria-hidden="true" id="environment_variables.default_listen_port" href="#environment_variables.default_listen_port">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>Default listen port</h2>
<p>By default, the API will listen on port <code>3000</code>. 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.</p>
<p>You may customize this port location with the environment variable <code>DEFAULT_LISTEN_PORT</code>.</p>
<p>This environment variable accepts any string that can be turned into a port number (e.g., the string <code>&#34;0&#34;</code> up to the string <code>&#34;65535&#34;</code>).</p>
</div>
<div class="Page" id="html">

View File

@@ -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())

View File

@@ -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.