Reputation: 893
I'm trying to write simple http server, that will serve requests to API. This is a code:
type Config struct {
ListenPort int `json:"listenPort"`
Requests []struct {
Request string `json:"request"`
ResponceFile string `json:"responceFile"`
} `json:"requests"`
}
...
func main() {
...
startServer(config)
}
func startServer(config Config) {
http.HandleFunc(apiPrefix+config.Requests[0].Request,
func(w http.ResponseWriter, r *http.Request) {
var dataStruct interface{}
err := loadJSON(config.Requests[0].ResponseFile, &dataStruct)
if err != nil {
w.Write([]byte("Oops! Something was wrong"))
}
data, _ := json.Marshal(dataStruct)
w.Header().Set("Content-Type", "application/json")
w.Write(data)
})
http.HandleFunc(apiPrefix+config.Requests[1].Request,
func(w http.ResponseWriter, r *http.Request) {
var dataStruct interface{}
err := loadJSON(config.Requests[1].ResponseFile, &dataStruct)
if err != nil {
w.Write([]byte("Oops! Something was wrong"))
}
data, _ := json.Marshal(dataStruct)
w.Header().Set("Content-Type", "application/json")
w.Write(data)
})
http.HandleFunc("/", http.NotFound)
port := ""
if config.ListenPort != 0 {
port = fmt.Sprintf(":%v", config.ListenPort)
} else {
port = ":8080"
}
fmt.Printf("Started @%v\n", port)
log.Fatal(http.ListenAndServe(port, nil))
}
func loadJSON(filePath string, retStruct interface{}) error {
fmt.Println(filePath)
fileJSON, err := ioutil.ReadFile(filePath)
json.Unmarshal(fileJSON, retStruct)
return err
}
And this is config, where files that should be returned via specific requests are described:
{
"listenPort": 8080,
"requests": [
{
"request": "switches/brocade",
"responseFile": "switches.json"
},
{
"request": "smth",
"responseFile": "smth.json"
}
]
}
So question is: why this code is not the same as code atop? It returns only last response file, described in config.json on all requests from this file? Or what is correct way to write dynamically-defined handlers?
func startServer(config Config) {
for _, req := config.Requests {
http.HandleFunc(apiPrefix+req.Request,
func(w http.ResponseWriter, r *http.Request) {
var dataStruct interface{}
err := loadJSON(req.ResponseFile, &dataStruct)
if err != nil {
w.Write([]byte("Oops! Something was wrong"))
}
data, _ := json.Marshal(dataStruct)
w.Header().Set("Content-Type", "application/json")
w.Write(data)
})
}
http.HandleFunc("/", http.NotFound)
Upvotes: 2
Views: 3235
Reputation: 142
You can create instance of struct with implement of http.Handler
that store ResponseFile
and pass it into http.Handle
type APIHandleFunc struct {
ResponseFile string
}
func (api *APIHandleFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
http.ServeFile(w, r, api.ResponseFile)
}
// StartServer .
func StartServer(config Config) {
for _, req := range config.Requests {
http.Handle(apiPrefix+"/"+req.Request, &APIHandleFunc{req.ResponceFile}/*new handler*/)
}
http.HandleFunc("/", http.NotFound)
}
or just create http.HandlerFunc
by pass ResponseFile
to it
// create handlerFunc
func getJSON(responseFile string) http.HandlerFunc {
return func (w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, responseFile)
}
}
// StartServer .
func StartServer(config Config) {
for _, req := range config.Requests {
http.Handle(apiPrefix+"/"+req.Request, getJSON(req.ResponseFile))
}
http.HandleFunc("/", http.NotFound)
}
and if you only write json to response just use http.ServeFile
Upvotes: 2
Reputation: 38343
This is because Go's range loops re-use the declared variable req
.
The iteration variables may be declared by the "range" clause using a form of short variable declaration (:=). In this case their types are set to the types of the respective iteration values and their scope is the block of the "for" statement; they are re-used in each iteration.
(emphasis mine)
This behaviour, together with the fact that you are capturing the variable inside a closure is the reason why all of your handlers refer to the value of the last variable.
Function literals are closures: they may refer to variables defined in a surrounding function. Those variables are then shared between the surrounding function and the function literal, and they survive as long as they are accessible.
To solve this you can create a new variable from the iteration variable inside the loop and have the closure use that.
https://play.golang.org/p/GTNbf1eeFKV
Upvotes: 4