diff options
| author | James Elliott <james-d-elliott@users.noreply.github.com> | 2025-03-09 01:53:44 +1100 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-09 01:53:44 +1100 | 
| commit | 9241731a4dd5592b4a02b5352c903b4d06b6f4ab (patch) | |
| tree | 5184b98751912a261ff70fd8721b9cd4f1c98f1e /internal/service/signal.go | |
| parent | bbcb38ab9ff35e69d5d52a71ab56346749f5e8b1 (diff) | |
feat(embed): make authelia embedable (#8841)
This adds a highly experimental option for developers looking to embed Authelia within another go binary.
Closes #5803
Signed-off-by: James Elliott <james-d-elliott@users.noreply.github.com>
Diffstat (limited to 'internal/service/signal.go')
| -rw-r--r-- | internal/service/signal.go | 81 | 
1 files changed, 81 insertions, 0 deletions
diff --git a/internal/service/signal.go b/internal/service/signal.go new file mode 100644 index 000000000..b6445a792 --- /dev/null +++ b/internal/service/signal.go @@ -0,0 +1,81 @@ +package service + +import ( +	"os" +	"os/signal" +	"syscall" + +	"github.com/sirupsen/logrus" + +	"github.com/authelia/authelia/v4/internal/logging" +) + +func ProvisionLoggingSignal(ctx Context) (service Provider, err error) { +	config := ctx.GetConfiguration() + +	if config == nil || len(config.Log.FilePath) == 0 { +		return nil, nil +	} + +	return &Signal{ +		name:    "log-reload", +		signals: []os.Signal{syscall.SIGHUP}, +		action:  logging.Reopen, +		log:     ctx.GetLogger().WithFields(map[string]any{logFieldService: serviceTypeSignal, serviceTypeSignal: "log-reload"}), +	}, nil +} + +// Signal is a Service which performs actions on signals. +type Signal struct { +	name    string +	signals []os.Signal +	action  func() (err error) +	log     *logrus.Entry + +	notify chan os.Signal +	quit   chan struct{} +} + +// ServiceType returns the service type for this service, which is always 'server'. +func (service *Signal) ServiceType() string { +	return serviceTypeSignal +} + +// ServiceName returns the individual name for this service. +func (service *Signal) ServiceName() string { +	return service.name +} + +// Run the ServerService. +func (service *Signal) Run() (err error) { +	service.quit = make(chan struct{}) + +	service.notify = make(chan os.Signal, 1) + +	signal.Notify(service.notify, service.signals...) + +	for { +		select { +		case s := <-service.notify: +			if err = service.action(); err != nil { +				service.log.WithError(err).Error("Error occurred executing service action.") +			} else { +				service.log.WithFields(map[string]any{"signal-received": s.String()}).Debug("Successfully executed service action.") +			} +		case <-service.quit: +			return +		} +	} +} + +// Shutdown the ServerService. +func (service *Signal) Shutdown() { +	signal.Stop(service.notify) + +	service.quit <- struct{}{} +} + +// Log returns the *logrus.Entry of the ServerService. +func (service *Signal) Log() *logrus.Entry { +	return service.log +}  | 
