Example #1
0
func main() {
	var err error
	flag.Parse()

	if err = database.Init(); err != nil {
		log.Fatal(err)
	}

	defer database.Close()

	// r := router.New()
	r := cors.New(cors.Options{
		AllowCredentials: true,
		AllowedMethods:   []string{"PUT", "POST", "GET", "DELETE", "OPTIONS"},
		AllowedHeaders:   []string{"*"},
	}).Handler(router.New())

	osPort := os.Getenv("PORT")
	if osPort != "" {
		*listenAddr = osPort
	}

	srv := &http.Server{
		Addr:         ":" + *listenAddr,
		Handler:      r,
		ReadTimeout:  30 * time.Second,
		WriteTimeout: 30 * time.Second,
	}

	log.Printf("Starting server on 127.0.0.1:%s\n", *listenAddr)
	log.Fatal(srv.ListenAndServe())

}
Example #2
0
func (n *Name) Get() error {
	if err := database.Init(); err != nil {
		return err
	}
	session := database.MongoSession.Clone()
	return session.DB(database.DatabaseName).C(database.NameCollection).FindId(n.ID).One(&n)
}
Example #3
0
func (u *User) Get() error {
	if err := database.Init(); err != nil {
		return err
	}
	session := database.MongoSession.Clone()
	return session.DB(database.DatabaseName).C(database.UserCollection).FindId(u.ID).One(&u)
}
Example #4
0
func (s *Song) Get() error {
	if err := database.Init(); err != nil {
		return err
	}
	session := database.MongoSession.Clone()
	return session.DB(database.DatabaseName).C(database.SongCollection).FindId(s.ID).One(&s)
}
Example #5
0
func (s *Song) Delete() error {
	if err := database.Init(); err != nil {
		return err
	}
	session := database.MongoSession.Clone()
	return session.DB(database.DatabaseName).C(database.SongCollection).RemoveId(s.ID)
}
Example #6
0
//status checks db connection and returns status
func status(ctx *middleware.Context, w http.ResponseWriter, r *http.Request) (interface{}, error) {
	status := "UP"
	err := database.Init()
	if err != nil {
		status = "DB error"
	}
	return status, err
}
Example #7
0
func (g *Genre) Create() error {
	if err := database.Init(); err != nil {
		return err
	}
	g.ID = bson.NewObjectId()
	session := database.MongoSession.Clone()
	return session.DB(database.DatabaseName).C(database.GenreCollection).Insert(g)
}
Example #8
0
func (s *Song) Create() error {
	if err := database.Init(); err != nil {
		return err
	}
	s.ID = bson.NewObjectId()
	session := database.MongoSession.Clone()
	return session.DB(database.DatabaseName).C(database.SongCollection).Insert(s)
}
Example #9
0
func (u *User) Delete() error {
	if err := database.Init(); err != nil {
		return err
	}
	session := database.MongoSession.Clone()
	err := session.DB(database.DatabaseName).C(database.UserCollection).RemoveId(u.ID)
	u.Password = ""
	return err
}
Example #10
0
func GetAll() ([]Name, error) {
	var names []Name
	if err := database.Init(); err != nil {
		return names, err
	}
	session := database.MongoSession.Clone()
	err := session.DB(database.DatabaseName).C(database.NameCollection).Find(bson.M{}).All(&names)
	return names, err
}
Example #11
0
func GetGenres() ([]Genre, error) {
	var gs []Genre
	if err := database.Init(); err != nil {
		return gs, err
	}
	session := database.MongoSession.Clone()
	err := session.DB(database.DatabaseName).C(database.GenreCollection).Find(bson.M{}).All(&gs)
	return gs, err
}
Example #12
0
func (u *User) Create() error {
	if err := database.Init(); err != nil {
		return err
	}
	session := database.MongoSession.Clone()
	u.ID = bson.NewObjectId()
	err := session.DB(database.DatabaseName).C(database.UserCollection).Insert(u)
	u.Password = ""
	return err
}
Example #13
0
// Authorize returns user with token if okay, else it errors
func (u *User) Authorize() error {
	if err := database.Init(); err != nil {
		return err
	}
	session := database.MongoSession.Clone()
	query := bson.M{
		"token": u.Token,
	}
	return session.DB(database.DatabaseName).C(database.UserCollection).Find(query).One(&u)
}
Example #14
0
func GetAllSongs() ([]Song, error) {
	var songs []Song
	if err := database.Init(); err != nil {
		return songs, err
	}

	session := database.MongoSession.Clone()
	err := session.DB(database.DatabaseName).C(database.SongCollection).Find(bson.M{}).All(&songs)
	return songs, err
}
Example #15
0
//Authenticate returns user with matching email & password, else errors
func (u *User) Authenticate() error {
	if err := database.Init(); err != nil {
		return err
	}
	session := database.MongoSession.Clone()
	query := bson.M{
		"email":    u.Email,
		"password": u.Password,
	}
	err := session.DB(database.DatabaseName).C(database.UserCollection).Find(query).One(&u)
	if err != nil {
		return err
	}
	return u.updateToken()
}
Example #16
0
func (s *Song) Update() error {
	if err := database.Init(); err != nil {
		return err
	}
	session := database.MongoSession.Clone()

	temp := Song{
		ID: s.ID,
	}
	err := temp.Get()
	if err != nil {
		return err
	}
	query := bson.M{}
	if s.Name != "" {
		query["name"] = s.Name
	}
	if s.User != nil {
		query["user"] = s.User
	}
	if s.Artist != "" {
		query["artist"] = s.Artist
	}
	if s.Link != "" {
		query["link"] = s.Link
	}
	if s.Comments != nil {
		query["comments"] = s.Comments
		for i, _ := range s.Comments {
			if s.Comments[i].User.ID.Hex() != "" {
				err = s.Comments[i].User.Get()
				if err != nil {
					return err
				}
			}
		}
	}
	if s.Genre != nil && s.Genre.Name != "" {
		query["genre"] = s.Genre
	}
	if s.Votes != nil {
		query["votes"] = s.Votes
	}
	return session.DB(database.DatabaseName).C(database.SongCollection).UpdateId(s.ID, query)
}
Example #17
0
func (s *Song) Find() ([]Song, error) {
	var songs []Song
	if err := database.Init(); err != nil {
		return songs, err
	}
	session := database.MongoSession.Clone()

	query := bson.M{}
	if s.ID.Valid() {
		query["_id"] = s.ID.String()
	}
	if s.User != nil {
		query["user"] = s.User
	}
	if s.Name != "" {
		query["name"] = s.Name
	}
	if s.Artist != "" {
		query["artist"] = s.Artist
	}
	if s.Link != "" {
		query["link"] = s.Link
	}
	if s.Comments != nil {
		query["comments"] = s.Comments
	}
	if s.Genre != nil {
		query["genre"] = s.Genre
	}
	if s.Votes != nil {
		query["votes"] = s.Votes
	}
	err := session.DB(database.DatabaseName).C(database.SongCollection).Find(query).All(&songs)
	for _, s := range songs {
		if s.User.ID.Hex() == "" {
			continue
		}
		err = s.User.Get()
		if err != nil {
			return songs, err
		}
	}
	return songs, err
}
Example #18
0
func (u *User) updateToken() error {
	token, err := createToken()
	if err != nil {
		return err
	}
	u.Token = token

	if err := database.Init(); err != nil {
		return err
	}
	session := database.MongoSession.Clone()
	update := bson.M{
		"$set": bson.M{
			"token": u.Token,
		},
	}
	err = session.DB(database.DatabaseName).C(database.UserCollection).UpdateId(u.ID, update)
	u.Password = ""
	return err
}