Add ready API endpoint and temporarily "backport" to ping

This commit is contained in:
Daniel 2024-04-23 10:46:50 +02:00
parent 20a72df439
commit b15a4aac46
2 changed files with 30 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package api
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@ -11,6 +12,7 @@ import (
"time" "time"
"github.com/safing/portbase/info" "github.com/safing/portbase/info"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/utils/debug" "github.com/safing/portbase/utils/debug"
) )
@ -25,6 +27,16 @@ func registerDebugEndpoints() error {
return err return err
} }
if err := RegisterEndpoint(Endpoint{
Path: "ready",
Read: PermitAnyone,
ActionFunc: ready,
Name: "Ready",
Description: "Check if Portmaster has completed starting and is ready.",
}); err != nil {
return err
}
if err := RegisterEndpoint(Endpoint{ if err := RegisterEndpoint(Endpoint{
Path: "debug/stack", Path: "debug/stack",
Read: PermitAnyone, Read: PermitAnyone,
@ -118,9 +130,22 @@ You can easily view this data in your browser with this command (with Go install
// ping responds with pong. // ping responds with pong.
func ping(ar *Request) (msg string, err error) { func ping(ar *Request) (msg string, err error) {
// TODO: Remove upgrade to "ready" when all UI components have transitioned.
if modules.IsStarting() || modules.IsShuttingDown() {
return "", ErrorWithStatus(errors.New("portmaster is not ready"), http.StatusTooEarly)
}
return "Pong.", nil return "Pong.", nil
} }
// ready checks if Portmaster has completed starting.
func ready(ar *Request) (msg string, err error) {
if modules.IsStarting() || modules.IsShuttingDown() {
return "", ErrorWithStatus(errors.New("portmaster is not ready"), http.StatusTooEarly)
}
return "Portmaster is ready.", nil
}
// getStack returns the current goroutine stack. // getStack returns the current goroutine stack.
func getStack(_ *Request) (data []byte, err error) { func getStack(_ *Request) (data []byte, err error) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}

View file

@ -24,6 +24,11 @@ func SetGlobalPrepFn(fn func() error) {
} }
} }
// IsStarting returns whether the initial global start is still in progress.
func IsStarting() bool {
return !initialStartCompleted.IsSet()
}
// Start starts all modules in the correct order. In case of an error, it will automatically shutdown again. // Start starts all modules in the correct order. In case of an error, it will automatically shutdown again.
func Start() error { func Start() error {
if !modulesLocked.SetToIf(false, true) { if !modulesLocked.SetToIf(false, true) {