aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorjackyzha0 <[email protected]>2020-05-09 14:32:24 -0700
committerjackyzha0 <[email protected]>2020-05-09 14:32:24 -0700
commitc9019b70c1f1828b197a4ae2595b87592cb256d9 (patch)
treee500671c1422bc7a99a122e9003ac6c660949757 /api
parentworking mongo doc add (diff)
downloadctrl-v-c9019b70c1f1828b197a4ae2595b87592cb256d9.tar.xz
ctrl-v-c9019b70c1f1828b197a4ae2595b87592cb256d9.zip
ip changes and remove some debug
Diffstat (limited to 'api')
-rw-r--r--api/api.go2
-rw-r--r--api/ip.go19
-rw-r--r--api/routes.go2
3 files changed, 11 insertions, 12 deletions
diff --git a/api/api.go b/api/api.go
index eb6af4d..81ee0a0 100644
--- a/api/api.go
+++ b/api/api.go
@@ -30,7 +30,7 @@ func Serve(port int) {
// Define Mux Router
r := mux.NewRouter()
r.HandleFunc("/health", healthCheckFunc)
- r.HandleFunc("/", insertFunc).Methods("POST")
+ r.HandleFunc("/api", insertFunc).Methods("POST")
http.Handle("/", r)
diff --git a/api/ip.go b/api/ip.go
index d65117f..0d135b3 100644
--- a/api/ip.go
+++ b/api/ip.go
@@ -1,38 +1,37 @@
package api
import (
- "fmt"
"net"
"net/http"
"strings"
)
-func getIP(r *http.Request) (string, error) {
- //Get IP from the X-REAL-IP header
+func getIP(r *http.Request) (s string) {
+ // Get IP from the X-REAL-IP header
ip := r.Header.Get("X-REAL-IP")
netIP := net.ParseIP(ip)
if netIP != nil {
- return ip, nil
+ return ip
}
- //Get IP from X-FORWARDED-FOR header
+ // Get IP from X-FORWARDED-FOR header
ips := r.Header.Get("X-FORWARDED-FOR")
splitIps := strings.Split(ips, ",")
for _, ip := range splitIps {
netIP := net.ParseIP(ip)
if netIP != nil {
- return ip, nil
+ return ip
}
}
- //Get IP from RemoteAddr
+ // Get IP from RemoteAddr
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
- return "", err
+ return
}
netIP = net.ParseIP(ip)
if netIP != nil {
- return ip, nil
+ return
}
- return "", fmt.Errorf("No valid ip found")
+ return
}
diff --git a/api/routes.go b/api/routes.go
index 655d18b..6f9c17b 100644
--- a/api/routes.go
+++ b/api/routes.go
@@ -21,7 +21,7 @@ func insertFunc(w http.ResponseWriter, r *http.Request) {
// get ip
ip, _ := getIP(r)
- log.Infof("got content `%s` and ip %s", content, ip)
+ log.Infof("got content '%s' and ip '%s'", content, ip)
// insert content
err := db.New(ip, content)