Cover Page

Backend Page

Go with Echo

handlers.go

Change to your chatterd folder and edit the file hanlders.go:

server$ cd ~/reactive/chatterd 
server$ vi handlers.go

Add one new property to the Chatt struct:

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

To the back-end database, geodata is just a string. Since the geodata string optional, we set the type to *string, which can take a null pointer, nil in Go.

To handle geodata data uploads, make a copy of your postchatt() function inside your handlers.go file and name the copy postmaps(). Replace the call to chatterDB.Exec() with:

	_, err := chatterDB.Exec(background, `INSERT INTO chatts (username, message, id, geodata) VALUES ($1, $2, gen_random_uuid(), $3)`, chatt.Username, chatt.Message, chatt.Geodata)

which extracts the geodata entry from the JSON object and insert it into the chatts table, along with the rest of its associated chatt.

Next, make a copy of your getchatts() function inside your handlers.go file and name the copy getmaps(). Replace the SELECT statement with: SELECT username, message, id, time, geodata FROM chatts. This will retrieve all data, including our new geodata from the PostgreSQL database.

Still in getmaps(), replace the rows.Scan() call in the for rows.Next() {} block with:

		err = rows.Scan(&chatt.Username, &chatt.Message, &chatt.Id, &chatt.Timestamp, &chatt.Geodata)

and if there’s no error (returned err is nil):

        chattArr = append(chattArr, []any{chatt.Username, chatt.Message, chatt.Id, chatt.Timestamp, chatt.Geodata})

In addition to the original columns, we added reading the geodata column and included it in the chatt data returned to the front end.

We’re done with handlers.go. Save and exit the file.

main.go

Edit the file main.go:

server$ vi main.go

Find the global variable router and add the following new routes for the new APIs /getmaps and /postmaps:

    {"GET", "/getmaps/", getmaps},
    {"POST", "/postmaps/", postmaps},

We’re done with main.go. Save and exit the file.

Build and test run

To build your server:

server$ go build

: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.

To run your server:

server$ sudo ./chatterd
# Hit ^C to end the test

The cover backend spec provides instructions for Testing geodata upload.


Prepared by Sugih Jamin Last updated August 11th, 2025