Beispiel #1
0
// Serve an HTTP request.
func (ir *IncludeRelated) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// retrieve route parameters
	env := handlers.GetRequestEnv(r)
	include := r.URL.Query().Get("include")

	// check if any resources should be included
	query := env.DB
	if len(include) > 0 {
		// preload related models
		resources := strings.Split(include, ",")
		for _, resource := range resources {
			query = query.Preload(Capitalize(resource))
		}

		// update the environment
		handlers.SetRequestEnv(r, handlers.NewEnv(query))
	}

	// run the original handler
	ir.handler.ServeHTTP(w, r)
}
Beispiel #2
0
// Run the application.
func main() {
	// connect to the database
	db, err := gorm.Open("sqlite3", "/tmp/music.db")
	if err != nil {
		log.Fatal(err)
	}

	// migrate the database
	db.SingularTable(true)
	db.AutoMigrate(&models.Song{}, &models.Album{}, &models.Artist{})

	// create some test data
	songs := []models.Song{
		// Deadmau5, 4x4=12
		models.Song{1, "Some Chords"},
		models.Song{2, "Sofi Needs a Ladder"},
		models.Song{3, "A City in Florida"},
		models.Song{4, "Bad Selection"},
		models.Song{5, "Animal Rights"},
		models.Song{6, "I Said (Michael Woods Remix)"},
		models.Song{7, "Cthulhu Sleeps"},
		models.Song{8, "Right This Second"},
		models.Song{9, "Raise Your Weapon"},
		models.Song{10, "One Trick Pony"},
		models.Song{11, "Everything Before"},

		// The Glitch Mob, Drink the Sea
		models.Song{12, "Animus Vox"},
		models.Song{13, "Bad Wings"},
		models.Song{14, "How to Be Eaten by a Woman"},
		models.Song{15, "A Dream Within a Dream"},
		models.Song{16, "Fistful of Silence"},
		models.Song{17, "Between Two Points"},
		models.Song{18, "We Swarm"},
		models.Song{19, "Drive It Like You Stole It"},
		models.Song{20, "Fortune Days"},
		models.Song{21, "Starve the Ego, Feed the Soul"},
	}

	for _, song := range songs {
		if err := db.Create(&song).Error; err != nil {
			log.Fatal(err)
		}
	}

	albums := []models.Album{
		// Deadmau5
		models.Album{1, "4x4=12", songs[0:11]},

		// The Glitch Mob
		models.Album{2, "Drink The Sea", songs[11:21]},
	}

	for _, album := range albums {
		if err := db.Create(&album).Error; err != nil {
			log.Fatal(err)
		}
	}

	artists := []models.Artist{
		models.Artist{1, "Deadmau5", songs[0:11]},
		models.Artist{2, "The Glitch Mob", songs[11:21]},
	}

	for _, artist := range artists {
		if err := db.Create(&artist).Error; err != nil {
			log.Fatal(err)
		}
	}

	// create the handler environment
	env := handlers.NewEnv(&db)

	// create the request router
	router := mux.NewRouter()
	router.StrictSlash(true)

	// register api routes
	routes := []Route{
		Route{"GetSongs", "GET", "/api/song", handlers.GetSongs},
		Route{"PostSongs", "POST", "/api/song", handlers.PostSongs},
		Route{"GetSong", "GET", "/api/song/{id:[0-9]+}", handlers.GetSong},
		Route{"GetSongArtists", "GET", "/api/song/{id:[0-9]+}/artists", handlers.GetSongArtists},
		Route{"PutSong", "PUT", "/api/song/{id:[0-9]+}", handlers.PutSong},
		Route{"DeleteSong", "DELETE", "/api/song/{id:[0-9]+}", handlers.DeleteSong},

		Route{"GetAlbums", "GET", "/api/album", handlers.GetAlbums},
		Route{"PostAlbums", "POST", "/api/album", handlers.PostAlbums},
		Route{"GetAlbum", "GET", "/api/album/{id:[0-9]+}", handlers.GetAlbum},
		Route{"GetAlbumArtists", "GET", "/api/album/{id:[0-9]+}/artists", handlers.GetAlbumArtists},
		Route{"PutAlbum", "PUT", "/api/album/{id:[0-9]+}", handlers.PutAlbum},
		Route{"DeleteAlbum", "DELETE", "/api/album/{id:[0-9]+}", handlers.DeleteAlbum},

		Route{"GetArtists", "GET", "/api/artist", handlers.GetArtists},
		Route{"PostArtists", "POST", "/api/artist", handlers.PostArtists},
		Route{"GetArtist", "GET", "/api/artist/{id:[0-9]+}", handlers.GetArtist},
		Route{"PutArtist", "PUT", "/api/artist/{id:[0-9]+}", handlers.PutArtist},
		Route{"DeleteArtist", "DELETE", "/api/artist/{id:[0-9]+}", handlers.DeleteArtist},
	}

	RegisterRoutes(router, routes, func(r *Route) http.Handler {
		var handler http.Handler
		handler = r.HandlerFunc

		if r.Method == "GET" {
			handler = mw.NewIncludeRelated(handler)
		}

		return mw.NewProvideEnv(env, handler)
	})

	// serve static content
	router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static")))

	// run the http server
	http.Handle("/", router)
	http.ListenAndServe(":8080", nil)
}