New HTTP logging flag and related code

This commit is contained in:
Thomas Schwery 2017-03-03 13:29:14 +01:00
parent dca2c12487
commit cbd327633d

27
main.go
View file

@ -37,6 +37,8 @@ import (
ESIWallet "./client/wallet"
httptransport "github.com/go-openapi/runtime/client"
loghttp "github.com/motemen/go-loghttp"
)
// Character - Structure to save the verification data.
@ -77,6 +79,7 @@ var (
cfgFilePath = flag.String("config", "configuration.toml", "Path to the configuration file.")
cacheDBPath = flag.String("cache", "cache.ldb", "Path to the cache leveldb database.")
insecureFlag = flag.Bool("insecure", false, "Do not check the HTTPS certificate")
logCalls = flag.Bool("httplog", false, "Log HTTP calls")
)
var ctx = context.Background()
@ -86,7 +89,7 @@ var cacheDB *leveldb.DB
func main() {
flag.Parse()
logfilename := fmt.Sprintf("log-%s.log", time.Now().Format("2006-01-02_15_04_05"))
logfilename := fmt.Sprintf("log-%s.log", time.Now().Format("2006-01-02"))
f, err := os.OpenFile(logfilename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
@ -118,11 +121,29 @@ func main() {
cachingTransport := httpcache.NewTransport(ldb)
if *insecureFlag {
proxiedTransport := &http.Transport{
insecureTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyFromEnvironment,
}
cachingTransport.Transport = proxiedTransport
cachingTransport.Transport = insecureTransport
}
if *logCalls {
var CustomLogResponse = func(resp *http.Response) {
log.Printf("<--- HTTP %d.%d %d %s (expires on %s) %s",
resp.ProtoMajor, resp.ProtoMinor,
resp.StatusCode,
resp.Request.URL,
resp.Header.Get("expires"),
resp.Header.Get("content-type"),
)
}
loggingTransport := &loghttp.Transport{
LogResponse: CustomLogResponse,
}
loggingTransport.Transport = cachingTransport.Transport
cachingTransport.Transport = loggingTransport
}
cachingClient := &http.Client{Transport: cachingTransport}