[TASK] http add GetRemoteIP
This commit is contained in:
parent
63b322494e
commit
34141ee2b0
|
@ -0,0 +1,11 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func GetRemoteIP(r *http.Request) string {
|
||||||
|
ip := r.Header.Get("X-Forwarded-For")
|
||||||
|
if len(ip) <= 1 {
|
||||||
|
ip = r.RemoteAddr
|
||||||
|
}
|
||||||
|
return ip
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Function to test the logging
|
||||||
|
// Input: pointer to teh testing object
|
||||||
|
func TestGetIP(t *testing.T) {
|
||||||
|
assertion := assert.New(t)
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("GET", "https://google.com/lola/duda?q=wasd", nil)
|
||||||
|
ip := GetRemoteIP(req)
|
||||||
|
|
||||||
|
assertion.Equal("", ip, "no remote ip address")
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
logger "github.com/Sirupsen/logrus"
|
logger "github.com/Sirupsen/logrus"
|
||||||
|
httpLib "github.com/genofire/golang-lib/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Current logger with it's configuration
|
// Current logger with it's configuration
|
||||||
|
@ -20,12 +21,8 @@ func init() {
|
||||||
|
|
||||||
// Function to add the information of a http request to the log
|
// Function to add the information of a http request to the log
|
||||||
func HTTP(r *http.Request) *logger.Entry {
|
func HTTP(r *http.Request) *logger.Entry {
|
||||||
ip := r.Header.Get("X-Forwarded-For")
|
|
||||||
if len(ip) <= 1 {
|
|
||||||
ip = r.RemoteAddr
|
|
||||||
}
|
|
||||||
return Log.WithFields(logger.Fields{
|
return Log.WithFields(logger.Fields{
|
||||||
"remote": ip,
|
"remote": httpLib.GetRemoteIP(r),
|
||||||
"method": r.Method,
|
"method": r.Method,
|
||||||
"url": r.URL.RequestURI(),
|
"url": r.URL.RequestURI(),
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue