Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Debug admin endpoint to clear caches #2940

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/gotosocial/action/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ var Start action.GTSAction = func(ctx context.Context) error {

var (
authModule = api.NewAuth(dbService, processor, idp, routerSession, sessionName) // auth/oauth paths
clientModule = api.NewClient(dbService, processor) // api client endpoints
clientModule = api.NewClient(&state, processor) // api client endpoints
metricsModule = api.NewMetrics() // Metrics endpoints
healthModule = api.NewHealth(dbService.Ready) // Health check endpoints
fileserverModule = api.NewFileserver(processor) // fileserver endpoints
Expand Down
2 changes: 1 addition & 1 deletion cmd/gotosocial/action/testrig/testrig.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ var Start action.GTSAction = func(ctx context.Context) error {

var (
authModule = api.NewAuth(state.DB, processor, idp, routerSession, sessionName) // auth/oauth paths
clientModule = api.NewClient(state.DB, processor) // api client endpoints
clientModule = api.NewClient(&state, processor) // api client endpoints
metricsModule = api.NewMetrics() // Metrics endpoints
healthModule = api.NewHealth(state.DB.Ready) // Health check endpoints
fileserverModule = api.NewFileserver(processor) // fileserver endpoints
Expand Down
25 changes: 25 additions & 0 deletions docs/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4494,6 +4494,31 @@ paths:
summary: Perform a GET to the specified ActivityPub URL and return detailed debugging information.
tags:
- debug
/api/v1/admin/debug/caches/clear:
post:
description: Only enabled / exposed if GoToSocial was built and is running with flag DEBUG=1.
operationId: debugClearCaches
produces:
- application/json
responses:
"200":
description: All good baby!
"400":
description: bad request
"401":
description: unauthorized
"404":
description: not found
"406":
description: not acceptable
"500":
description: internal server error
security:
- OAuth2 Bearer:
- admin
summary: Sweep/clear all in-memory caches.
tags:
- debug
/api/v1/admin/domain_allows:
get:
operationId: domainAllowsGet
Expand Down
7 changes: 4 additions & 3 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/middleware"
"github.com/superseriousbusiness/gotosocial/internal/processing"
"github.com/superseriousbusiness/gotosocial/internal/router"
"github.com/superseriousbusiness/gotosocial/internal/state"
)

type Client struct {
Expand Down Expand Up @@ -127,13 +128,13 @@ func (c *Client) Route(r *router.Router, m ...gin.HandlerFunc) {
c.user.Route(h)
}

func NewClient(db db.DB, p *processing.Processor) *Client {
func NewClient(state *state.State, p *processing.Processor) *Client {
return &Client{
processor: p,
db: db,
db: state.DB,

accounts: accounts.New(p),
admin: admin.New(p),
admin: admin.New(state, p),
apps: apps.New(p),
blocks: blocks.New(p),
bookmarks: bookmarks.New(p),
Expand Down
7 changes: 6 additions & 1 deletion internal/api/client/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"codeberg.org/gruf/go-debug"
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/processing"
"github.com/superseriousbusiness/gotosocial/internal/state"
)

const (
Expand Down Expand Up @@ -56,6 +57,7 @@ const (
InstanceRulesPathWithID = InstanceRulesPath + "/:" + IDKey
DebugPath = BasePath + "/debug"
DebugAPUrlPath = DebugPath + "/apurl"
DebugClearCachesPath = DebugPath + "/caches/clear"

IDKey = "id"
FilterQueryKey = "filter"
Expand All @@ -73,11 +75,13 @@ const (

type Module struct {
processor *processing.Processor
state *state.State
}

func New(processor *processing.Processor) *Module {
func New(state *state.State, processor *processing.Processor) *Module {
return &Module{
processor: processor,
state: state,
}
}

Expand Down Expand Up @@ -145,5 +149,6 @@ func (m *Module) Route(attachHandler func(method string, path string, f ...gin.H
// debug stuff
if debug.DEBUG {
attachHandler(http.MethodGet, DebugAPUrlPath, m.DebugAPUrlHandler)
attachHandler(http.MethodPost, DebugClearCachesPath, m.DebugClearCachesHandler)
}
}
2 changes: 1 addition & 1 deletion internal/api/client/admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (suite *AdminStandardTestSuite) SetupTest() {
suite.sentEmails = make(map[string]string)
suite.emailSender = testrig.NewEmailSender("../../../../web/template/", suite.sentEmails)
suite.processor = testrig.NewTestProcessor(&suite.state, suite.federator, suite.emailSender, suite.mediaManager)
suite.adminModule = admin.New(suite.processor)
suite.adminModule = admin.New(&suite.state, suite.processor)
testrig.StandardDBSetup(suite.db, nil)
testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media")
}
Expand Down
32 changes: 32 additions & 0 deletions internal/api/client/admin/debug_off.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,35 @@ import (
// '500':
// description: internal server error
func (m *Module) DebugAPUrlHandler(c *gin.Context) {}

// DebugClearCachesHandler swagger:operation POST /api/v1/admin/debug/caches/clear debugClearCaches
//
// Sweep/clear all in-memory caches.
//
// Only enabled / exposed if GoToSocial was built and is running with flag DEBUG=1.
//
// ---
// tags:
// - debug
//
// produces:
// - application/json
//
// security:
// - OAuth2 Bearer:
// - admin
//
// responses:
// '200':
// description: All good baby!
// '400':
// description: bad request
// '401':
// description: unauthorized
// '404':
// description: not found
// '406':
// description: not acceptable
// '500':
// description: internal server error
func (m *Module) DebugClearCachesHandler(c *gin.Context) {}
24 changes: 24 additions & 0 deletions internal/api/client/admin/debug_on.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,27 @@ func (m *Module) DebugAPUrlHandler(c *gin.Context) {

c.JSON(http.StatusOK, resp)
}

func (m *Module) DebugClearCachesHandler(c *gin.Context) {
authed, err := oauth.Authed(c, true, true, true, true)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
return
}

if !*authed.User.Admin {
err := fmt.Errorf("user %s not an admin", authed.User.ID)
apiutil.ErrorHandler(c, gtserror.NewErrorForbidden(err, err.Error()), m.processor.InstanceGetV1)
return
}

if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
}

// Sweep all caches down to 0 (empty).
m.state.Caches.Sweep(0)

c.JSON(http.StatusOK, gin.H{"status": "OK"})
}