This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from safing/fix/connect-issues
Improve tooling and safeguards for connecting issues
- Loading branch information
Showing
11 changed files
with
166 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package captain | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/safing/portbase/api" | ||
"github.com/safing/portbase/database" | ||
"github.com/safing/portbase/database/query" | ||
"github.com/safing/portbase/modules" | ||
) | ||
|
||
func registerAPIEndpoints() error { | ||
if err := api.RegisterEndpoint(api.Endpoint{ | ||
Path: `spn/reinit`, | ||
Write: api.PermitAdmin, | ||
// BelongsTo: module, // Do not attach to module, as this must run outside of the module. | ||
ActionFunc: handleReInit, | ||
Name: "Re-initialize SPN", | ||
Description: "Stops the SPN, resets all caches and starts it again. The SPN account and settings are not changed.", | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func handleReInit(ar *api.Request) (msg string, err error) { | ||
// Disable module and check | ||
changed := module.Disable() | ||
if !changed { | ||
return "", errors.New("can only re-initialize when the SPN is enabled") | ||
} | ||
|
||
// Run module manager. | ||
err = modules.ManageModules() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to stop SPN: %w", err) | ||
} | ||
|
||
// Delete SPN cache. | ||
db := database.NewInterface(&database.Options{ | ||
Local: true, | ||
Internal: true, | ||
}) | ||
deletedRecords, err := db.Purge(ar.Context(), query.New("cache:spn/")) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to delete SPN cache: %w", err) | ||
} | ||
|
||
// Enable module. | ||
module.Enable() | ||
|
||
// Run module manager. | ||
err = modules.ManageModules() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to start SPN after cache reset: %w", err) | ||
} | ||
|
||
return fmt.Sprintf( | ||
"Completed SPN re-initialization and deleted %d cache records in the process.", | ||
deletedRecords, | ||
), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.