英文:
Go simple API Gateway proxy
问题
我一直在互联网上搜索如何做到这一点,但是我没有找到。我正在尝试使用Go和Martini构建一个简单的API网关,用于管理我的系统中运行的一些具有REST接口的微服务。例如,我有一个在192.168.2.8:8000
上运行的users
服务,我想通过/users
访问它。
所以我的API网关看起来像这样:
package main
import (
"github.com/codegangsta/martini"
"net/http"
)
func main(){
app := martini.Classic()
app.Get("/users/:resource", func(req *http.Request, res http.ResponseWriter){
//proxy to http://192.168.2.8:8000/:resource
})
app.Run()
}
我已经有一些工作了,但我只看到[vhost v2] release 2.2.5
:
package main
import(
"net/url"
"net/http"
"net/http/httputil"
"github.com/codegangsta/martini"
"fmt"
)
func main() {
remote, err := url.Parse("http://127.0.0.1:3000")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
app := martini.Classic()
app.Get("/users/**", handler(proxy))
app.RunOnAddr(":4000")
}
func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request, martini.Params) {
return func(w http.ResponseWriter, r *http.Request, params martini.Params) {
fmt.Println(params)
r.URL.Path = "/authorize"
p.ServeHTTP(w, r)
}
}
只有在直接通过浏览器使用时才会出现这个问题,XMLHttpRequest
工作正常。
英文:
I've been searching all over the internet how to do this, but I haven't been able to find it. I'm trying to build a simple API gateway using Go and Martini for my system that has a few microservices with REST interfaces running. For example, I have my users
service running on 192.168.2.8:8000
, and I want to access it through /users
So my API gateway would look something like this:
package main
import (
"github.com/codegangsta/martini"
"net/http"
)
func main(){
app := martini.Classic()
app.Get("/users/:resource", func(req *http.Request, res http.ResponseWriter){
//proxy to http://192.168.2.8:8000/:resource
})
app.Run()
}
<hr>
edit
<hr>
I've got something working, but all i see is [vhost v2] release 2.2.5
:
package main
import(
"net/url"
"net/http"
"net/http/httputil"
"github.com/codegangsta/martini"
"fmt"
)
func main() {
remote, err := url.Parse("http://127.0.0.1:3000")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
app := martini.Classic()
app.Get("/users/**", handler(proxy))
app.RunOnAddr(":4000")
}
func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request, martini.Params) {
return func(w http.ResponseWriter, r *http.Request, params martini.Params) {
fmt.Println(params)
r.URL.Path = "/authorize"
p.ServeHTTP(w, r)
}
}
<hr>
edit 2
<hr>
This only seems to be a problem when using it directly through the browser, XMLHttpRequest
works just fine
答案1
得分: 12
stdlib版本
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
target, err := url.Parse("http://192.168.2.8:8000")
if err != nil {
log.Fatal(err)
}
http.Handle("/users/", http.StripPrefix("/users/", httputil.NewSingleHostReverseProxy(target)))
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./Documents"))))
log.Fatal(http.ListenAndServe(":8080", nil))
}
如果需要记录日志,请使用一个在调用http.StripPrefix
之前记录日志的函数来包装它。
英文:
stdlib version
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
target, err := url.Parse("http://192.168.2.8:8000")
if err != nil {
log.Fatal(err)
}
http.Handle("/users/", http.StripPrefix("/users/", httputil.NewSingleHostReverseProxy(target)))
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./Documents"))))
log.Fatal(http.ListenAndServe(":8080", nil))
}
Wrap http.StripPrefix
with a function that logs before calling it if you need logging.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论