Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go simple API Gateway proxy

Tags:

proxy

go

martini

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


edit

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


edit 2

This only seems to be a problem when using it directly through the browser, XMLHttpRequest works just fine

like image 674
Dirk Avatar asked Apr 06 '15 17:04

Dirk


People also ask

Is API gateway a proxy?

A well-designed API gateway will act as a proxy and allow you to turn on or off certain capabilities that aren't relevant to the needs of your application. Every business is different, so a customizable API gateway is necessary to tailor your APIs experience to your desired use case.

Can API proxy act as API gateway?

An API Proxy Can Act As An API Gateway, But a Gateway Is Better. An API proxy may be a good solution for taking the first few simple steps with a basic API or two.

What is AWS API gateway proxy?

An HTTP proxy integration enables you to connect an API route to a publicly routable HTTP endpoint. With this integration type, API Gateway passes the entire request and response between the frontend and the backend. To create an HTTP proxy integration, provide the URL of a publicly routable HTTP endpoint.

How do I use API gateway proxy?

To set up a proxy integration in an API Gateway API with a proxy resource, you perform the following tasks: Create a proxy resource with a greedy path variable of { proxy +} . Set the ANY method on the proxy resource. Integrate the resource and method with a backend using the HTTP or Lambda integration type.


Video Answer


1 Answers

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.

like image 150
freeformz Avatar answered Nov 10 '22 00:11

freeformz