mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-12 02:12:15 +01:00
wip refactoring: better logging and error systems
This commit is contained in:
3
internal/app/api/pkg/handler/doc.go
Normal file
3
internal/app/api/pkg/handler/doc.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Package handler contains all
|
||||
// the endpoint methods of the API.
|
||||
package handler
|
||||
151
internal/app/api/pkg/handler/handler.go
Normal file
151
internal/app/api/pkg/handler/handler.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
|
||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/resource"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/random"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
)
|
||||
|
||||
const (
|
||||
// PingEndpoint is the route for healthcheck.
|
||||
PingEndpoint = "/ping"
|
||||
// MergeEndpoint is the route for merging PDF files.
|
||||
MergeEndpoint = "/merge"
|
||||
// ConvertGroupEndpoint is the route of the group
|
||||
// in charge of converting files to PDF.
|
||||
ConvertGroupEndpoint = "/convert"
|
||||
// HTMLEndpoint is the route for converting
|
||||
// HTML to PDF.
|
||||
HTMLEndpoint = "/html"
|
||||
// URLEndpoint is the route for converting
|
||||
// a URL to PDF.
|
||||
URLEndpoint = "/url"
|
||||
// MarkdownEndpoint is the route for converting
|
||||
// Markdown to PDF.
|
||||
MarkdownEndpoint = "/markdown"
|
||||
// OfficeEndpoint is the route for converting
|
||||
// Office files to PDF.
|
||||
OfficeEndpoint = "/office"
|
||||
)
|
||||
|
||||
func convert(ctx *context.Context, p printer.Printer) error {
|
||||
const (
|
||||
op = "convert"
|
||||
debugOp = "handler.convert"
|
||||
)
|
||||
r := ctx.Resource()
|
||||
logger := ctx.StandardLogger()
|
||||
baseFilename := random.Get()
|
||||
filename := fmt.Sprintf("%s.pdf", baseFilename)
|
||||
fpath := fmt.Sprintf("%s/%s", r.DirPath(), filename)
|
||||
// if no webhook URL given, run conversion
|
||||
// and directly return the resulting PDF file
|
||||
// or an error.
|
||||
if !r.Has(resource.WebhookURLFormField) {
|
||||
logger.DebugfOp(debugOp, "no '%s' found, converting synchronously", resource.WebhookURLFormField)
|
||||
if err := convertSync(filename, fpath, ctx, p); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// as a webhook URL has been given, we
|
||||
// run the following lines in a goroutine so that
|
||||
// it doesn't block.
|
||||
logger.DebugfOp(debugOp, "'%s' found, converting asynchronously", resource.WebhookURLFormField)
|
||||
return convertAsync(filename, fpath, ctx, p)
|
||||
}
|
||||
|
||||
func convertSync(filename, fpath string, ctx *context.Context, p printer.Printer) error {
|
||||
const (
|
||||
op = "convertSync"
|
||||
debugOp = "handler.convertSync"
|
||||
)
|
||||
r := ctx.Resource()
|
||||
logger := ctx.StandardLogger()
|
||||
if err := p.Print(fpath); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
if !r.Has(resource.ResultFilenameFormField) {
|
||||
logger.DebugfOp(
|
||||
debugOp,
|
||||
"no '%s' found, using generated filename '%s'",
|
||||
resource.ResultFilenameFormField,
|
||||
filename,
|
||||
)
|
||||
if err := ctx.Attachment(fpath, filename); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
logger.DebugfOp(
|
||||
debugOp,
|
||||
"'%s' found, so not using generated filename",
|
||||
resource.ResultFilenameFormField,
|
||||
)
|
||||
filename, err := r.Get(resource.ResultFilenameFormField)
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
if err := ctx.Attachment(fpath, filename); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertAsync(filename, fpath string, ctx *context.Context, p printer.Printer) error {
|
||||
const (
|
||||
op = "convertAsync"
|
||||
debugOp = "handler.convertAsync"
|
||||
)
|
||||
r := ctx.Resource()
|
||||
logger := ctx.StandardLogger()
|
||||
go func() {
|
||||
defer r.Close() // nolint: errcheck
|
||||
if err := p.Print(fpath); err != nil {
|
||||
logger.ErrorOp(
|
||||
op,
|
||||
&standarderror.Error{Op: op, Err: err},
|
||||
)
|
||||
return
|
||||
}
|
||||
f, err := os.Open(fpath)
|
||||
if err != nil {
|
||||
logger.ErrorOp(
|
||||
op,
|
||||
&standarderror.Error{Op: op, Err: err},
|
||||
)
|
||||
return
|
||||
}
|
||||
defer f.Close() // nolint: errcheck
|
||||
webhookURL, err := r.Get(resource.WebhookURLFormField)
|
||||
if err != nil {
|
||||
logger.ErrorOp(
|
||||
op,
|
||||
&standarderror.Error{Op: op, Err: err},
|
||||
)
|
||||
return
|
||||
}
|
||||
logger.DebugfOp(
|
||||
debugOp,
|
||||
"sending result file '%s' to '%s'",
|
||||
filename,
|
||||
webhookURL,
|
||||
)
|
||||
resp, err := http.Post(webhookURL, "application/pdf", f) /* #nosec */
|
||||
if err != nil {
|
||||
logger.ErrorOp(
|
||||
op,
|
||||
&standarderror.Error{Op: op, Err: err},
|
||||
)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close() // nolint: errcheck
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
24
internal/app/api/pkg/handler/html.go
Normal file
24
internal/app/api/pkg/handler/html.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
|
||||
)
|
||||
|
||||
// HTML is the endpoint for converting
|
||||
// HTML to PDF.
|
||||
func HTML(c echo.Context) error {
|
||||
ctx := context.MustCastFromEchoContext(c)
|
||||
r := ctx.Resource()
|
||||
opts, err := r.ChromePrinterOptions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fpath, err := r.Fpath("index.html")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := printer.NewHTML(fpath, opts)
|
||||
return convert(ctx, p)
|
||||
}
|
||||
27
internal/app/api/pkg/handler/markdown.go
Normal file
27
internal/app/api/pkg/handler/markdown.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
|
||||
)
|
||||
|
||||
// Markdown is the endpoint for converting
|
||||
// Markdown to PDF.
|
||||
func Markdown(c echo.Context) error {
|
||||
ctx := context.MustCastFromEchoContext(c)
|
||||
r := ctx.Resource()
|
||||
opts, err := r.ChromePrinterOptions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fpath, err := r.Fpath("index.html")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p, err := printer.NewMarkdown(fpath, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return convert(ctx, p)
|
||||
}
|
||||
24
internal/app/api/pkg/handler/merge.go
Normal file
24
internal/app/api/pkg/handler/merge.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
|
||||
)
|
||||
|
||||
// Merge is the endpoint for
|
||||
// merging PDF files.
|
||||
func Merge(c echo.Context) error {
|
||||
ctx := context.MustCastFromEchoContext(c)
|
||||
r := ctx.Resource()
|
||||
opts, err := r.MergePrinterOptions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fpaths, err := r.Fpaths(".pdf")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := printer.NewMerge(fpaths, opts)
|
||||
return convert(ctx, p)
|
||||
}
|
||||
37
internal/app/api/pkg/handler/office.go
Normal file
37
internal/app/api/pkg/handler/office.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
|
||||
)
|
||||
|
||||
// Office is the endpoint for converting
|
||||
// Office files to PDF.
|
||||
func Office(c echo.Context) error {
|
||||
ctx := context.MustCastFromEchoContext(c)
|
||||
r := ctx.Resource()
|
||||
opts, err := r.OfficePrinterOptions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fpaths, err := r.Fpaths(
|
||||
".txt",
|
||||
".rtf",
|
||||
".fodt",
|
||||
".doc",
|
||||
".docx",
|
||||
".odt",
|
||||
".xls",
|
||||
".xlsx",
|
||||
".ods",
|
||||
".ppt",
|
||||
".pptx",
|
||||
".odp",
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := printer.NewOffice(fpaths, opts)
|
||||
return convert(ctx, p)
|
||||
}
|
||||
10
internal/app/api/pkg/handler/ping.go
Normal file
10
internal/app/api/pkg/handler/ping.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Ping is the endpoint for healthcheck.
|
||||
func Ping(c echo.Context) error {
|
||||
return nil
|
||||
}
|
||||
25
internal/app/api/pkg/handler/url.go
Normal file
25
internal/app/api/pkg/handler/url.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
|
||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/resource"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
|
||||
)
|
||||
|
||||
// URL is the endpoint for converting
|
||||
// a URL to PDF.
|
||||
func URL(c echo.Context) error {
|
||||
ctx := context.MustCastFromEchoContext(c)
|
||||
r := ctx.Resource()
|
||||
opts, err := r.ChromePrinterOptions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
remoteURL, err := r.Get(resource.RemoteURLFormField)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := printer.NewURL(remoteURL, opts)
|
||||
return convert(ctx, p)
|
||||
}
|
||||
Reference in New Issue
Block a user