doujia2090 2012-09-20 19:33
浏览 556
已采纳

golang传递http.ResponseWriter

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.

  • 写回答

1条回答 默认 最新

  • dongwu8050 2012-09-21 04:26
    关注

    There are a number of ways you can do this. The trick is to figure out what is you actually need from the ResponseWriter you are passing through. It sounds like you just need to exercise a little function composition.

    Change your design so that the View returns an io.Reader, and an error that you can then pipe into the ResponseWriter. Here's a completely untested example:

    func View(path string, data interface{}) (io.Reader, error) {
        // Go grab file from views folder
        temp, err := template.ParseFiles(path+".html")
        if err != nil {
            // Couldnt find html file send error
           return nil, err
        } else {
            buf := bytes.Buffer()
            temp.ExecuteTemplate(buf, temp.Name(), data)
            return buf
        }
    }
    
    func HandleFunc(handlepath string, runfunc func(*UrlInfo) (io.Reader, error)) {
        // 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)
    
            rdr, err := runfunc(url)
            io.Copy(w, rdr);
        })
    }
    

    With this the only thing that has to worry about the http ResponseWriter is your HandleFunc function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)