Example #1
0
// Init registers our routes into Gin
func Init() {
	log.Info("Initializing routes")
	gin.SetMode(gin.ReleaseMode)
	middleware()
	v1Routes()
	v2Routes()
	log.Info("Serving API on port %v", config.WebPort())
	g.Run(config.WebPort())
}
Example #2
0
// Status handler for the /status route
func OpenWebsocket(c *gin.Context) {
	userId := c.MustGet("request_user_id").(string)
	noteId := c.Param("note_id")
	in, note, err := db.GetNoteById(noteId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	if !in {
		c.Error(errors.NewHttp(http.StatusNotFound, "The requested note was not found"))
		return
	}
	if userId != note.Owner {
		c.Error(errors.NewHttp(http.StatusUnauthorized, "Only owners can open websockets into their notes"))
		return
	}
	conn, err := websocketUpgrader.Upgrade(c.Writer, c.Request, nil)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	log.Info("Opening ws for user %v on %v", userId, note.Id)
	bundle := model.NewContext(userId, noteId)
	WrapWebsocket(conn, bundle)
	ws.ProcessMessages(bundle)
}
Example #3
0
func WrapWebsocket(conn *websocket.Conn, bundle *model.WsContext) {
	// Read from WS, write to channel
	go func() {
		for {
			_, frameb, err := conn.ReadMessage()
			if log.Error(err) {
				bundle.Close <- true
				return
			}
			log.Info("Received: %v", string(frameb))
			frame := make(map[string]interface{})
			err = json.Unmarshal(frameb, &frame)
			if log.Error(err) {
				continue
			}
			bundle.Incoming <- frame
		}
	}()
	// Read from channel, write to WS
	go func() {
		for {
			select {
			case msg := <-bundle.Outgoing:
				b, err := json.Marshal(msg)
				if log.Error(err) {
					continue
				}
				log.Info("Sending: %v", string(b))
				err = conn.WriteMessage(1, b)
				if log.Error(err) {
					continue
				}
			case <-bundle.Close:
				return
			}
		}
	}()
}
Example #4
0
func HandleUpdate(frame map[string]interface{}, c *model.WsContext) error {
	var updateFrame model.WsUpdate
	err := util.FillStruct(&updateFrame, frame)
	if log.Error(err) {
		return err
	}
	log.Info("Processing update %+v", updateFrame)

	// Update the note content in our cache
	note := cache.GetNote(c.NoteId)
	note.Content, err = updateFrame.Update.Operations.Apply(note.Content)
	if log.Error(err) {
		return err
	}
	cache.Note(note)

	return nil

}
Example #5
0
func SendSuggestion(suggestion model.Suggestion, c *model.WsContext) {
	if suggestion.Recommendation.Genesis == c.UserId {
		return
	}
	if _, in := SentSuggestions[c.UserId]; in {
		for _, sentSuggestion := range SentSuggestions[c.UserId] {
			distance := matchr.Levenshtein(suggestion.Recommendation.Text, sentSuggestion.Recommendation.Text)
			if distance < 10 {
				return
			}
		}
	} else {
		SentSuggestions[c.UserId] = make([]model.Suggestion, 0)
	}
	log.Info("Sending suggestion to user %v", c.UserId)
	SentSuggestions[c.UserId] = append(SentSuggestions[c.UserId], suggestion)
	SentSuggestions[suggestion.Recommendation.Genesis] = append(SentSuggestions[suggestion.Recommendation.Genesis], suggestion)
	c.SendI(suggestion)
}
Example #6
0
// Init creates a connection to the database
func Init() {
	log.Info("Establishing a connection to the database")
	db, err := sql.Open("postgres", config.PostgresURL())
	if log.Error(err) {
		os.Exit(1)
	}
	err = db.Ping()
	if log.Error(err) {
		os.Exit(1)
	}
	dbmap = &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
	dbmap.AddTableWithName(model.DbSchool{}, "schools").SetKeys(false, "Id")
	dbmap.AddTableWithName(model.DbCourse{}, "courses").SetKeys(false, "Id")
	dbmap.AddTableWithName(model.DbCourseSection{}, "sections").SetKeys(false, "Id")
	dbmap.AddTableWithName(model.DbUser{}, "users").SetKeys(false, "Id")
	dbmap.AddTableWithName(model.DbSchoolRequest{}, "school_requests").SetKeys(false, "Id")
	dbmap.AddTableWithName(model.DbNotebook{}, "notebooks").SetKeys(false, "Id")
	dbmap.AddTableWithName(model.DbTopic{}, "topics").SetKeys(false, "Id")
	dbmap.AddTableWithName(model.DbNote{}, "notes").SetKeys(false, "Id")
	dbmap.AddTableWithName(model.DbSubscription{}, "subscriptions").SetKeys(false, "UserId", "NotebookId")
}
Example #7
0
func UpdateUser(user model.DbUser) error {
	log.Info("Updating user token and profile pic")
	_, err := dbmap.Update(&user)
	return err
}
Example #8
0
func CreateUser(u model.DbUser) error {
	log.Info("Creating new user " + u.Id)
	return dbmap.Insert(&u)
}