func TestServer(t *testing.T) { Convey("the server entry point", t, func() { initial := len(routes) Convey("should allow a new route to be added", func() { Map("GET", "/testing", func(writer http.ResponseWriter, request *http.Request) { log.Debug("/testing was accessed") }) So(len(routes), ShouldEqual, initial+1) Convey("and should have the proper http method set", func() { So(routes[initial].method, ShouldEqual, "GET") }) Convey("and should have the proper path set", func() { So(routes[initial].path, ShouldEqual, "/testing") }) }) Convey("should be able to create a gorilla mux router", func() { So(initRouter(), ShouldNotBeNil) }) }) }
// callback executed after ListenAndServer to set everything up func initRouter() http.Handler { router := mux.NewRouter() for _, value := range routes { log.Debug("bunding %v %v", value.method, value.path) router.HandleFunc(value.path, value.handler).Methods(value.method) } return router }
func RegisterObjectClick(variables map[string]string) interface{} { objectId := variables["object_id"] userId := variables["X-Clikr-User"] sceneObject, ok := SceneObjects[objectId] if !ok { log.Warn("attempt to register object click with invalid object id: '%v'", objectId) return []*SceneObject{} } log.Debug("user '%v' register click on object '%v:%v'", userId, sceneObject.Name, objectId) // mark the object as liked for this user, will strength bond between other // liked objects. LikeObject(userId, sceneObject) bonds, ok := Bonds[sceneObject] if !ok { log.Debug("no bonds available for object: '%v'", objectId) return []*SceneObject{} } maxSize := 3 // maximum number of recommendations to return size := int(math.Min(float64(len(bonds)), float64(maxSize))) out := make([]*SceneObject, size, size) for i, b := range bonds { log.Debug("recommending '%v' with bond strength '%v'", b.With.Name, b.Strength) out[i] = b.With if i == (maxSize - 1) { break } } return out }
// main entry point for the server func main() { log.Debug("clikr server booting up, binding to port 3000") http.ListenAndServe(":3000", initRouter()) }