duanqian2368 2014-10-11 23:01
浏览 41
已采纳

Golang Goji:如何同时提供静态内容和api

I have been playing with Golang for the last two weeks and finally could make a real application work. It uses static HTML files served by NGINX and the API is using Goji Web Framework as backend. I don't use any Golang templating because everything is Angular.Js, so static is fine for my needs.

I would like to have the option to chose whether to use NGINX on production or let Go serve the static content at root using the same port the application uses (8000). This way development environments would not require NGINX to be installed.

So, tried adding a handle to the default mux like this

goji.DefaultMux.Handle("/*", serveStatic)

func serveStatic(w http.ResponseWriter, r *http.Request) {
//http.ServeFile(w, r, r.URL.Path[1:])
//http.FileServer(http.Dir("static"))
http.StripPrefix("/static/", http.FileServer(http.Dir("static")))

}

This handle is executed just after all the API paths have been registered (otherwise API would not work).

I already tried any sort of combination and either it redirects me to HTTP 404 or it displays the HTML content as text. Neither is good. I wonder if anyone has been here and could give me a heads up on what am I doing wrong.

Thanks.

Although this has nothing to do with my issue, here is the NGINX configuration I am using:

server {
listen 80;

# enable gzip compression
    gzip on;
    gzip_min_length  1100;
    gzip_buffers  4 32k;
    gzip_types    text/plain application/x-javascript text/xml text/css;
    gzip_vary on;
# end gzip configuration

location / {
    root /home/mleyzaola/go/src/bitbucket.org/mauleyzaola/goerp/static;
    try_files $uri $uri/ /index.html = 404;
}

location /api {
    proxy_pass http://localhost:8000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}

  • 写回答

2条回答 默认 最新

  • dtddjq9637 2014-10-12 01:36
    关注

    I had run into similar issues so perhaps the following points would be helpful.

    • Remember to register the handler for serving static content as the final route. Otherwise, it might match everything.

    • Perhaps try using absolute paths instead of relative ones.

    Here's a simplified version of how my routes are set up with Goji.

    func apiExampleHandler(context web.C, resp http.ResponseWriter, req *http.Request) {
        fmt.Fprint(resp, "You've hit the API!")
    }
    
    func main() {
        goji.Handle("/api", apiExampleHandler)
    
        // Static file handler should generally be the last handler registered. Otherwise, it'll match every path.
        // Be sure to use an absolute path.
        staticFilesLocation := "Some absolute to the directory with your static content."
        goji.Handle("/*", http.FileServer(http.Dir(staticFilesLocation)))
    
        goji.Serve()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?