Repository navigation
|
Hi, Assuming I have two different entities, say "product" and "location". I create GetProduct, ListProduct, GetLocation and ListLocation API endpoints. For I created a middleware to pass the information into the context: func nextMiddleware(ctx huma.Context, next func(huma.Context)) {
ctx = huma.WithValue(ctx, "url", ctx.URL())
ctx = huma.WithValue(ctx, "host", ctx.Host())
next(ctx)
}And then, within my handler, access it: func listProducts(productservice *service.ProductService) func(context.Context, *ListProductsInput) (*ListProductsOutput, error) {
return func(ctx context.Context, input *ListProductsInput) (resp *ListProductsOutput, err error) {
// use service.ProductService to fetch Products
// transform them into ListProductsOutput
// trying to generate a next Url which modifies pagesize and offset, so the user can immediately fetch the next n Products with this link
reqUrl := ctx.Value("url").(url.URL)
reqHost := ctx.Value("host").(string)
values := url.Values{}
values.Add("offset", strconv.Itoa(input.Offset+50))
reqUrl.Host = reqHost
reqUrl.RawQuery = values.Encode()
slog.Debug("next url will be", slog.String("url", reqUrl.String()))This results in the following output:
Is this expected behaviour? If so, how could I access the Scheme? Am I completely missing the plot and doing this entirely wrong? I was considering to add this info entirely on the Middleware, but Product and Location may use different values for max page size - that's why I tried moving it into the handler. EDIT: The API is created as following: func createServer(cfg Config, mux *http.ServeMux) *http.Server {
slog.Info("Creating HTTP Server", slog.String("listener", cfg.Listener))
return &http.Server{
Addr: cfg.Listener,
Handler: mux,
}
}
func createApi(cfg Config, mux *http.ServeMux) huma.API {
humaConfig := huma.DefaultConfig("app", "0.0.1")
endpoint := ""
if cfg.Endpoint == "" {
endpoint = fmt.Sprintf("http://%s/", cfg.Listener)
} else {
endpoint = cfg.Endpoint
}
slog.Info("Creating API Endpoint", slog.String("endpoint", endpoint))
humaConfig.Servers = []*huma.Server{{URL: endpoint}}
return humago.New(
mux,
humaConfig,
)
}where cfg.Listener is just |
Replies: 1 comment 1 reply
|
Hey @dev00, yes this is expected. func absURL(ctx huma.Context) url.URL {
u := ctx.URL()
u.Host = ctx.Host()
u.Scheme = "http"
if ctx.TLS() != nil {
u.Scheme = "https"
}
// behind a TLS-terminating proxy
if p := ctx.Header("X-Forwarded-Proto"); p != "" {
u.Scheme = p
}
if h := ctx.Header("X-Forwarded-Host"); h != "" {
u.Host = h
}
return u
}To use it in the handler, stash the type ctxKey struct{}
func urlMiddleware(ctx huma.Context, next func(huma.Context)) {
next(huma.WithValue(ctx, ctxKey{}, absURL(ctx)))
}Then Use a struct key, not a bare string, so it can't collide with another package's value. |
Hey @dev00, yes this is expected.
ctx.URL()returns the request URL as the server sees it, and for server-side requests Go only fills inPath/RawQuery(see the net/http.Request docs: scheme and host live inHostand the connection state, not the URI). No middleware needed for either piece, both are already onhuma.Context:To use it in the handler, stash the
huma.Context