Golang Goji:如何同时提供静态内容和API服务

huangapple go评论71阅读模式
英文:

Golang Goji: How to serve static content and api at the same time

问题

我过去两周一直在使用Golang,并最终成功运行了一个真实的应用程序。它使用由NGINX提供静态HTML文件,并且API使用Goji Web框架作为后端。我不使用任何Golang模板,因为一切都是Angular.Js,所以对于我的需求来说,静态文件是可以的。

我想要有选择的选项,可以在生产环境中使用NGINX,或者让Go在根目录下使用与应用程序相同的端口(8000)来提供静态内容。这样开发环境就不需要安装NGINX。

所以,我尝试像这样向默认的mux添加一个处理程序:

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")))
}

这个处理程序在所有API路径注册之后执行(否则API将无法工作)。

我已经尝试了各种组合,但要么会重定向到HTTP 404,要么会将HTML内容显示为文本。都不好。我想知道是否有人遇到过这个问题,并能告诉我我做错了什么。

谢谢。

虽然这与我的问题无关,但这是我使用的NGINX配置:

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;
    }
}
英文:

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;
}

}

答案1

得分: 6

我遇到了类似的问题,也许以下几点会有所帮助:

  • 记得将处理静态内容的处理程序注册为最后一个路由。否则,它可能匹配到所有内容。

  • 可以尝试使用绝对路径而不是相对路径。

以下是我在使用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)

    // 静态文件处理程序通常应该是最后一个注册的处理程序。否则,它将匹配到每个路径。
    // 确保使用绝对路径。
    staticFilesLocation := "包含静态内容的目录的绝对路径。"
    goji.Handle("/*", http.FileServer(http.Dir(staticFilesLocation)))

    goji.Serve()
}

希望对你有所帮助!

英文:

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()
}

答案2

得分: 2

如果您对URL具有完全控制权,一个简单的策略是在顶级进行划分。我在所有应用程序URL的开头使用/a,在所有静态URL的开头使用/s。这样可以使路由非常简单。

我之前使用过Goji,然后切换到了Gocraft-web。但是这两个框架的原则是相同的,无论使用哪个框架,URL都将是明确的。Gocraft-web显然可以进行子路由;我认为Goji也可以做到,只是不太明显。子路由有几个好处:

  • 它是消除歧义的一种简单方法
  • 如果搜索模式更简单,路由器可能会更快
  • 您可以将代码分割,这样可能更容易理解

如果您在生产环境中提供静态资源,您可能希望对其进行性能测量和改进。我发现对我的JS和CSS文件进行预压缩(gzip)可以帮助提高性能。我在同一个文件系统中同时拥有未压缩和压缩版本,并且我有一个专门的静态资源包,可以检测到预压缩的文件,并将其提供给所有支持的客户端(几乎所有浏览器都支持)。此外,设置未来的到期日期也值得探索。这两个想法都内置在Nginx中,并且只需一点努力即可编码实现。

英文:

If you have full control over your URLs, a simple strategy is to divide them at the top level. I use /a at the start of all application URLs and /s at the start of all static URLs. This makes the routing very simple.

I was using Goji for a while, then switched to Gocraft-web. But the principles are the same in that the URLs will be unambiguous with either framework. Gocraft-web can obviously do subrouting; I think Goji can also do this but it's less obvious. Subrouting is helpful for several reasons:

  • its an easy way to remove ambiguity
  • the router might well be faster if its search patterns are simpler
  • you can divide your code so it might be easier to understand

If you are serving static assets in production, you may like to measure it and improve its performance. I find that pre-compressing (gzip) my JS and CSS files can help. I have both uncompressed and compressed versions in the same file system and I have a bespoke static asset package that spots the pre-compressed files and serves them to all clients that understand (which is almost all browsers). Also, setting a future expiry date is worth exploring. Both of these ideas are built-into Nginx, and quite easy to code up with a bit of effort.

huangapple
  • 本文由 发表于 2014年10月12日 07:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/26320144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定