Web Server
In this programming assignment, you will implement a simple web server to maintain your personal music library. Through this assignment, you will gain experience developing a more complex program that utilizes multiple CPU cores concurrently, implements persistent data storage, and incorporates external crates from the Rust ecosystem as dependencies. This assignment is worth 10% of your course grade and is due at 11:59 p.m. Eastern time on Monday, December 14, 2026.
Download lab4.rta. In a terminal in the folder where you saved it, update Rustrace and open the assignment:
rustrace update
rustrace work lab4.rtaBasic Web Server
As a starting point, your new web server, called server for simplicity in this assignment, should be able to accept standard GET HTTP requests from a web client. Your web client can be your favourite web browser or the UNIX curl utility. Your web server should accept requests to the local host (called localhost) and port 8080, and produce a response.
Once you have implemented the welcome route, run the following command in Rustrace’s F9 console:
cargo runThe output will be:
The server is currently listening on localhost:8080.This implies that the web server is ready to respond to HTTP requests sent to localhost:8080. If a web client, such as your web browser or the command-line curl utility in a second terminal, sends a HTTP request to your new server, it will respond as follows:
$ curl "http://localhost:8080"
Welcome to the Rust-powered web server!If you are using zsh as your shell, you may notice a % at the end of the line. This indicates that curl’s output does not include a newline character. To eliminate the % at the end, you can use curl -w '\n' http://localhost:8080 instead.
Site-Wide Visit Count
Now that a basic web server is functional, you can start to work on a new site-wide visit count. Every time a web client visits the URL http://localhost:8080/count, the server’s visit count will be incremented by 1. For example, if we use curl as the web client, we will see the following responses:
$ curl "http://localhost:8080/count"
Visit count: 1
$ curl "http://localhost:8080/count"
Visit count: 2
$ curl "http://localhost:8080/count"
Visit count: 3and so on, until the web server is restarted (when the visit count is reset to 0).
Please keep in mind, however, that your implementation of the web server should make every attempt to be performant, in that it should try to take advantage of multiple CPU cores on your computer. If you would like to try this for yourself, restart your server to reset the count. In a second terminal, the following sequence of commands uses an HTTP load generator called oha to send a million requests with many clients active at once:
$ cargo install oha
$ oha -n 1000000 "http://localhost:8080/count"
$ curl -s "http://localhost:8080/count"
Visit count: 1000001In this example, the oha command-line utility is used to generate HTTP load on your web server, and to measure the amount of time for your web server to process 1000000 requests. When the instructor tried this on a MacBook Pro M1 Max, the server took about 20 seconds in a debug build and 10 seconds in a release build. Depending on the speed of your computer, your measured times may vary. Needless to say, your visit count should be correctly maintained, in that your next visit will return a visit count of 1000001.
During the time when your web server is processing these requests, you can also use the htop command to check whether all CPU cores on your computer have been fully utilized, so that server performance is maximized:
htopIf you do not yet have the htop utility installed, you can use brew install htop to install it on macOS, and sudo apt install htop to install it on Ubuntu Linux.
Personal Music Library
With some initial experience building the site-wide visit count, you should be ready to extend your web server to maintain information about your personal music library. Each entry will contain several pieces of information: a song’s title, its artist, its genre (the type of music), as well as its play count. For simplicity, the song’s title, artist, and genre are strings, and its play count is an integer value.
Your web server will allow you to add new songs to your personal music library, to search your personal music library for songs, and to play a song in your library.
Adding new songs
To add a new song, a JSON POST request should be sent to your web server using the songs/new route, with the title, artist, and genre included in the JSON data. Your web server will produce a JSON response, containing a unique id assigned to the song, and an initial play count of 0. For example:
$ curl "http://localhost:8080/songs/new" \
--json '{"title":"Bohemian Rhapsody", "artist":"Queen", "genre":"Rock"}'
{"id":1,"title":"Bohemian Rhapsody","artist":"Queen","genre":"Rock","play_count":0}
$ curl "http://localhost:8080/songs/new" \
--json '{"title":"Love Story", "artist":"Taylor Swift", "genre":"Country"}'
{"id":2,"title":"Love Story","artist":"Taylor Swift","genre":"Country","play_count":0}
$ curl "http://localhost:8080/songs/new" \
--json '{"title":"Welcome to New York", "artist":"Taylor Swift", "genre":"Pop"}'
{"id":3,"title":"Welcome to New York","artist":"Taylor Swift","genre":"Pop","play_count":0}Before adding new songs to the personal music library, the web server does not need to check for duplicates. If you add the same song twice, you should receive two different IDs.
A successful POST returns HTTP status 201. If the JSON is malformed or any of the three fields is missing or is not a string, return status 400 and {"error":"Invalid song"}.
Searching for songs
A web client can send a query to the web server using the /songs/search route, searching for a particular title, artist, genre, or any combination of these attributes. For example:
$ curl "http://localhost:8080/songs/search?title=Bohemian"
[{"id":1,"title":"Bohemian Rhapsody","artist":"Queen","genre":"Rock","play_count":0}]
$ curl "http://localhost:8080/songs/search?artist=Queen"
[{"id":1,"title":"Bohemian Rhapsody","artist":"Queen","genre":"Rock","play_count":0}]
$ curl "http://localhost:8080/songs/search?genre=Rock"
[{"id":1,"title":"Bohemian Rhapsody","artist":"Queen","genre":"Rock","play_count":0}]A query can also include a conjunctive combination of these attributes:
$ curl "http://localhost:8080/songs/search?genre=Country&artist=Taylor+Swift"
[{"id":2,"title":"Love Story","artist":"Taylor Swift","genre":"Country","play_count":0}]Your web server’s response to a query should contain all matching songs whose title, artist, or genre contains the search keyword anywhere within the string:
$ curl "http://localhost:8080/songs/search?artist=Swift"
[{"id":2,"title":"Love Story","artist":"Taylor Swift","genre":"Country","play_count":0},{"id":3,"title":"Welcome to New York","artist":"Taylor Swift","genre":"Pop","play_count":0}]All queries are case-insensitive for ASCII letters. Here is an example:
$ curl "http://localhost:8080/songs/search?artist=taylor+swift"
[{"id":2,"title":"Love Story","artist":"Taylor Swift","genre":"Country","play_count":0},{"id":3,"title":"Welcome to New York","artist":"Taylor Swift","genre":"Pop","play_count":0}]If a query does not match any song in the personal music library, your web server should produce an empty JSON response:
$ curl "http://localhost:8080/songs/search?genre=Rock&artist=Taylor+Swift"
[]With no filters, return the entire library. Decode the query string before searching: + stands for a space, %2B for a plus sign, and %26 for an ampersand. Treat percent signs and underscores in the decoded search term as literal characters. Ignore unknown keys and use the last value of a repeated key.
Playing a song
A web client can play a song with a specific id by sending a GET request to the web server using the /songs/play route. When a song is played, its play count is incremented by 1. For example:
$ curl "http://localhost:8080/songs/play/1"
{"id":1,"title":"Bohemian Rhapsody","artist":"Queen","genre":"Rock","play_count":1}
$ curl "http://localhost:8080/songs/play/1"
{"id":1,"title":"Bohemian Rhapsody","artist":"Queen","genre":"Rock","play_count":2}If a song is not found in the music library, or the ID is not an integer, the server will return status 404 and an error in its JSON response:
$ curl "http://localhost:8080/songs/play/4"
{"error":"Song not found"}If two clients play the same song at once, each play must count. For example, if the song has never been played, one client should see a count of 1 and the other a count of 2.
Your personal music library should be persistent, in that even after your web server is terminated and then restarted, the entire content of your library should still remain available.
Implementation Notes
Rustrace opens the project in lab4.work. Write your code in the editor, where you will find eight functions with hints. In src/main.rs, implement route, Filters::parse, initialize_database, and serve. In src/library.rs, implement next_visit, add_song, search_songs, and play_song. Use the supplied Cargo manifest and lockfile.
You can get the routes working with the starter’s sequential loop. A successful GET returns status 200. An unknown path or an unsupported method returns 404 and {"error":"Route not found"}. When the routes work, replace the loop with four long-lived workers. Give each worker a connection to the same SQLite database and share one visit counter among them. If one client takes its time uploading a song, another should still be able to fetch the welcome page.
Create the songs table in target/lab4/music.db before the server announces it is listening. The tests use an integer primary key called id, text columns called title, artist, and genre, and an integer play_count that defaults to zero. Try stopping the server with Ctrl-C in F9 and starting it again. Can you still play the song you added earlier?
Testing for Correctness
For your convenience, five public test cases are included in tests/http.rs. Press F4 in Rustrace to choose a test or select Run all. You can also run cargo test in the F9 console. Each test starts its own server with a fresh database and uses curl to make requests, so make sure curl is on your PATH. Try the visits test as soon as you have the welcome page and visit count working.
Similar to previous assignments, several tests have been held back as private test cases for marking purposes.
Testing for Performance
If you would like to enter the optional performance competition, add Competition: yes to your Quercus submission comment by the deadline. To go for the best performance, you don’t have to work with the existing skeleton code; instead, you can start from scratch and write your own code however you wish. The performance test suite will first create a personal music library by adding 100,000 songs, and then measure the time your server takes to respond to queries using this large library. Eight clients add the songs and then run three rounds of 300 searches each. Your time is the time to add the songs plus the median of the three search-round times.
The performance test suite has been released for your own testing purposes as well. Download it beside lab4.work and run cargo build in F9. To try the benchmark without filling your own music library with test songs, open a terminal in the folder containing lab4.work and run:
server="$PWD/lab4.work/target/debug/server"
(cd "$(mktemp -d)" && LAB4_PORT=8081 "$server")In another terminal, try a short run:
python3 competition.py --port 8081 --smokeFor the full workload, stop the server, repeat the command that starts it in a fresh directory, and leave off --smoke.
Notes on AI Tools
This assignment is designed for manual work without AI assistance, and you should not use AI tools at all for this assignment. While we recognize that the habit of using AI may be so reflective and strong that — like social media — you cannot think without AI assistance, one of the objectives in this course is for you to learn writing code in the Rust programming language, and this learning experience needs to start from somewhere. This assignment offers an excellent starting point for you to learn from the beginning, and using AI defeats the purpose and completely changes the learning experience.
For this reason, you are required to write your programs in a dedicated Rust playground, called Rustrace, designed specifically for the programming assignments in this course. Rustrace records how your program came together, and it accepts only text that you type or copied within the editor itself. See the Rustrace guide for how to install and use it.
Submission
Submit your project by uploading your Rustrace ZIP to Quercus, under Assignment 4. Quit the editor, then run the following command from the directory containing lab4.work, using your own UTORid:
rustrace submit lab4.work --student-id YOUR_UTORIDKeep your workspace until grades are released. If you need to revise a finalized submission before the deadline, follow the Rustrace revision instructions.
The deadline for this assignment is Monday, December 14, 2026, at 11:59pm Eastern time, and late submissions will not be accepted.
Marking
The marking will be based on the correctness of your implementation in five public and five private tests, worth 10 points each. Make sure that your code is well-commented, easy to read, and that you have followed the Rust naming conventions. It goes without saying that you should also make sure that your code is free of warnings when compiled with the cargo build or the cargo check command.
We will check that your site-wide visit count and each song’s play count remain correct when requests arrive concurrently, and that other clients can get a response while an upload is in progress.
If you enter the competition and your solution passes all tests for correctness, we will verify your Rustrace ZIP and time your release build on the same course machine as the other entries. The driver checks the responses as it goes. If your solution is in the top 50% of the entrants who finish the benchmark correctly, you will be awarded a bonus of 1 assignment point. If your solution is in the top 30%, you will be awarded an additional 2 point. If your solution is in the top 15%, you will be awarded yet an additional 2 point. Finally, If your solution is in the top 5 students of the entire class, you will be awarded an additional 1 point, for a maximum of 16 out of 10. We round the number of places up in each group. Times equal to the nearest millisecond share a rank and the same bonus.