Posts are showing from Go categories

A simple reverse proxy in Go using Gin

by Sebastien le gall, at 23 June 2020, category : Go

Reverse proxies are great to handle logging, tracing, and authentication on applications you don't own or which you cannot modify the source code to make them do what you need.

A reverse proxy is nothing more than an HTTP server that handles a request and makes the request to a backend server. When doing so, it may add some headers, log data about the request or stop the request if authentication fails.

Go provide a reverse proxy feature in its standard library. It's a great start, but you will probably find out it lacks some helper features such as logging or handling routing easily.

gin

On the other hand, Gin is a very popular and great web-based application framework. It helps to build powerful REST API quickly, it has a lot of useful features and it's very fast since it uses httprouter under the hood. The idea is to handle HTTP requests with Gin and then proxy the request to the backend using the built-in reverse proxy.

The ReverseProxy struct contains a Director field. A director a function where you tell the reverse proxy what to do with the incoming request. It is where you may change the host, maybe add some headers, or even check the authentication.

remote, err := url.Parse("http://myremotedomain.com")
if err != nil {
	panic(err)
}

proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Director = func(req *http.Request) {
	req.Host = remote.Host
	req.URL.Scheme = remote.Scheme
	req.URL.Host = remote.Host
}

read more

Parsing git status with Go

by Sebastien le gall, at 15 June 2020, category : Go Git

At work I often have to switch from a repo to another, making some changes here and not there, starting a new branch on repo A and then, fixing an issue on repo B. Ideally, a big fat monorepo should help me to deal with the ~40 apps and libs I work on. But monorepos have drawbacks when you're not Facebook or Google. By the time, they become heavier and soon a simple git status takes more than 2 seconds.

Instead of a monorepo I have built a few script helping me to deal with all those repos. One of them helps me knowing the status of each repository, just to know where I am and what I was doing before being interrupted.

So here we are… building a Go script to parse a git repository status.

read more

Integrating logrus with cobra

by Sebastien le gall, at 17 January 2019, category : Go Logrus Cobra

You can find a working example on my GitHub: https://github.com/seblegall/blog-scripts/tree/master/logrus-cobra

I recently had to refactor the way one of my app print logs. My idea was to use logrus as it is a very well known lib to produce logs with Go.

Logrus let you print nicely color-coded and structured logs and is completely API compatible with the standard library logger.

logrus

My first question was about log level and how to setup it once across all sub-directory/package of my project.

Under the hood, logrus instantiate a new variable log. Once done, calling the logrus.SetLevel() function will store the level directly in this newly instantiated struct.

Thus, we have a global struct, available across all sub-package making the level automatically shared without having to use dependency injection.

If you need to set a different level in a sub-package (for example), you just need to instantiate a new logger :

var log = logrus.New()

//...

log.Debug("debug log")

read more