简单的API网关代理

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

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(
    &quot;net/url&quot;
    &quot;net/http&quot;
    &quot;net/http/httputil&quot;
    &quot;github.com/codegangsta/martini&quot;
    &quot;fmt&quot;
)

func main() {
    remote, err := url.Parse(&quot;http://127.0.0.1:3000&quot;)
    if err != nil {
        panic(err)
    }

    proxy := httputil.NewSingleHostReverseProxy(remote)
    app := martini.Classic()
    app.Get(&quot;/users/**&quot;, handler(proxy))
    app.RunOnAddr(&quot;:4000&quot;)
}

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 = &quot;/authorize&quot;
        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 (
	&quot;log&quot;
	&quot;net/http&quot;
	&quot;net/http/httputil&quot;
	&quot;net/url&quot;
)

func main() {
	target, err := url.Parse(&quot;http://192.168.2.8:8000&quot;)
	if err != nil {
		log.Fatal(err)
	}
	http.Handle(&quot;/users/&quot;, http.StripPrefix(&quot;/users/&quot;, httputil.NewSingleHostReverseProxy(target)))
	http.Handle(&quot;/public/&quot;, http.StripPrefix(&quot;/public/&quot;, http.FileServer(http.Dir(&quot;./Documents&quot;))))
	log.Fatal(http.ListenAndServe(&quot;:8080&quot;, nil))
}

Wrap http.StripPrefix with a function that logs before calling it if you need logging.

huangapple
  • 本文由 发表于 2015年4月7日 01:50:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/29476611.html
匿名

发表评论

匿名网友

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

确定