mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 08:32:16 +01:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76f17cb6d7 | ||
|
|
7149c421cf | ||
|
|
353c599767 |
@@ -1,8 +1,8 @@
|
||||
<p align="center">
|
||||
<img src="https://user-images.githubusercontent.com/8983173/130322857-185831e2-f041-46eb-a17f-0a69d066c4e5.png" alt="Gotenberg Logo" width="150" height="150" />
|
||||
<h3 align="center">Gotenberg</h3>
|
||||
<p align="center">A Docker-powered stateless API for PDF files</p>
|
||||
<p align="center"><a href="https://gotenberg.dev/docs/about">Documentation</a> · 🔥 <a href="https://gotenberg.dev/docs/get-started/live-demo">Live Demo</a></p>
|
||||
<h3 align="center">Gotenberg</h3>
|
||||
<p align="center">A Docker-powered stateless API for PDF files</p>
|
||||
<p align="center"><a href="https://gotenberg.dev/docs/about">Documentation</a> · 🔥 <a href="https://gotenberg.dev/docs/get-started/live-demo">Live Demo</a></p>
|
||||
</p>
|
||||
|
||||
---
|
||||
|
||||
@@ -33,7 +33,7 @@ var Version = "snapshot"
|
||||
func Run() {
|
||||
fmt.Printf(banner, Version)
|
||||
|
||||
// Creates the roo` FlagSet and adds the modules flags to it.
|
||||
// Create the root FlagSet and adds the modules flags to it.
|
||||
fs := flag.NewFlagSet("gotenberg", flag.ExitOnError)
|
||||
fs.Duration("gotenberg-graceful-shutdown-duration", time.Duration(30)*time.Second, "Set the graceful shutdown duration")
|
||||
|
||||
@@ -46,14 +46,14 @@ func Run() {
|
||||
|
||||
fmt.Printf("[SYSTEM] modules: %s\n", modsInfo)
|
||||
|
||||
// Parses the flags...
|
||||
// Parse the flags...
|
||||
err := fs.Parse(os.Args[1:])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// ...and creates a wrapper around those.
|
||||
// ...and create a wrapper around those.
|
||||
parsedFlags := gotenberg.ParsedFlags{FlagSet: fs}
|
||||
|
||||
// Get the graceful shutdown duration.
|
||||
@@ -61,7 +61,7 @@ func Run() {
|
||||
|
||||
ctx := gotenberg.NewContext(parsedFlags, descriptors)
|
||||
|
||||
// Starts application modules.
|
||||
// Start application modules.
|
||||
apps, err := ctx.Modules(new(gotenberg.App))
|
||||
if err != nil {
|
||||
fmt.Printf("[FATAL] %s\n", err)
|
||||
@@ -87,7 +87,23 @@ func Run() {
|
||||
|
||||
fmt.Printf("[SYSTEM] %s: %s\n", id, startupMessage)
|
||||
}(a.(gotenberg.App))
|
||||
}
|
||||
|
||||
// Get modules that want to print system messages.
|
||||
sysLoggers, err := ctx.Modules(new(gotenberg.SystemLogger))
|
||||
if err != nil {
|
||||
fmt.Printf("[FATAL] %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for _, l := range sysLoggers {
|
||||
go func(logger gotenberg.SystemLogger) {
|
||||
id := logger.(gotenberg.Module).Descriptor().ID
|
||||
|
||||
for _, message := range logger.SystemMessages() {
|
||||
fmt.Printf("[SYSTEM] %s: %s\n", id, message)
|
||||
}
|
||||
}(l.(gotenberg.SystemLogger))
|
||||
}
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
|
||||
@@ -69,6 +69,12 @@ type App interface {
|
||||
Stop(ctx context.Context) error
|
||||
}
|
||||
|
||||
// SystemLogger is a module interface for modules which want to display
|
||||
// messages on startup.
|
||||
type SystemLogger interface {
|
||||
SystemMessages() []string
|
||||
}
|
||||
|
||||
// MustRegisterModule registers a module.
|
||||
//
|
||||
// To register a module, create an init() method in the module main go file:
|
||||
|
||||
@@ -3,6 +3,7 @@ package pdfengines
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/modules/api"
|
||||
@@ -36,7 +37,7 @@ func (PDFEngines) Descriptor() gotenberg.ModuleDescriptor {
|
||||
ID: "pdfengines",
|
||||
FlagSet: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("pdfengines", flag.ExitOnError)
|
||||
fs.StringSlice("pdfengines-engines", make([]string, 0), "Set the PDF engines - all by default")
|
||||
fs.StringSlice("pdfengines-engines", make([]string, 0), "Set the PDF engines and their order - all by default")
|
||||
fs.Bool("pdfengines-disable-routes", false, "Disable the routes")
|
||||
|
||||
return fs
|
||||
@@ -118,14 +119,25 @@ func (mod PDFEngines) Validate() error {
|
||||
return fmt.Errorf("non-existing PDF engine(s): %s - available PDF engine(s): %s", nonExistingEngines, availableEngines)
|
||||
}
|
||||
|
||||
// SystemMessages returns one message with the selected gotenberg.PDFEngine
|
||||
// modules.
|
||||
func (mod PDFEngines) SystemMessages() []string {
|
||||
return []string{
|
||||
strings.Join(mod.names[:], " "),
|
||||
}
|
||||
}
|
||||
|
||||
// PDFEngine returns a gotenberg.PDFEngine.
|
||||
func (mod PDFEngines) PDFEngine() (gotenberg.PDFEngine, error) {
|
||||
engines := make([]gotenberg.PDFEngine, len(mod.engines))
|
||||
engines := make([]gotenberg.PDFEngine, len(mod.names))
|
||||
|
||||
i := 0
|
||||
for _, engine := range mod.engines {
|
||||
engines[i] = engine
|
||||
i++
|
||||
for i, name := range mod.names {
|
||||
for _, engine := range mod.engines {
|
||||
if name == engine.(gotenberg.Module).Descriptor().ID {
|
||||
engines[i] = engine
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newMultiPDFEngines(engines...), nil
|
||||
@@ -155,6 +167,7 @@ var (
|
||||
_ gotenberg.Module = (*PDFEngines)(nil)
|
||||
_ gotenberg.Provisioner = (*PDFEngines)(nil)
|
||||
_ gotenberg.Validator = (*PDFEngines)(nil)
|
||||
_ gotenberg.SystemLogger = (*PDFEngines)(nil)
|
||||
_ gotenberg.PDFEngineProvider = (*PDFEngines)(nil)
|
||||
_ api.Router = (*PDFEngines)(nil)
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
|
||||
@@ -252,11 +253,40 @@ func TestPDFEngine_Validate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFEngines_SystemMessages(t *testing.T) {
|
||||
mod := new(PDFEngines)
|
||||
mod.names = []string{"foo", "bar"}
|
||||
|
||||
messages := mod.SystemMessages()
|
||||
if len(messages) != 1 {
|
||||
t.Errorf("expected one and only one message but got %d", len(messages))
|
||||
}
|
||||
|
||||
expect := strings.Join(mod.names[:], " ")
|
||||
if messages[0] != expect {
|
||||
t.Errorf("expected message '%s' but got '%s'", expect, messages[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFEngine_PDFEngine(t *testing.T) {
|
||||
mod := new(PDFEngines)
|
||||
mod.engines = []gotenberg.PDFEngine{
|
||||
struct{ ProtoPDFEngine }{},
|
||||
}
|
||||
mod.names = []string{"foo", "bar"}
|
||||
mod.engines = func() []gotenberg.PDFEngine {
|
||||
engine1 := struct{ ProtoPDFEngine }{}
|
||||
engine1.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module { return engine1 }}
|
||||
}
|
||||
|
||||
engine2 := struct{ ProtoPDFEngine }{}
|
||||
engine2.descriptor = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "bar", New: func() gotenberg.Module { return engine2 }}
|
||||
}
|
||||
|
||||
return []gotenberg.PDFEngine{
|
||||
engine1,
|
||||
engine2,
|
||||
}
|
||||
}()
|
||||
|
||||
_, err := mod.PDFEngine()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user