wip refactoring: better logging and error systems

This commit is contained in:
Julien Neuhart
2019-07-07 17:45:07 +02:00
parent a4aa8dafac
commit c8f7ea934c
36 changed files with 1346 additions and 1096 deletions

View File

@@ -0,0 +1,3 @@
// Package handler contains all
// the endpoint methods of the API.
package handler

View 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
}

View 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)
}

View 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)
}

View 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)
}

View 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)
}

View 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
}

View 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)
}