Browse Source

added ipFilter, implemented select item by user (adding scores) --> not finished yet

master
arnaucode 7 years ago
parent
commit
7cce4a11f4
8 changed files with 129 additions and 3 deletions
  1. +9
    -0
      README.md
  2. +30
    -0
      ipFilter.go
  3. +27
    -0
      mongodb.go
  4. +1
    -0
      mongodbModels.go
  5. +4
    -2
      serverConfig.go
  6. +5
    -1
      serverConfig.json
  7. +43
    -0
      serverRoutes.go
  8. +10
    -0
      tests.sh

+ 9
- 0
README.md

@ -36,3 +36,12 @@ GET /r/{userid}/{nrec}
{userid}: is the userid {userid}: is the userid
{nrec}: number of recommendations requested {nrec}: number of recommendations requested
``` ```
- Select Item
```
POST /selectItem
{
userid: "user1",
itemid: "item1"
}

+ 30
- 0
ipFilter.go

@ -0,0 +1,30 @@
package main
import (
"errors"
"fmt"
"net/http"
"strings"
)
func ipFilter(w http.ResponseWriter, r *http.Request) {
var err error
fmt.Println(r.RemoteAddr)
reqIP := strings.Split(r.RemoteAddr, ":")[0]
for _, ip := range serverConfig.BlockedIPs {
if reqIP == ip {
err = errors.New("ip not allowed to post images")
}
}
for _, ip := range serverConfig.AllowedIPs {
if reqIP != ip {
err = errors.New("ip not allowed to post images")
}
}
//return err
if err != nil {
fmt.Fprintln(w, err.Error())
return
}
}

+ 27
- 0
mongodb.go

@ -110,6 +110,33 @@ func getUserById(id string) (UserModel, error) {
return result, err return result, err
} }
} }
func getItemById(id string) (ItemModel, error) {
result := ItemModel{}
err := itemCollection.Find(bson.M{"id": id}).One(&result)
if err != nil {
//user not exist
return result, err
} else {
//user exist
return result, err
}
}
func updateItem(item ItemModel) (ItemModel, error) {
err := itemCollection.Update(bson.M{"id": item.ID}, item)
if err != nil {
//log.Fatal(err)
return item, err
}
return item, err
}
func updateUser(user UserModel) (UserModel, error) {
err := userCollection.Update(bson.M{"id": user.ID}, user)
if err != nil {
return user, err
}
return user, err
}
func getAllItems() ([]ItemModel, error) { func getAllItems() ([]ItemModel, error) {
result := []ItemModel{} result := []ItemModel{}
iter := itemCollection.Find(nil).Limit(100).Iter() iter := itemCollection.Find(nil).Limit(100).Iter()

+ 1
- 0
mongodbModels.go

@ -9,6 +9,7 @@ type ItemModel struct {
ID string ID string
TRecommended int TRecommended int
TActed int TActed int
TScored int //times acted from recommendation
} }
var scores []float64 var scores []float64

+ 4
- 2
serverConfig.go

@ -20,8 +20,10 @@ type Route struct {
//server config //server config
type ServerConfig struct { type ServerConfig struct {
ServerIP string `json:"serverIP"`
ServerPort string `json:"serverPort"`
ServerIP string `json:"serverIP"`
ServerPort string `json:"serverPort"`
AllowedIPs []string `json:"allowedIPs"`
BlockedIPs []string `json:"blockedIPs"`
} }
var serverConfig ServerConfig var serverConfig ServerConfig

+ 5
- 1
serverConfig.json

@ -1,4 +1,8 @@
{ {
"serverIP": "127.0.0.1", "serverIP": "127.0.0.1",
"serverPort": "3056"
"serverPort": "3056",
"allowedIPs": [
"127.0.0.1"
],
"blockedIPs": []
} }

+ 43
- 0
serverRoutes.go

@ -30,6 +30,12 @@ var routes = Routes{
"/user", "/user",
NewUser, NewUser,
}, },
Route{
"SelectItem",
"GET",
"/selectItem/{userid}/{itemid}",
SelectItem,
},
} }
//ROUTES //ROUTES
@ -39,6 +45,7 @@ func Index(w http.ResponseWriter, r *http.Request) {
} }
func NewUser(w http.ResponseWriter, r *http.Request) { func NewUser(w http.ResponseWriter, r *http.Request) {
ipFilter(w, r)
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)
var newUser UserModel var newUser UserModel
err := decoder.Decode(&newUser) err := decoder.Decode(&newUser)
@ -51,6 +58,7 @@ func NewUser(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "new user added: ", newUser.ID) fmt.Fprintln(w, "new user added: ", newUser.ID)
} }
func Recommendations(w http.ResponseWriter, r *http.Request) { func Recommendations(w http.ResponseWriter, r *http.Request) {
ipFilter(w, r)
vars := mux.Vars(r) vars := mux.Vars(r)
userid := vars["userid"] userid := vars["userid"]
nrec, err := strconv.Atoi(vars["nrec"]) nrec, err := strconv.Atoi(vars["nrec"])
@ -61,3 +69,38 @@ func Recommendations(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "recommendations") fmt.Fprintln(w, "recommendations")
} }
func SelectItem(w http.ResponseWriter, r *http.Request) {
ipFilter(w, r)
vars := mux.Vars(r)
userid := vars["userid"]
itemid := vars["itemid"]
//find item
item, err := getItemById(itemid)
if err != nil {
fmt.Fprintln(w, "item "+itemid+" not found")
}
//increase TActed in item
item.TActed = item.TActed + 1
//save item
item, err = updateItem(item)
check(err)
fmt.Println(item)
//find user
user, err := getUserById(userid)
if err != nil {
fmt.Fprintln(w, "user "+userid+" not found")
}
//add item to []Actions of user
user.Actions = append(user.Actions, itemid)
//save user
user, err = updateUser(user)
check(err)
fmt.Println(user)
fmt.Fprintln(w, "user: "+userid+", selects item: "+itemid)
}

+ 10
- 0
tests.sh

@ -10,6 +10,16 @@ http http://127.0.0.1:3056/user id="user2" age=32
echo "------------------" echo "------------------"
echo "[Getting recommendations for user]" echo "[Getting recommendations for user]"
echo http http://127.0.0.1:3056/user1/3
http http://127.0.0.1:3056/r/user1/3
echo "[selecting item by user]"
echo http http://127.0.0.1:3056/selectItem/user1/item1
http http://127.0.0.1:3056/selectItem/user1/item1
echo "[Getting recommendations for user]"
echo http http://127.0.0.1:3056/user1/3 echo http http://127.0.0.1:3056/user1/3
http http://127.0.0.1:3056/r/user1/3 http http://127.0.0.1:3056/r/user1/3

Loading…
Cancel
Save