Maps Back End

Cover Page

DUE Wed, 04/03, 2 pm

As when we added support for images in the previous lab, we must prepare the Chatter back end to support geodata.

Install updates

Remember to install updates available to your Ubuntu back end. If N in the following notice you see when you ssh to your back-end server is not 0,

N updates can be applied immediately.

run the following:

server$ sudo apt update
server$ sudo apt upgrade

If you see *** System restart required *** when you ssh to your server, please run:

server$ sync
server$ sudo reboot

Your ssh session will be ended at the server. Wait a few minutes for the system to reboot before you ssh to your server again.

Failure to update your packages could lead to your lab back end **not performing correctly** and also make you vulnerable to security hacks.

Modified Chatter API data formats

In this lab, we will add the user’s geodata, consisting of their geolocation and velocity (facing and speed) at the time of posting, to a chatt.

As in previous labs, the chatts retrieval API will send back all accumulated chatts in the form of a JSON object consisting of a dictionary entry with key “chatts” and value being an array of string arrays. In addition to the three elements “username”, “message”, “timestamp” of the first lab, each string array now carries an additional element which is itself an array containing the user’s geodata (in order): latitude (lat), longitue (lon), location (corresponding to the lat/lon), compass point facing, and speed.

{ 
    "chatts": [["username0", "message0", "timestamp0", "[lat0, lon0, \"loc0\", \"facing0\", \"speed0\"]"],
               ["username1", "message1", "timestamp1", "[lat1, lon1, \"loc1\", \"facing1\", \"speed1\"]"], 
               ... 
              ]
}

To post a chatt, the client correspondingly sends a JSON object with keys “username”, “message”, and “geodata”, where the value for key “geodata” conforms to the format above. For example:

{	
   "username": "YOUR_UNIQNAME",	
   "message": "Hello world!",	
   "geodata": "[42.29, -83.72, \"Ann Arbor\", \"South\", \"walking\"]"
}

Notice that both the “facing” and “speed” elements are descriptive, as we’ll explain further.

Database table

Add one new column named geodata of type text to the chatts table in your chatterdb database. If you’re uncertain as to how to add a column to a PostgreSQL database table, please review the Audio and Chatter labs’ back-end specs.

Web server

We now add new URL paths and their corresponding handlers to your back-end server.

Go with atreugo

Go with atreugo

Editing views.go

Add one new property to the Chatt struct in views/views.go:

type Chatt struct {
    // . . .
    GeoData  string    `json:"geodata"`
}

As with audio data in the previous lab, geodata is just a string to the back-end server. Make a copy of your PostAudio() in views.go and call it PostMaps(). In the newly copied PostMaps(), find the one occurence of audio and one of Audio and replace them with geodata and GeoData respectively.

Next, make a copy of your GetAudio() function inside your views.go file and name the copy GetMaps(). In the newly copied GetMaps(), find the one occurence of audio and replace it with geodata and find the two occurences of Audio and replace both with GeoData.

Save and exit views.go.

Routing for new URLs

For the newly added GetMaps() and PostMaps() functions, add the following new routes to the routes array in router/router.go:

var routes = []Route {
    // . . .
    {"GET", "/getmaps/", views.GetMaps},
    {"POST", "/postmaps/", views.PostMaps},
}

Save and exit router.go.

:point_right:Go is a compiled language, like C/C++ and unlike Python, which is an interpreted language. This means you must run go build each and every time you made changes to your code, for the changes to show up in your executable.

Rebuild and restart chatterd:

server$ go build
server$ sudo systemctl restart chatterd

Python with Django

Python with Django

Editing views.py

Now edit your views.py to handle geodata uploads. As with the audio data in the previous lab, geodata is just a string to the back-end server. Make a copy of your postaudio() function inside your views.py file and name the copy postmaps(). In the newly copied postmaps(), find the four occurences of audio and replace them with geodata.

Next, make a copy of your getaudio() function inside your views.py file and name the copy getmaps(). In the newly copied getmaps(), find the one occurence of audio and replace it with geodata.

Save and exit views.py.

Routing for new urls

For the newly added getmaps() and postmaps() functions, add the following new routes to the urlpatterns array in urls.py:

path('getmaps/', views.getmaps, name='getmaps'),
path('postmaps/', views.postmaps, name='postmaps'),

Save and exit urls.py and restart Gunicorn.

Rust with axum

Rust with axum

Editing handlers.rs

Add one new property to the Chatt struct in handlers.rs:

pub struct Chatt {
    // . . .
    geodata: Option<String>,
}

As with audio in the previous lab, the audio data is just a string to the back-end server. Make a copy of your postaudio() function inside your handlers.rs file and name the copy postmaps(). In the newly copied postmaps(), find the two occurences of audio and replace both with geodata.

Next, make a copy of your getaudio() function inside your handlers.rs file and name the copy getmaps(). In the newly copied getmaps(), find the one occurence of audio and replace it with geodata.

Save and exit handlers.rs.

Routing for new URLs

For the newly added getmaps() and postmaps() functions, add the following new routes to the Router instantiation of the router variable in main.rs, before the .with_state(pgpool); line:

        .route("/getmaps/", get(handlers::getmaps))
        .route("/postmaps/", post(handlers::postmaps))

Save and exit main.rs. Rebuild and restart chatterd.

server$ cargo build --release
server$ sudo systemctl restart chatterd

Testing geodata upload

You can test your postmaps/ API using the example JSON above. After a successful POST, your database should contain:

chatterdb=# SELECT * FROM chatts;
 username | message |          time          | audio | geodata                      
----------+---------+------------------------+----------+---------
 YOUR_UNIQNAME    | Hello world! | 2022-09-28 17:53:28-04 |          | [42.29, -83.72, "Ann Arbor", "South", "walking"] |
(1 row)

Submitting your back end

Congratulations! You’ve completed the back end modifications necessary to support geodata!


Prepared for EECS 441 by Wendan Jiang, Ollie Elmgren, Benjamin Brengman, Mark Wassink, Alexander Wu, Yibo Pi, and Sugih Jamin Last updated: March 17th, 2024