Skip to content

Instantly share code, notes, and snippets.

@pranavraja
Created April 29, 2013 10:47
Show Gist options
  • Save pranavraja/5480903 to your computer and use it in GitHub Desktop.
Save pranavraja/5480903 to your computer and use it in GitHub Desktop.
Simple logging HTTP proxy in Go
package main
import (
"fmt"
"github.com/elazarl/goproxy"
"github.com/vrischmann/termcolor"
"log"
"net/http"
)
func main() {
proxy := goproxy.NewProxyHttpServer()
proxy.OnResponse().DoFunc(func(r *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
status := fmt.Sprintf("[%d]", r.StatusCode)
var coloredStatusCode string
switch r.StatusCode {
case 301, 302:
coloredStatusCode = termcolor.Colored(status, termcolor.Grey, termcolor.Bold)
case 200:
coloredStatusCode = termcolor.Colored(status, termcolor.Cyan)
case 404, 500:
coloredStatusCode = termcolor.Colored(status, termcolor.Red)
default:
coloredStatusCode = termcolor.Colored(status, termcolor.Magenta)
}
fmt.Printf("%s %s %v\n", coloredStatusCode, termcolor.Colored(ctx.Req.Method, termcolor.White, termcolor.Bold), ctx.Req.URL)
return r
})
log.Fatal(http.ListenAndServe(":8080", proxy))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment