Fiber: grouping routes #508
-
Hi! I have a question regarding grouping routes using huma with fiber. From the example below, my expectation, that all sub-groups of
Here is what I did using fiber:
So I provided OpenAPI server as |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I'm not sure if I understand you correctly, but you can see a working example for api-grouping with fiber below (without the Consider that you have to specify the base path for the docs in the config, but there is no built-in functionality for multiple routing groups. (See) As danielgtaylor mentioned here you can add a middleware which checks the incoming path to handle it.
config.Servers = []*huma.Server{
{URL: "http://127.0.0.1:3001/api/group1"},
} package main
import (
"context"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/adapters/humafiber"
"github.com/gofiber/fiber/v2"
)
type Response struct {
Body Body
}
type Body struct {
Message string `json:"message"`
}
func main() {
app := fiber.New()
config := huma.DefaultConfig("some-api", "1.0.0")
config.Servers = []*huma.Server{
{URL: "http://127.0.0.1:3001/api/group1"},
}
app.Get("/fiber", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
// api/group
{
group := app.Group("/api/group1")
api := humafiber.NewWithGroup(app, group, config)
huma.Get(api, "/", func(ctx context.Context, input *struct{}) (*Response, error) {
return &Response{Body: Body{Message: "/api/group1/huma works"}}, nil
})
huma.Post(api, "/huma", func(ctx context.Context, input *struct{}) (*struct{}, error) { return nil, nil })
}
// api/group2
{
group := app.Group("/api/group2")
api := humafiber.NewWithGroup(app, group, config)
huma.Get(api, "/", func(ctx context.Context, input *struct{}) (*Response, error) {
return &Response{Body: Body{Message: "/api/group2/huma works"}}, nil
})
huma.Post(api, "/huma", func(ctx context.Context, input *struct{}) (*struct{}, error) { return nil, nil })
}
app.Listen(":3001")
} Your mistake was that you set the |
Beta Was this translation helpful? Give feedback.
I'm not sure if I understand you correctly, but you can see a working example for api-grouping with fiber below (without the
/docs
feature):Consider that you have to specify the base path for the docs in the config, but there is no built-in functionality for multiple routing groups. (See)
As danielgtaylor mentioned here you can add a middleware which checks the incoming path to handle it.