@ -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 |
|||
} |
|||
} |
@ -0,0 +1,69 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"fmt" |
|||
"io/ioutil" |
|||
"log" |
|||
"net/http" |
|||
"time" |
|||
|
|||
"github.com/gorilla/mux" |
|||
) |
|||
|
|||
type Route struct { |
|||
Name string |
|||
Method string |
|||
Pattern string |
|||
HandlerFunc http.HandlerFunc |
|||
} |
|||
|
|||
//server config
|
|||
type ServerConfig struct { |
|||
ServerIP string `json:"serverIP"` |
|||
ServerPort string `json:"serverPort"` |
|||
AllowedIPs []string `json:"allowedIPs"` |
|||
BlockedIPs []string `json:"blockedIPs"` |
|||
} |
|||
|
|||
var serverConfig ServerConfig |
|||
|
|||
func readServerConfig(path string) { |
|||
file, err := ioutil.ReadFile(path) |
|||
if err != nil { |
|||
fmt.Println("error: ", err) |
|||
} |
|||
content := string(file) |
|||
json.Unmarshal([]byte(content), &serverConfig) |
|||
} |
|||
|
|||
func Logger(inner http.Handler, name string) http.Handler { |
|||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|||
start := time.Now() |
|||
|
|||
inner.ServeHTTP(w, r) |
|||
|
|||
log.Printf( |
|||
"%s\t%s\t%s\t%s", |
|||
r.Method, |
|||
r.RequestURI, |
|||
name, |
|||
time.Since(start), |
|||
) |
|||
}) |
|||
} |
|||
func NewRouter() *mux.Router { |
|||
router := mux.NewRouter().StrictSlash(true) |
|||
for _, route := range routes { |
|||
var handler http.Handler |
|||
handler = route.HandlerFunc |
|||
handler = Logger(handler, route.Name) |
|||
|
|||
router. |
|||
Methods(route.Method). |
|||
Path(route.Pattern). |
|||
Name(route.Name). |
|||
Handler(handler) |
|||
} |
|||
return router |
|||
} |
@ -0,0 +1,117 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"fmt" |
|||
"net/http" |
|||
) |
|||
|
|||
type Routes []Route |
|||
|
|||
var routes = Routes{ |
|||
Route{ |
|||
"Index", |
|||
"GET", |
|||
"/", |
|||
Index, |
|||
}, |
|||
/* Route{ |
|||
"Recommendations", |
|||
"GET", |
|||
"/r/{userid}/{nrec}", |
|||
Recommendations, |
|||
}, |
|||
*/ |
|||
Route{ |
|||
"NetworkMap", |
|||
"Get", |
|||
"/map", |
|||
NetworkMap, |
|||
}, |
|||
/* |
|||
Route{ |
|||
"SelectItem", |
|||
"GET", |
|||
"/selectItem/{userid}/{itemid}", |
|||
SelectItem, |
|||
}, |
|||
*/ |
|||
} |
|||
|
|||
//ROUTES
|
|||
|
|||
func Index(w http.ResponseWriter, r *http.Request) { |
|||
fmt.Fprintln(w, "ask for recommendations in /r") |
|||
} |
|||
|
|||
/* |
|||
func NewUser(w http.ResponseWriter, r *http.Request) { |
|||
ipFilter(w, r) |
|||
decoder := json.NewDecoder(r.Body) |
|||
var newUser UserModel |
|||
err := decoder.Decode(&newUser) |
|||
check(err) |
|||
defer r.Body.Close() |
|||
|
|||
saveUser(userCollection, newUser) |
|||
|
|||
fmt.Println(newUser) |
|||
fmt.Fprintln(w, "new user added: ", newUser.ID) |
|||
} |
|||
*/ |
|||
func NetworkMap(w http.ResponseWriter, r *http.Request) { |
|||
ipFilter(w, r) |
|||
|
|||
nodes, err := getAllNodes() |
|||
check(err) |
|||
edges, err := getAllEdges() |
|||
check(err) |
|||
|
|||
var network NetworkModel |
|||
network.Nodes = nodes |
|||
network.Edges = edges |
|||
|
|||
//convert []resp struct to json
|
|||
jNetwork, err := json.Marshal(network) |
|||
check(err) |
|||
|
|||
fmt.Fprintln(w, string(jNetwork)) |
|||
} |
|||
|
|||
/* |
|||
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") |
|||
} |
|||
|
|||
//find user
|
|||
user, err := getUserById(userid) |
|||
if err != nil { |
|||
fmt.Fprintln(w, "user "+userid+" not found") |
|||
} |
|||
|
|||
//increase TActed in item
|
|||
item.TActed = item.TActed + 1 |
|||
|
|||
//save item
|
|||
item, err = updateItem(item) |
|||
check(err) |
|||
fmt.Println(item) |
|||
|
|||
//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) |
|||
} |
|||
*/ |