Go simple API Gateway proxy - go

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

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.

Related

how to create a reverse proxy in golang

I wants to make a reverse proxy in golang using net package from stl library. Used httputil for creating reverse proxy. But when I make request to the proxy server it return 404 error.
Here is the proxy server code
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
demoUrl , err := url.Parse("http://localhost:1000/run/")
if err!=nil{
log.Fatal(err)
return
}
proxy := httputil.NewSingleHostReverseProxy(demoUrl)
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(rw, r)
})
http.ListenAndServe(":2000", nil)
}
Here is the origin server code :
package main
import (
"net/http"
)
func main(){
http.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request){
w.Write([]byte("I am running"))
})
http.ListenAndServe(":1000", nil)
}
Please tell what I am missing here and how to fix the bug! Please
The router doesn't match.
this will be working as expected.
...
func main(){
http.HandleFunc("/run/", func(w http.ResponseWriter, r *http.Request){
w.Write([]byte("I am running"))
})
http.ListenAndServe(":1000", nil)
}
...

How to test reverse proxy with martini in go

I'm writing test code for martini app working as a reverse proxy in go, and want to test it using httptest.ResponseRecorder, but I got an error the following.
[martini] PANIC: interface conversion: *httptest.ResponseRecorder is not http.CloseNotifier: missing method CloseNotify
httptest.ResponseRecorder has no method CloseNotify()
How should I test it?
package main
import (
"github.com/go-martini/martini"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"testing"
)
func TestReverseProxy(t *testing.T) {
// Mock backend
backendResponse := "I am the backend"
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(backendResponse))
}))
defer backend.Close()
backendURL, _ := url.Parse(backend.URL)
// Frontend
m := martini.Classic()
m.Get("/", func(w http.ResponseWriter, r *http.Request) {
proxy := httputil.NewSingleHostReverseProxy(backendURL)
proxy.ServeHTTP(w, r)
})
// Testing
req, _ := http.NewRequest("GET", "/", nil)
res := httptest.NewRecorder()
m.ServeHTTP(res, req)
assert.Equal(t, 200, res.Code, "should be equal")
}
First, please note that the martini framework is no longer maintained as said in their README.
Then, about your issue, it's because Martini does something that looks pretty bad to me: it takes an http.ResponseWriter and assumes it is also an http.CloseNotifier, while there is absolutely no guarantee of this. They should take a custom interface wrapping both of them, something like that:
type ResponseWriterCloseNotifier interface {
http.ResponseWriter
http.CloseNotifier
}
You can see in their source code that they had the same issue for their own tests, and used some workaround: https://github.com/go-martini/martini/commit/063dfcd8b0f64f4e2c97f0bc27fa422969baa23b#L13
Here is some working code made with it:
package main
import (
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"testing"
"github.com/go-martini/martini"
"github.com/stretchr/testify/assert"
)
type closeNotifyingRecorder struct {
*httptest.ResponseRecorder
closed chan bool
}
func newCloseNotifyingRecorder() *closeNotifyingRecorder {
return &closeNotifyingRecorder{
httptest.NewRecorder(),
make(chan bool, 1),
}
}
func (c *closeNotifyingRecorder) close() {
c.closed <- true
}
func (c *closeNotifyingRecorder) CloseNotify() <-chan bool {
return c.closed
}
func TestReverseProxy(t *testing.T) {
// Mock backend
backendResponse := "I am the backend"
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(backendResponse))
}))
defer backend.Close()
backendURL, _ := url.Parse(backend.URL)
// Frontend
m := martini.Classic()
m.Get("/", func(w http.ResponseWriter, r *http.Request) {
proxy := httputil.NewSingleHostReverseProxy(backendURL)
proxy.ServeHTTP(w, r)
})
// Testing
req, _ := http.NewRequest("GET", "/", nil)
res := newCloseNotifyingRecorder()
m.ServeHTTP(res, req)
assert.Equal(t, 200, res.Code, "should be equal")
}

"Blank response" NotFoundHandler not working Gorilla

I am trying to debug 404-not-found by writing a custom not-found handler. Here is my code.
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/coopernurse/gorp"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
func main() {
// Create a MUX
r := mux.NewRouter()
http.Handle("/", r)
r.NotFoundHandler = http.HandlerFunc(NotFound)
// Static
r.PathPrefix("/app").HandlerFunc(uiAppHandler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
func NotFound(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "custom 404")
}
func uiAppHandler(w http.ResponseWriter, r *http.Request) {
repoFrontend1 := "/UI/KD/WebContent"
http.StripPrefix("/app/", http.FileServer(http.Dir(repoFrontend1)))
}
I am getting a blank response for both existing and non-existing files. I guess NotFound is not getting triggered because of my "/" handler. Then how do I handle notFound for http.Dir?
Here is my directory structure
The response from uiAppHandler is blank because the function does not write to the response w. You should register the file server handler directly with the mux instead of trying to create a handler:
r.PathPrefix("/app").Handler(http.StripPrefix("/app/", http.FileServer(http.Dir(repoFrontend1))))
The mux passes all requests with the prefix "/app" to the handler registered for that prefix. All requests with that prefix are found as far as the mux is concerned. The http.FileServer or whatever you register for that prefix is responsible for generating the 404 response.

gorilla mux router handlers

I can not get the gorilla mux to work..
When requesting http://www.localhost:9000 this is returned by the web server 404 page not found
But this works http://localhost:9000/ and prints Hello world
package main
import (
"net/http"
"fmt"
"log"
"github.com/gorilla/mux"
)
func Handler(w http.ResponseWriter, r *http.Request){
fmt.Fprint(w, "Hello world")
}
func main(){
r := mux.NewRouter()
r.Host("www.localhost")
r.HandleFunc("/", Handler)
err := http.ListenAndServe(":9000", r)
if err != nil {
log.Fatal("ListenAndServe error: ", err)
}
}
You want to be able to support both localhost and www.localhost
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello world")
}
func main() {
r := mux.NewRouter()
r.Host("www.localhost").Path("/").HandlerFunc(Handler)
r.HandleFunc("/", Handler)
err := http.ListenAndServe(":9000", r)
if err != nil {
log.Fatal("ListenAndServe error: ", err)
}
}
If you read the documentation carefully, you'll notice that r.Host() is just another pattern matching function. It doesn't set any global rule for that router.
if you want to make that rule to be inherited you'll need to use a subrouter:
subrouter := r.Host("www.localhost").Subrouter()
then you use "subrouter" in place of "r"

How to pass type into an http handler

I'm attempting to separate my http go code into "controllers" by creating a new package for them, but can't figure out how to pass a db type into the handler. I want to be able to pass in the Db type that I create in main.go into my Index handler in index.go. If this is the wrong way to solve this, let me know a better way (I'm learning as I go and would like to keep it simple for now). My code so far:
main.go:
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"log"
"mvc3/app/c"
"net/http"
)
var Db *sql.DB
func main() {
fmt.Println("Starting up!")
var err error
Db, err = sql.Open("mysql", "root#/dev?charset=utf8")
if err != nil {
log.Fatalf("Error on initializing database connection: %s", err.Error())
}
Db.SetMaxIdleConns(100)
err = Db.Ping()
if err != nil {
log.Fatalf("Error on opening database connection: %s", err.Error())
}
r := mux.NewRouter()
r.HandleFunc("/", c.Index)
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}
/app/c/index.go:
package c
import (
"fmt"
"net/http"
)
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
}
Thanks!
use a closure.
in app/c change Index to:
func Index(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// do stuff with db here
fmt.Fprintf(w, "Hello world!")
}
}
then in your main function use it like so: r.HandleFunc("/", c.Index(db))
The Index function returns an anonymous function that fits the the HandleFunc type and also closes over the value of the db that was passed in giving your handler access to that variable.

Resources