mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-12-06 16:22:15 +00:00
docs: document every exported identifier and enforce it with godoclint
This commit is contained in:
@@ -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
|
||||
|
||||
3
cmd/doc.go
Normal file
3
cmd/doc.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Package gotenbergcmd runs Gotenberg with the modules its importer
|
||||
// registers, see [Run].
|
||||
package gotenbergcmd
|
||||
2
cmd/gotenberg-chromium/doc.go
Normal file
2
cmd/gotenberg-chromium/doc.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Command gotenberg-chromium runs Gotenberg without LibreOffice.
|
||||
package main
|
||||
2
cmd/gotenberg-libreoffice/doc.go
Normal file
2
cmd/gotenberg-libreoffice/doc.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Command gotenberg-libreoffice runs Gotenberg without Chromium.
|
||||
package main
|
||||
2
cmd/gotenberg/doc.go
Normal file
2
cmd/gotenberg/doc.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Command gotenberg runs Gotenberg with every standard module.
|
||||
package main
|
||||
@@ -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}
|
||||
}
|
||||
|
||||
@@ -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()))
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
72
pkg/modules/api/example_test.go
Normal file
72
pkg/modules/api/example_test.go
Normal file
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -46,6 +46,8 @@ 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.
|
||||
@@ -53,6 +55,8 @@ var (
|
||||
// GotenbergVariant selects a variant image, such as "chromium", tagged
|
||||
// <version>-<variant>. Empty selects the full image, tagged <version>.
|
||||
GotenbergVariant string
|
||||
// GotenbergContainerPlatform is the platform of the image, such as
|
||||
// "linux/amd64".
|
||||
GotenbergContainerPlatform string
|
||||
)
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user