diff --git a/.golangci.yml b/.golangci.yml index b8dbddf3..1a3f9b85 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -32,10 +32,26 @@ linters: - sloglint - usetesting - gocritic + - godoclint - wastedassign - whitespace + settings: + godoclint: + default: basic + enable: + - require-doc + - require-pkg-doc + - start-with-name + options: + require-doc: + ignore-unexported: true exclusions: generated: lax + rules: + # A verbatim copy of otelhttp's generated internal package. + - path: pkg/gotenberg/semconv/ + linters: + - godoclint presets: - comments - common-false-positives diff --git a/cmd/doc.go b/cmd/doc.go new file mode 100644 index 00000000..10507c5f --- /dev/null +++ b/cmd/doc.go @@ -0,0 +1,3 @@ +// Package gotenbergcmd runs Gotenberg with the modules its importer +// registers, see [Run]. +package gotenbergcmd diff --git a/cmd/gotenberg-chromium/doc.go b/cmd/gotenberg-chromium/doc.go new file mode 100644 index 00000000..a3eae175 --- /dev/null +++ b/cmd/gotenberg-chromium/doc.go @@ -0,0 +1,2 @@ +// Command gotenberg-chromium runs Gotenberg without LibreOffice. +package main diff --git a/cmd/gotenberg-libreoffice/doc.go b/cmd/gotenberg-libreoffice/doc.go new file mode 100644 index 00000000..7dbcd2e7 --- /dev/null +++ b/cmd/gotenberg-libreoffice/doc.go @@ -0,0 +1,2 @@ +// Command gotenberg-libreoffice runs Gotenberg without Chromium. +package main diff --git a/cmd/gotenberg/doc.go b/cmd/gotenberg/doc.go new file mode 100644 index 00000000..a77343e8 --- /dev/null +++ b/cmd/gotenberg/doc.go @@ -0,0 +1,2 @@ +// Command gotenberg runs Gotenberg with every standard module. +package main diff --git a/pkg/gotenberg/internal/log/handler.go b/pkg/gotenberg/internal/log/handler.go index 0793d425..c23d28cf 100644 --- a/pkg/gotenberg/internal/log/handler.go +++ b/pkg/gotenberg/internal/log/handler.go @@ -12,6 +12,9 @@ type gotenbergHandler struct { loggerName string } +// NewGotenbergHandler wraps next to add the logger name to each record and, +// when prefix is set, to prefix the keys of its fields, the correlation and +// trace fields aside. func NewGotenbergHandler(next slog.Handler, prefix string) slog.Handler { return &gotenbergHandler{Handler: next, fieldsPrefix: prefix} } @@ -104,6 +107,8 @@ type multiHandler struct { handlers []slog.Handler } +// FanOut returns a [slog.Handler] that hands each record to every handler that +// accepts its level. func FanOut(handlers ...slog.Handler) slog.Handler { return &multiHandler{handlers: handlers} } @@ -150,6 +155,8 @@ type levelHandler struct { level slog.Level } +// LevelFilter returns a [slog.Handler] that drops the records of next below +// level. func LevelFilter(next slog.Handler, level slog.Level) slog.Handler { return &levelHandler{Handler: next, level: level} } diff --git a/pkg/gotenberg/internal/log/stdhandler.go b/pkg/gotenberg/internal/log/stdhandler.go index 8744ae5e..1817ebd5 100644 --- a/pkg/gotenberg/internal/log/stdhandler.go +++ b/pkg/gotenberg/internal/log/stdhandler.go @@ -49,12 +49,12 @@ func (h traceContextHandler) WithGroup(name string) slog.Handler { return traceContextHandler{Handler: h.Handler.WithGroup(name)} } -// NewStdHandler returns a [slog.Handler] instance for the standard output. // upperLevelCase is the value of the level-case setting that keeps the level // field uppercase in the standard output. It mirrors gotenberg.UpperLevelCase, // duplicated here because the internal log package cannot import gotenberg. const upperLevelCase = "upper" +// NewStdHandler returns a [slog.Handler] instance for the standard output. func NewStdHandler(level slog.Level, format string, fieldsPrefix string, enableGcpFields bool, levelCase string) (slog.Handler, error) { // #nosec: G115 isTerminal := term.IsTerminal(int(os.Stdout.Fd())) diff --git a/pkg/gotenberg/mocks.go b/pkg/gotenberg/mocks.go index 81c1d3c3..4e00c32c 100644 --- a/pkg/gotenberg/mocks.go +++ b/pkg/gotenberg/mocks.go @@ -11,6 +11,7 @@ type ModuleMock struct { DescriptorMock func() ModuleDescriptor } +// Descriptor calls DescriptorMock. func (mod *ModuleMock) Descriptor() ModuleDescriptor { return mod.DescriptorMock() } @@ -20,6 +21,7 @@ type ProvisionerMock struct { ProvisionMock func(*Context) error } +// Provision calls ProvisionMock. func (mod *ProvisionerMock) Provision(ctx *Context) error { return mod.ProvisionMock(ctx) } @@ -29,14 +31,17 @@ type ValidatorMock struct { ValidateMock func() error } +// Validate calls ValidateMock. func (mod *ValidatorMock) Validate() error { return mod.ValidateMock() } +// DebuggableMock is a mock for the [Debuggable] interface. type DebuggableMock struct { DebugMock func() map[string]any } +// Debug calls DebugMock. func (mod *DebuggableMock) Debug() map[string]any { return mod.DebugMock() } @@ -48,14 +53,17 @@ type ProcessMock struct { HealthyMock func(logger *slog.Logger) bool } +// Start calls StartMock. func (p *ProcessMock) Start(ctx context.Context, logger *slog.Logger) error { return p.StartMock(ctx, logger) } +// Stop calls StopMock. func (p *ProcessMock) Stop(logger *slog.Logger) error { return p.StopMock(logger) } +// Healthy calls HealthyMock. func (p *ProcessMock) Healthy(logger *slog.Logger) bool { return p.HealthyMock(logger) } @@ -68,18 +76,22 @@ type ProcessSupervisorMock struct { RunMock func(ctx context.Context, logger *slog.Logger, task func() error) error } +// Launch calls LaunchMock. func (s *ProcessSupervisorMock) Launch(ctx context.Context) error { return s.LaunchMock(ctx) } +// Shutdown calls ShutdownMock. func (s *ProcessSupervisorMock) Shutdown() error { return s.ShutdownMock() } +// Healthy calls HealthyMock. func (s *ProcessSupervisorMock) Healthy() bool { return s.HealthyMock() } +// Run calls RunMock. func (s *ProcessSupervisorMock) Run(ctx context.Context, logger *slog.Logger, task func() error) error { return s.RunMock(ctx, logger, task) } @@ -89,6 +101,7 @@ type MkdirAllMock struct { MkdirAllMock func(path string, perm os.FileMode) error } +// MkdirAll calls MkdirAllMock. func (mkdirAll *MkdirAllMock) MkdirAll(path string, perm os.FileMode) error { return mkdirAll.MkdirAllMock(path, perm) } @@ -98,6 +111,7 @@ type PathRenameMock struct { RenameMock func(oldpath, newpath string) error } +// Rename calls RenameMock. func (rename *PathRenameMock) Rename(oldpath, newpath string) error { return rename.RenameMock(oldpath, newpath) } diff --git a/pkg/gotenberg/sort.go b/pkg/gotenberg/sort.go index 5ff87805..f3dfebf2 100644 --- a/pkg/gotenberg/sort.go +++ b/pkg/gotenberg/sort.go @@ -25,14 +25,18 @@ const ( // https://github.com/gotenberg/gotenberg/issues/1287. type AlphanumericSort []string +// Len implements [sort.Interface]. func (s AlphanumericSort) Len() int { return len(s) } +// Swap implements [sort.Interface]. func (s AlphanumericSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] } +// Less implements [sort.Interface]. It orders by number first, see +// [AlphanumericSort]. func (s AlphanumericSort) Less(i, j int) bool { numI, restI, locI := extractNumber(s[i]) numJ, restJ, locJ := extractNumber(s[j]) diff --git a/pkg/gotenberg/telemetry.go b/pkg/gotenberg/telemetry.go index d9df0344..2f9b3728 100644 --- a/pkg/gotenberg/telemetry.go +++ b/pkg/gotenberg/telemetry.go @@ -16,12 +16,14 @@ import ( internalotel "github.com/gotenberg/gotenberg/v9/pkg/gotenberg/internal/otel" ) +// The formats of the standard logger, see [TelemetryConfig]. const ( AutoLoggingFormat = "auto" JsonLoggingFormat = "json" TextLoggingFormat = "text" ) +// The logging levels, see [TelemetryConfig]. const ( ErrorLoggingLevel = "error" WarnLoggingLevel = "warn" @@ -29,6 +31,7 @@ const ( DebugLoggingLevel = "debug" ) +// The cases of the level the standard logger writes, see [TelemetryConfig]. const ( LowerLevelCase = "lower" UpperLevelCase = "upper" diff --git a/pkg/modules/api/api.go b/pkg/modules/api/api.go index d74cee0c..8ed1188e 100644 --- a/pkg/modules/api/api.go +++ b/pkg/modules/api/api.go @@ -121,8 +121,12 @@ type MiddlewareProvider interface { type MiddlewareStack uint32 const ( + // DefaultStack runs the middleware after routing, on every route. DefaultStack MiddlewareStack = iota + // PreRouterStack runs the middleware before routing, on every request. PreRouterStack + // MultipartStack runs the middleware on the multipart/form-data routes + // only, after the [Context] is built. MultipartStack ) @@ -130,6 +134,7 @@ const ( // middlewares provided by the [MiddlewareProvider] modules in a stack. type MiddlewarePriority uint32 +// The priorities, from the last middleware of a stack to run to the first. const ( VeryLowPriority MiddlewarePriority = iota LowPriority diff --git a/pkg/modules/api/example_test.go b/pkg/modules/api/example_test.go new file mode 100644 index 00000000..985fc2c8 --- /dev/null +++ b/pkg/modules/api/example_test.go @@ -0,0 +1,72 @@ +package api_test + +import ( + "fmt" + "net/http" + + "github.com/labstack/echo/v5" + + "github.com/gotenberg/gotenberg/v9/pkg/gotenberg" + "github.com/gotenberg/gotenberg/v9/pkg/modules/api" +) + +// Stamp is a module adding the multipart/form-data route /forms/stamp. A +// custom build registers it in an init function, then imports its package, +// next to github.com/gotenberg/gotenberg/v9/pkg/standard, in a main calling +// github.com/gotenberg/gotenberg/v9/cmd.Run. +type Stamp struct{} + +// Descriptor returns the module's descriptor. +func (Stamp) Descriptor() gotenberg.ModuleDescriptor { + return gotenberg.ModuleDescriptor{ + ID: "stamp", + New: func() gotenberg.Module { return new(Stamp) }, + } +} + +// Routes returns the module's routes. +func (Stamp) Routes() ([]api.Route, error) { + return []api.Route{ + { + Method: http.MethodPost, + Path: "/forms/stamp", + IsMultipart: true, + Handler: func(c *echo.Context) error { + // The API builds the context of a multipart route: the + // uploaded files, the form fields, and the working directory. + ctx := c.Get("context").(*api.Context) + + var inputPaths []string + var text string + err := ctx.FormData(). + MandatoryPaths([]string{".pdf"}, &inputPaths). + String("text", &text). + Validate() + if err != nil { + return fmt.Errorf("validate form data: %w", err) + } + + // Write the results under ctx.GeneratePath(".pdf") and register + // them: the API sends them back, zipped if many, or to the + // webhook. + return ctx.AddOutputPaths(inputPaths...) + }, + }, + }, nil +} + +// This example writes a module adding a route to the API. The interface +// guards make the compiler check that the module is what the application +// looks for. +func Example_router() { + var ( + _ gotenberg.Module = (*Stamp)(nil) + _ api.Router = (*Stamp)(nil) + ) + + routes, _ := Stamp{}.Routes() + for _, route := range routes { + fmt.Println(route.Method, route.Path) + } + // Output: POST /forms/stamp +} diff --git a/pkg/modules/api/mocks.go b/pkg/modules/api/mocks.go index da282eae..5c1e0fe2 100644 --- a/pkg/modules/api/mocks.go +++ b/pkg/modules/api/mocks.go @@ -109,6 +109,7 @@ type RouterMock struct { RoutesMock func() ([]Route, error) } +// Routes calls RoutesMock. func (router *RouterMock) Routes() ([]Route, error) { return router.RoutesMock() } @@ -118,6 +119,7 @@ type MiddlewareProviderMock struct { MiddlewaresMock func() ([]Middleware, error) } +// Middlewares calls MiddlewaresMock. func (provider *MiddlewareProviderMock) Middlewares() ([]Middleware, error) { return provider.MiddlewaresMock() } @@ -128,10 +130,12 @@ type HealthCheckerMock struct { ReadyMock func(ctx context.Context) error } +// Checks calls ChecksMock. func (mod *HealthCheckerMock) Checks() ([]health.CheckerOption, error) { return mod.ChecksMock() } +// Ready calls ReadyMock. func (mod *HealthCheckerMock) Ready(ctx context.Context) error { return mod.ReadyMock(ctx) } diff --git a/pkg/modules/libreoffice/api/mocks.go b/pkg/modules/libreoffice/api/mocks.go index 628018dc..7f268e99 100644 --- a/pkg/modules/libreoffice/api/mocks.go +++ b/pkg/modules/libreoffice/api/mocks.go @@ -14,10 +14,12 @@ type ApiMock struct { ExtensionsMock func() []string } +// Pdf calls PdfMock. func (api *ApiMock) Pdf(ctx context.Context, logger *slog.Logger, inputPath, outputPath string, options Options) error { return api.PdfMock(ctx, logger, inputPath, outputPath, options) } +// Extensions calls ExtensionsMock. func (api *ApiMock) Extensions() []string { return api.ExtensionsMock() } @@ -27,6 +29,7 @@ type ProviderMock struct { LibreOfficeMock func() (Converter, error) } +// LibreOffice calls LibreOfficeMock. func (provider *ProviderMock) LibreOffice() (Converter, error) { return provider.LibreOfficeMock() } diff --git a/test/integration/scenario/containers.go b/test/integration/scenario/containers.go index e833fb75..f24a09d3 100644 --- a/test/integration/scenario/containers.go +++ b/test/integration/scenario/containers.go @@ -46,13 +46,17 @@ func PruneOrphanedNetworks(ctx context.Context) (int, error) { } var ( + // GotenbergDockerRepository is the repository of the image, such as + // "gotenberg/gotenberg". GotenbergDockerRepository string // GotenbergVersion is the version the image reports, such as in the // Gotenberg-Version header. GotenbergVersion string // GotenbergVariant selects a variant image, such as "chromium", tagged // -. Empty selects the full image, tagged . - GotenbergVariant string + GotenbergVariant string + // GotenbergContainerPlatform is the platform of the image, such as + // "linux/amd64". GotenbergContainerPlatform string ) diff --git a/test/integration/scenario/scenario.go b/test/integration/scenario/scenario.go index 56844976..8b43963d 100644 --- a/test/integration/scenario/scenario.go +++ b/test/integration/scenario/scenario.go @@ -51,6 +51,7 @@ func (s *scenario) reset(ctx context.Context) error { return nil } +// InitializeScenario registers the steps and hooks of a scenario. func InitializeScenario(ctx *godog.ScenarioContext) { s := &scenario{} ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {