Update API endpoints and handlers

This commit is contained in:
Daniel
2020-12-31 00:00:48 +01:00
parent 3d2cbdd4e6
commit 72c7592cdd
7 changed files with 182 additions and 72 deletions

43
core/api.go Normal file
View File

@@ -0,0 +1,43 @@
package core
import (
"github.com/safing/portbase/api"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"github.com/safing/portmaster/updates"
)
func registerActions() error {
if err := api.RegisterEndpoint(api.Endpoint{
Path: "core/shutdown",
Read: api.PermitSelf,
ActionFn: shutdown,
}); err != nil {
return err
}
if err := api.RegisterEndpoint(api.Endpoint{
Path: "core/restart",
Read: api.PermitSelf,
ActionFn: restart,
}); err != nil {
return err
}
return nil
}
// shutdown shuts the Portmaster down.
func shutdown(_ *api.Request) (msg string, err error) {
log.Warning("core: user requested shutdown via action")
// Do not use a worker, as this would block itself here.
go modules.Shutdown() //nolint:errcheck
return "shutdown initiated", nil
}
// restart restarts the Portmaster.
func restart(_ *api.Request) (msg string, err error) {
log.Info("core: user requested restart via action")
updates.RestartNow()
return "restart initiated", nil
}

View File

@@ -55,6 +55,10 @@ func start() error {
return err
}
if err := registerActions(); err != nil {
return err
}
registerLogCleaner()
return nil

View File

@@ -1,3 +1,5 @@
// DEPRECATED: remove in v0.7
package core
import (
@@ -19,12 +21,12 @@ func registerEvents() {
}
func registerEventHooks() error {
err := module.RegisterEventHook("core", eventShutdown, "execute shutdown", shutdown)
err := module.RegisterEventHook("core", eventShutdown, "execute shutdown", shutdownHook)
if err != nil {
return err
}
err = module.RegisterEventHook("core", eventRestart, "execute shutdown", restart)
err = module.RegisterEventHook("core", eventRestart, "execute shutdown", restartHook)
if err != nil {
return err
}
@@ -32,16 +34,16 @@ func registerEventHooks() error {
return nil
}
// shutdown shuts the Portmaster down.
func shutdown(ctx context.Context, _ interface{}) error {
// shutdownHook shuts the Portmaster down.
func shutdownHook(ctx context.Context, _ interface{}) error {
log.Warning("core: user requested shutdown")
// Do not use a worker, as this would block itself here.
go modules.Shutdown() //nolint:errcheck
return nil
}
// restart restarts the Portmaster.
func restart(ctx context.Context, data interface{}) error {
// restartHook restarts the Portmaster.
func restartHook(ctx context.Context, data interface{}) error {
log.Info("core: user requested restart")
updates.RestartNow()
return nil