mirror of
https://github.com/arnaucube/goRecommender.git
synced 2026-02-06 19:16:39 +01:00
server structure, mongodb connection
This commit is contained in:
10
README.md
Normal file
10
README.md
Normal file
@@ -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
|
||||
57
color.go
Normal file
57
color.go
Normal file
@@ -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
|
||||
}
|
||||
9
errors.go
Normal file
9
errors.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
26
main.go
Normal file
26
main.go
Normal file
@@ -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))
|
||||
|
||||
}
|
||||
72
mongodb.go
Normal file
72
mongodb.go
Normal file
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
5
mongodbConfig.json
Normal file
5
mongodbConfig.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"ip": "127.0.0.1",
|
||||
"database": "goRecommend",
|
||||
"collection": "userHistory"
|
||||
}
|
||||
6
mongodbModels.go
Normal file
6
mongodbModels.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package main
|
||||
|
||||
type UserModel struct {
|
||||
Userid string
|
||||
History []string
|
||||
}
|
||||
67
serverConfig.go
Normal file
67
serverConfig.go
Normal file
@@ -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
|
||||
}
|
||||
4
serverConfig.json
Normal file
4
serverConfig.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"serverIP": "127.0.0.1",
|
||||
"serverPort": "3056"
|
||||
}
|
||||
32
serverRoutes.go
Normal file
32
serverRoutes.go
Normal file
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user