Skip to content

Commit

Permalink
Merge pull request #95 from wesen/bug/fix-middleware-for-json-handler
Browse files Browse the repository at this point in the history
🚑 Fix missing middleware layer for JSON endpoint
  • Loading branch information
wesen authored Jan 17, 2024
2 parents b4b12a5 + 338b545 commit f0e138d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
9 changes: 4 additions & 5 deletions pkg/glazed/handlers/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (h *QueryHandler) Handle(c *gin.Context, writer io.Writer) error {
description := h.cmd.Description()
parsedLayers := layers.NewParsedLayers()

middlewares_ := append(h.middlewares,
middlewares_ := append(
h.middlewares,
middlewares2.UpdateFromQueryParameters(c, parameters.WithParseStepSource("query")),
middlewares.SetFromDefaults(),
)
Expand Down Expand Up @@ -119,11 +120,9 @@ func (h *QueryHandler) Handle(c *gin.Context, writer io.Writer) error {

func CreateJSONQueryHandler(
cmd cmds.Command,
middlewares ...middlewares.Middleware,
options ...QueryHandlerOption,
) gin.HandlerFunc {
handler := NewQueryHandler(cmd,
WithMiddlewares(middlewares...),
)
handler := NewQueryHandler(cmd, options...)
return func(c *gin.Context) {
err := handler.Handle(c, c.Writer)
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions pkg/handlers/command-dir/command-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (cd *CommandDirHandler) Serve(server *parka.Server, path string) error {

path = strings.TrimSuffix(path, "/")

middlewares_ := cd.ParameterFilter.ComputeMiddlewares(cd.Stream)
server.Router.GET(path+"/data/*path", func(c *gin.Context) {
commandPath := c.Param("path")
commandPath = strings.TrimPrefix(commandPath, "/")
Expand All @@ -203,13 +204,14 @@ func (cd *CommandDirHandler) Serve(server *parka.Server, path string) error {

switch v := command.(type) {
case cmds.GlazeCommand:
json.CreateJSONQueryHandler(v)(c)
json.CreateJSONQueryHandler(v, json.WithMiddlewares(middlewares_...))(c)
default:
text.CreateQueryHandler(v)(c)
}
})

// Redirect Route
// Redirect Route for legacy
// TODO(manuel, 2024-01-17) This really should be moved to some kind of config file option
server.Router.GET(path+"/sqleton/*path", func(c *gin.Context) {
commandPath := c.Param("path")
rawQuery := c.Request.URL.RawQuery
Expand All @@ -220,7 +222,6 @@ func (cd *CommandDirHandler) Serve(server *parka.Server, path string) error {
c.Redirect(301, newURL)
})

middlewares := cd.ParameterFilter.ComputeMiddlewares(cd.Stream)
server.Router.GET(path+"/text/*path", func(c *gin.Context) {
commandPath := c.Param("path")
commandPath = strings.TrimPrefix(commandPath, "/")
Expand All @@ -230,7 +231,7 @@ func (cd *CommandDirHandler) Serve(server *parka.Server, path string) error {
return
}

text.CreateQueryHandler(command, middlewares...)(c)
text.CreateQueryHandler(command, middlewares_...)(c)
})

server.Router.GET(path+"/streaming/*path", func(c *gin.Context) {
Expand All @@ -242,7 +243,7 @@ func (cd *CommandDirHandler) Serve(server *parka.Server, path string) error {
return
}

sse.CreateQueryHandler(command, middlewares...)(c)
sse.CreateQueryHandler(command, middlewares_...)(c)
})

server.Router.GET(path+"/datatables/*path",
Expand All @@ -259,7 +260,7 @@ func (cd *CommandDirHandler) Serve(server *parka.Server, path string) error {
switch v := command.(type) {
case cmds.GlazeCommand:
options := []datatables.QueryHandlerOption{
datatables.WithMiddlewares(middlewares...),
datatables.WithMiddlewares(middlewares_...),
datatables.WithTemplateLookup(cd.TemplateLookup),
datatables.WithTemplateName(cd.TemplateName),
datatables.WithAdditionalData(cd.AdditionalData),
Expand Down Expand Up @@ -298,7 +299,7 @@ func (cd *CommandDirHandler) Serve(server *parka.Server, path string) error {
output_file.CreateGlazedFileHandler(
v,
fileName,
middlewares...,
middlewares_...,
)(c)

case cmds.WriterCommand:
Expand Down
9 changes: 7 additions & 2 deletions pkg/handlers/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,15 @@ func NewCommandHandlerFromConfig(
func (ch *CommandHandler) Serve(server *parka.Server, path string) error {
path = strings.TrimSuffix(path, "/")

middlewares_ := ch.ParameterFilter.ComputeMiddlewares(ch.Stream)

server.Router.GET(path+"/data", func(c *gin.Context) {
json.CreateJSONQueryHandler(ch.Command)(c)
options := []json.QueryHandlerOption{
json.WithMiddlewares(middlewares_...),
}
json.CreateJSONQueryHandler(ch.Command, options...)(c)
})
middlewares_ := ch.ParameterFilter.ComputeMiddlewares(ch.Stream)
// TODO(manuel, 2024-01-17) This doesn't seem to match what is in command-dir
server.Router.GET(path+"/glazed", func(c *gin.Context) {
options := []datatables.QueryHandlerOption{
datatables.WithMiddlewares(middlewares_...),
Expand Down

0 comments on commit f0e138d

Please sign in to comment.