From e218286be00e26d28fd28f62a221454791150197 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Wed, 5 Jul 2017 19:17:48 +0200 Subject: [PATCH] server structure, mongodb connection --- README.md | 10 +++++++ color.go | 57 ++++++++++++++++++++++++++++++++++++ errors.go | 9 ++++++ main.go | 26 +++++++++++++++++ mongodb.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++ mongodbConfig.json | 5 ++++ mongodbModels.go | 6 ++++ serverConfig.go | 67 ++++++++++++++++++++++++++++++++++++++++++ serverConfig.json | 4 +++ serverRoutes.go | 32 +++++++++++++++++++++ 10 files changed, 288 insertions(+) create mode 100644 README.md create mode 100644 color.go create mode 100644 errors.go create mode 100644 main.go create mode 100644 mongodb.go create mode 100644 mongodbConfig.json create mode 100644 mongodbModels.go create mode 100644 serverConfig.go create mode 100644 serverConfig.json create mode 100644 serverRoutes.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..dae71ad --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# goRecommender + +Recommendation system API, based on Machine Learning, written in Go lang + +Data stored in MongoDB + +Applies Machine Learning to perform recommendations: + + - Random Forests + - K Nearest Neighbours diff --git a/color.go b/color.go new file mode 100644 index 0000000..e945ac5 --- /dev/null +++ b/color.go @@ -0,0 +1,57 @@ +package main + +import "fmt" + +//Color struct, defines the color +type Color struct{} + +var c Color + +//DarkGray color +func (c Color) DarkGray(t string) { + fmt.Print("\x1b[30;1m") //dark gray + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Red color +func (c Color) Red(t string) { + fmt.Print("\x1b[31;1m") //red + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Green color +func (c Color) Green(t string) { + fmt.Print("\x1b[32;1m") //green + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Yellow color +func (c Color) Yellow(t string) { + fmt.Print("\x1b[33;1m") //yellow + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Blue color +func (c Color) Blue(t string) { + fmt.Print("\x1b[34;1m") //blue + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Purple color +func (c Color) Purple(t string) { + fmt.Print("\x1b[35;1m") //purple + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Cyan color +func (c Color) Cyan(t string) { + fmt.Print("\x1b[36;1m") //cyan + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..9e83a92 --- /dev/null +++ b/errors.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func check(err error) { + if err != nil { + fmt.Println(err) + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..06914d1 --- /dev/null +++ b/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "log" + "net/http" + + "github.com/fatih/color" +) + +func main() { + fmt.Println("starting") + + //mongodb start + readMongodbConfig("./mongodbConfig.json") + c := connectMongodb() + fmt.Println(c) + color.Green("mongodb connected") + + //http server start + readServerConfig("./serverConfig.json") + color.Green("server running") + router := NewRouter() + log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, router)) + +} diff --git a/mongodb.go b/mongodb.go new file mode 100644 index 0000000..6abefe3 --- /dev/null +++ b/mongodb.go @@ -0,0 +1,72 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + + mgo "gopkg.in/mgo.v2" +) + +//MongoConfig stores the configuration of mongodb to connect +type MongoConfig struct { + Ip string `json:"ip"` + Database string `json:"database"` + Collection string `json:"collection"` +} + +var mongoConfig MongoConfig + +func readMongodbConfig(path string) { + file, e := ioutil.ReadFile(path) + if e != nil { + fmt.Println("error:", e) + } + content := string(file) + json.Unmarshal([]byte(content), &mongoConfig) +} + +func getSession() (*mgo.Session, error) { + session, err := mgo.Dial("mongodb://" + mongoConfig.Ip) + if err != nil { + panic(err) + } + //defer session.Close() + + // Optional. Switch the session to a monotonic behavior. + session.SetMode(mgo.Monotonic, true) + + // Optional. Switch the session to a monotonic behavior. + session.SetMode(mgo.Monotonic, true) + + return session, err +} +func getCollection(session *mgo.Session) *mgo.Collection { + + c := session.DB(mongoConfig.Database).C(mongoConfig.Collection) + return c +} +func connectMongodb() *mgo.Collection { + session, err := getSession() + if err != nil { + log.Fatal(err) + } + c := getCollection(session) + return c +} + +func saveDataEntryToMongo(c *mgo.Collection, user UserModel) { + /* + how to call this function + var auxArr []string + user := UserModel{"123", auxArr} + saveDataEntryToMongo(c, user) + */ + + err := c.Insert(user) + if err != nil { + log.Fatal(err) + } + +} diff --git a/mongodbConfig.json b/mongodbConfig.json new file mode 100644 index 0000000..e769fcd --- /dev/null +++ b/mongodbConfig.json @@ -0,0 +1,5 @@ +{ + "ip": "127.0.0.1", + "database": "goRecommend", + "collection": "userHistory" +} diff --git a/mongodbModels.go b/mongodbModels.go new file mode 100644 index 0000000..d5b95bd --- /dev/null +++ b/mongodbModels.go @@ -0,0 +1,6 @@ +package main + +type UserModel struct { + Userid string + History []string +} diff --git a/serverConfig.go b/serverConfig.go new file mode 100644 index 0000000..f93cbc5 --- /dev/null +++ b/serverConfig.go @@ -0,0 +1,67 @@ +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"` +} + +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 +} diff --git a/serverConfig.json b/serverConfig.json new file mode 100644 index 0000000..252b474 --- /dev/null +++ b/serverConfig.json @@ -0,0 +1,4 @@ +{ + "serverIP": "127.0.0.1", + "serverPort": "3056" +} diff --git a/serverRoutes.go b/serverRoutes.go new file mode 100644 index 0000000..5c57a68 --- /dev/null +++ b/serverRoutes.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "net/http" +) + +type Routes []Route + +var routes = Routes{ + Route{ + "Index", + "GET", + "/", + Index, + }, + Route{ + "NewImage", + "POST", + "/image", + NewImage, + }, +} + +//ROUTES + +func Index(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "send images to the /image path") +} +func NewImage(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "response") +}