aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjackyzha0 <[email protected]>2020-05-09 14:28:24 -0700
committerjackyzha0 <[email protected]>2020-05-09 14:28:24 -0700
commit35ce1bff3496c67cda124aea3c36bc5b66b1e4e6 (patch)
tree6dedc5d47077ff9831f2e944bc83dd957eb3f894
parentMerge pull request #1 from jackyzha0/hash (diff)
downloadctrl-v-35ce1bff3496c67cda124aea3c36bc5b66b1e4e6.tar.xz
ctrl-v-35ce1bff3496c67cda124aea3c36bc5b66b1e4e6.zip
working mongo doc add
-rw-r--r--api/api.go1
-rw-r--r--api/ip.go38
-rw-r--r--api/routes.go23
-rw-r--r--cache/cache.go39
-rw-r--r--db/db.go12
-rw-r--r--db/mongo.go10
6 files changed, 120 insertions, 3 deletions
diff --git a/api/api.go b/api/api.go
index 1954496..eb6af4d 100644
--- a/api/api.go
+++ b/api/api.go
@@ -30,6 +30,7 @@ func Serve(port int) {
// Define Mux Router
r := mux.NewRouter()
r.HandleFunc("/health", healthCheckFunc)
+ r.HandleFunc("/", insertFunc).Methods("POST")
http.Handle("/", r)
diff --git a/api/ip.go b/api/ip.go
new file mode 100644
index 0000000..d65117f
--- /dev/null
+++ b/api/ip.go
@@ -0,0 +1,38 @@
+package api
+
+import (
+ "fmt"
+ "net"
+ "net/http"
+ "strings"
+)
+
+func getIP(r *http.Request) (string, error) {
+ //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
+ }
+
+ //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
+ }
+ }
+
+ //Get IP from RemoteAddr
+ ip, _, err := net.SplitHostPort(r.RemoteAddr)
+ if err != nil {
+ return "", err
+ }
+ netIP = net.ParseIP(ip)
+ if netIP != nil {
+ return ip, nil
+ }
+ return "", fmt.Errorf("No valid ip found")
+}
diff --git a/api/routes.go b/api/routes.go
index edb5a02..655d18b 100644
--- a/api/routes.go
+++ b/api/routes.go
@@ -3,8 +3,29 @@ package api
import (
"fmt"
"net/http"
+
+ "github.com/jackyzha0/ctrl-v/db"
+
+ log "github.com/sirupsen/logrus"
)
func healthCheckFunc(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "status ok")
+ fmt.Fprint(w, "status ok")
+}
+
+func insertFunc(w http.ResponseWriter, r *http.Request) {
+ // get content
+ _ = r.ParseMultipartForm(0)
+ content := r.FormValue("content")
+
+ // get ip
+ ip, _ := getIP(r)
+
+ log.Infof("got content `%s` and ip %s", content, ip)
+
+ // insert content
+ err := db.New(ip, content)
+ if err != nil {
+ fmt.Fprintf(w, "got err: %s", err.Error())
+ }
}
diff --git a/cache/cache.go b/cache/cache.go
new file mode 100644
index 0000000..da27939
--- /dev/null
+++ b/cache/cache.go
@@ -0,0 +1,39 @@
+package cache
+
+import (
+ "sync"
+
+ "github.com/jackyzha0/ctrl-v/db"
+)
+
+type Cache struct {
+ m map[string]db.Paste
+ lock sync.RWMutex
+}
+
+func New() *Cache {
+ return &Cache{
+ m: map[string]db.Paste{},
+ }
+}
+
+func (c *Cache) Get(hash string) (db.Paste, error) {
+ c.lock.RLock()
+ defer c.lock.RUnlock()
+
+ // check if hash in cache
+ if v, ok := c.m[hash]; ok {
+ return v, nil
+ }
+
+ // if it doesnt, lookup from db
+ p, err := db.Lookup(hash)
+ c.add(p)
+ return p, err
+}
+
+func (c *Cache) add(p db.Paste) {
+ c.lock.Lock()
+ c.m[p.Hash] = p
+ c.lock.Unlock()
+}
diff --git a/db/db.go b/db/db.go
index 5c35495..a6f439e 100644
--- a/db/db.go
+++ b/db/db.go
@@ -3,6 +3,7 @@ package db
import (
"os"
+ "github.com/jackyzha0/ctrl-v/hashing"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
)
@@ -21,8 +22,10 @@ func init() {
initSessions(mUser, mPass, mIP)
}
-// creates a new
-func New(hash, content string) error {
+// creates a new paste with content and hash
+func New(ip, content string) error {
+ // generate hash from ip
+ hash := hashing.GenerateURI(ip)
// create new struct
new := Paste{
@@ -35,3 +38,8 @@ func New(hash, content string) error {
insertErr := insert(new)
return insertErr
}
+
+// lookup
+func Lookup(hash string) (Paste, error) {
+ return fetch(hash)
+}
diff --git a/db/mongo.go b/db/mongo.go
index c3e31dd..1c694e7 100644
--- a/db/mongo.go
+++ b/db/mongo.go
@@ -6,6 +6,7 @@ import (
"net"
"github.com/globalsign/mgo"
+ "github.com/globalsign/mgo/bson"
log "github.com/sirupsen/logrus"
)
@@ -39,5 +40,14 @@ func initSessions(user, pass, ip string) {
}
func insert(new Paste) error {
+ log.Infof("new paste struct: %+v", new)
return pastes.Insert(new)
}
+
+func fetch(hash string) (Paste, error) {
+ p := Paste{}
+
+ q := bson.M{"hash": hash}
+ err := pastes.Find(q).One(&p)
+ return p, err
+}