-
I'm trying to access some values in the HTTP request header (specifically the value of "remoteIP" ) i've created an Input struct that the request decoder is populating but i couldn't find any examples of getting values that aren't in the path, param, queryString, json body, etc. my http request looks like this (from server log, doesn't show json payload values)
as you can see, the this is my struct that i'm using for the usecase Input:
EDIT: I think the value of "remoteIP" in that request should correspond to the struct member |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Currently, the easiest way to access remote addr is to embed package main
import (
"context"
"fmt"
"log"
"net/http"
"github.com/swaggest/openapi-go/openapi31"
"github.com/swaggest/rest/request"
"github.com/swaggest/rest/web"
swgui "github.com/swaggest/swgui/v5cdn"
"github.com/swaggest/usecase"
)
func main() {
s := web.NewService(openapi31.NewReflector())
type inputWithReq struct {
request.EmbeddedSetter
Locale string `query:"locale" default:"en-US" pattern:"^[a-z]{2}-[A-Z]{2}$" enum:"ru-RU,en-US"`
Name string `path:"name" minLength:"3" default:"Jane"`
}
u := usecase.NewInteractor(func(ctx context.Context, input inputWithReq, output *struct{}) error {
fmt.Println("Remote addr:", input.Request().RemoteAddr) // <------------------- *http.Request access
return nil
})
s.Get("/hello/{name}", u)
s.Docs("/docs", swgui.New)
log.Println("http://localhost:8011/docs")
if err := http.ListenAndServe("localhost:8011", s); err != nil {
log.Fatal(err)
}
} Perhaps, we can introduce some field tag to implement automated mapping of remote addr (and maybe some other fields) into structure fields. |
Beta Was this translation helpful? Give feedback.
Currently, the easiest way to access remote addr is to embed
request.EmbeddedSetter
and then get direct access to*http.Request
.