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
}