I am trying to figure out if its possible to not have to pass along the http.ResponseWriter everywhere i go when programming a web application. I am setting up a simple mvc web framework and i find myself having to pass the http.ResponseWriter through various functions when its only used in lets say the last function.
Routes package
// Struct containing http requests and variables
type UrlInfo struct {
Res http.ResponseWriter
Req *http.Request
Vars map[string]string
}
func HandleFunc(handlepath string, runfunc func(*UrlInfo)) {
// Set handler and setup struct
http.HandleFunc(getHandlePath(handlepath), func(w http.ResponseWriter, r *http.Request) {
url := new(UrlInfo)
url.Res = w
url.Req = r
url.Vars = parsePathVars(r.URL.Path, handlepath)
runfunc(url)
})
}
// Parse file and send to responsewriter
func View(w http.ResponseWriter, path string, data interface{}) {
// Go grab file from views folder
temp, err := template.ParseFiles(path+".html")
if err != nil {
// Couldnt find html file send error
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
temp.ExecuteTemplate(w, temp.Name(), data)
}
}
Controller package
import (
"routes"
)
func init() {
// Build handlefunc
routes.HandleFunc("/home/", home)
}
func home(urlinfo *routes.UrlInfo) {
info := make(map[string]string)
info["Title"] = urlinfo.Vars["title"]
info["Body"] = "Body Info"
gi.View(urlinfo.Res, "pages/about", info)
}
I would like to not have to pass anything in the home function just so i can pass it again to the view function to spit out. Would be nice to be able to set it in one place and pull from it whenever needed. This would also nice in regard to multiple packages that communicate to the routes package in the same regard.
Any and all thoughts, tips or tricks are welcome. Thanks.