A MusicApi Web API application built using the .NET 6 Web API framework and MongoDB that aggregates music data using the Audio Network API.
- The application is built using a persistance layer (MongoDB) for a fast response time. Another approach can include Redis for caching or persistance.
- The application only loads 500 music tracks and returns only a subset of the Track record to keep things simple and small. However, the application is built to be scalable.
- The application seeds and updates the Database on Application startup. This is designed to keep it simple and easy to run. The assumption is that on production there will be a seperate scheduled or cronjob service to keep the Database updated.
- The overall goal was to keep it simple, but feel free to ask questions regarding the application architecture or approach.
I personally used Visual Studio to develop, test and run the project, but there are multiple ways to run this project. The different methods are listed below.
First, clone the repo in to the desired folder using CLI git clone https://github.com/sanjayh94/MusicApi/
or your favourite IDE/GUI tool.
You will need git CLI installed for this command to work, Find instructions here
- Download and Install Docker and docker-compose if you haven't already. Find instructions here. Note: Verify Docker is running on Linux containers mode.
- Open your preferred terminal and Navigate to the directory where you cloned the repo earlier. For example
cd ~/repos/MusicApi
- Navigate to the MusicApi Project folder
cd MusicApi
. Create an emptysecrets.json
file.touch secrets.json
. The secrets.json file should be inside the application folder, not the root of the repo. For example,cd ~/repos/MusicApi/MusicApi/secrets.json
- Add the Audio Network Public Api Key
{
"AudioNetworkApiKey": "YOUR_KEY_HERE"
}
- Navigate to
appsettings.json
and verify theMusicTracksDatabase.ConnectionString
is"ConnectionString": "mongodb://musictracksdb:27017"
{
"MusicTracksDatabase": {
"ConnectionString": "mongodb://musictracksdb:27017",
"DatabaseName": "MusicTracks",
"TracksCollectionName": "Tracks"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
- Navigate to the root of the repo
cd ~/repos/MusicApi
. - Build the images
docker-compose build
- Run the application
docker-compose up
- The application will be taking requests at
http://localhost:5000
. (Ignore theNow listening on: http://[::]:80
in the logs)
Not needed for the Docker approach, only for running a local server through IDE or CLI
- Download and Install MongoDB Server from here.
- Choose a directory on your development machine for storing the data.
- Open a command shell. Run the following command to connect to MongoDB on default port 27017. Remember to replace <data_directory_path> with the directory you chose in the previous step.
mongod --dbpath <data_directory_path>
- In the application folder, make sure the file
MusicApi/appsettings.json
has the correct connection string"ConnectionString": "mongodb://localhost:27017"
{
"MusicTracksDatabase": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "MusicTracks",
"TracksCollectionName": "Tracks"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
- The Database should get seeded when the application starts up.
- Install Visual Studio (or VSCode if preferred) from here.
- Navigate to the directory where you cloned the repo earlier and open the Solution from the
MusicApi.sln
file in the root of the directory. - Follow the previous steps to add secrets and Make sure MongoDB is set up and running.
- Hit the Run icon and the project should build and launch in a browser.
- Take a note of the url and port when the browser launches to test the endpoint. For example,
http://localhost:5000/
Note: You will have to Install the C# extension and/or the .NET SDK, if you are planning to use VSCode. It should automatically prompt you to install when you open the project for the first time.
- Install .NET SDK (CLI tools are bundled) if you haven't already. Find instructions here. If you installed Visual Studio in the previous step, .NET CLI should already be installed.
- Open your preferred terminal and Navigate to the directory where you cloned the repo earlier. For example
cd ~/repos/MusicApi/MusicApi
- Build the project using
dotnet build
command. Make sure the Build succeeds at this stage - Run the 'MusicApi' project using
dotnet run --project MusicApi
. Make note of the url and port from the logNow listening on: http://localhost:5000
Run the project using any of the methods above, and then use your preferred API client (Postman, httpie, curl or even your browser!) to test these endpoints. Make sure the the correct port is used when querying the endpoints. Replace the port 5000
with your own.
-
GET
http://localhost:5000/swagger
: Autogenerated OpenAPI swagger documentation. You can also use this to try the endpoints out. Click the appropriate endpoint and click theTry it out
button. Input a word in the parameters box and click theExecute
button to initiate an API call. -
GET
http://localhost:5000/health
: Basic healthcheck -
GET
http://localhost:5000/api/tracks
: Returns a list of all tracks- Example call using httpie
http localhost:5000/api/tracks
- Response would be:
- (Rest of result omitted)
[ { "album": { "description": "From the former in-house producer at Dr Dre’s Aftermath comes an upbeat yet darkly hypnotic set exploring EDM-influenced trap. Features modulated vocals samples, hooky synth melodies, layered FX, dark clap loops, bouncy beats and melodic 808 bass.", "mainMixTrackCount": 10, "name": "Some Ting", "number": 3720, "pillar": "Core", "trackCount": 86 }, "description": "Dark, hypnotic trap with pitched vocal loop & vocal shouts, heavy bass, synths & driving trap beats", "explicit": false, "id": 1059108, "keywords": "Mell Beets ; beats ; beats series ; modulator ; modulated ; loop ; sample ; hook ; hypnotic ; driving ; forward motion ; energy ; energetic ; synths ; shouts ; vocal shouts ; ", "releaseDate": "2022-05-31T00:00:00Z", "title": "Loosie" }, ...
- Example call using httpie
-
GET
http://localhost:5000/api/tracks/{id}
: Returns a track with given id- Example call using httpie
http localhost:5000/api/tracks/1057691
- Response would be:
{ "album": { "description": "Pioneering ambient composer Theo Travis (Robert Fripp, Harold Budd) and producer Paul Ressel (Lana Del Rey) explore ominous soundscapes. Textural arrangements include unnerving saxophones, clarinet, alto flute, deep drone bass, synth glitches and guitar.", "mainMixTrackCount": 6, "name": "Beauty In Hell", "number": 3715, "pillar": "Core", "trackCount": 85 }, "description": "Slow moody soundscape with moody clarinets, deep drone, synths & subtle acoustic guitar textures", "explicit": false, "id": 1057691, "keywords": "desolate ; clarinets ; hollow ; empty ; nothingness ; unsettled ; developing ; synth ; synthesizer ; deep drone ; developing ; synth bass", "releaseDate": "2022-05-30T00:00:00Z", "title": "Heart Of The Storm 10" }
- Example call using httpie
-
GET
http://localhost:5000/api/tracks/count/{word}
: For a given word, will return the count of tracks where given word is included in the title- Example call using httpie
http localhost:5000/api/tracks/count/heart
- Response would be:
{ "count": 11 }
- Example call using httpie
-
GET
http://localhost:5000/api/tracks/search/{word}
: For a given word, will return the list of tracks where given word is included in the title- Example call using httpie
http localhost:5000/api/tracks/count/heart
- Response would be:
- (Rest of result omitted)
[ { "album": { "description": "Pioneering ambient composer Theo Travis (Robert Fripp, Harold Budd) and producer Paul Ressel (Lana Del Rey) explore ominous soundscapes. Textural arrangements include unnerving saxophones, clarinet, alto flute, deep drone bass, synth glitches and guitar.", "mainMixTrackCount": 6, "name": "Beauty In Hell", "number": 3715, "pillar": "Core", "trackCount": 85 }, "description": "Slow moody soundscape with moody clarinets, deep drone, synths & subtle acoustic guitar textures", "explicit": false, "id": 1055576, "keywords": "desolate ; clarinets ; hollow ; empty ; nothingness ; unsettled ; developing ; synth ; synthesizer ; deep drone ; developing ; synth bass", "releaseDate": "2022-05-30T00:00:00Z", "title": "Heart Of The Storm" }, { "album": { "description": "Pioneering ambient composer Theo Travis (Robert Fripp, Harold Budd) and producer Paul Ressel (Lana Del Rey) explore ominous soundscapes. Textural arrangements include unnerving saxophones, clarinet, alto flute, deep drone bass, synth glitches and guitar.", "mainMixTrackCount": 6, "name": "Beauty In Hell", "number": 3715, "pillar": "Core", "trackCount": 85 }, "description": "Slow moody soundscape with deep drone, synths & acoustic guitar textures. No clarinets", "explicit": false, "id": 1057286, "keywords": "desolate ; clarinets ; hollow ; empty ; nothingness ; unsettled ; developing ; synth ; synthesizer ; deep drone ; developing ;", "releaseDate": "2022-05-30T00:00:00Z", "title": "Heart Of The Storm 2" }, ...
- Example call using httpie
The project consists of a number of Unit Tests. Unit tests test methods in isolation of dependencies.
- Open the solution in Visual Studio
- Open the Test Explorer window/tab and hit the Run Tests button
- Make sure .NET CLI is installed from the previous steps
- Open your preferred terminal and Navigate to the directory where you cloned the repo earlier. For example
cd ~/repos/MusicApi/MusicApi.Tests
- Run the command
dotnet test