Backend & APIs

The backend for the project is written in Go and performs two functions:

  1. Serves REST APIs for retrieving user information and stats
  2. Acts as a listener to Wonderville's WebSocket and subsequently records new users and updates stats in the MongoDB database.

The socket listener is decoupled from the REST API service through a goroutine, meaning any interruptions to the REST service will not affect the recording of new data from the socket server. The socket listener will also automatically retry connecting every 60 seconds to the WebSocket server upon disconnection. The application logs are outputted to the rest_golang/logs.txt file.

Notable packages used in the backend are:

Project Structure

├── rest_golang
│   ├── db        // (Package) Retrieving and updating data from MongoDB
│   ├── logger    // (Package) Application logger
│   ├── model     // (Package) Type definitions for API and internal use
│   ├── server    // (Package) REST API service (handler and router)
│   ├── socket    // (Package) WebSocket listener
│   ├── logs.txt  // Application logs
│   └── main.go   // Entry point to the application

APIs

v1/api/users

Methods: GET
Description: Gets historical users and the total counts of users, uniques cities and unique countries relative to the given start date.
Parameters:

ParameterRequired?Type
Description
Example
startDateYStringThe start of the date range in ISO string format.
2022-10-04T14:48:00.000Z
maxUsersYIntegerThe maximum number of users to return.
100
mapBoundsYObjectThe latitude and longitude boundaries used to search for users. Latitude and longitude values are integers.
{
lat: {
lower: -49.68,
upper: 84.71
},
lng: {
lower: -175.25,
upper: -52.74
}
filterNObjectThe activity filter value.

Valid filter categories are Category or ActivityType. If Category is chosen, you can specify Game, Video, Activity or Story as the filter type.
{
filterCategory: "Category",
filterType: "Game"
}

Responses
Success: 200 OK
Content:

{
  "users": [
    {
      "type": "wondervilleAsset",
      "payload": {
        "ip": "172.219.38.100",
        "url": "tree-cookies",
        "location": {
          "country_name": "Canada",
          "region_name": "Alberta",
          "city": "Leduc",
          "longitude": -113.5587,
          "latitude": 53.2659
        },
        "asset": {
          "name": "Tree Cookies",
          "url": "tree-cookies",
          "id": 32,
          "uuid": "",
          "type": "Game",
          "imageUrl": "https://wonderville.org/wvAssets/Uploads/Tree-Cookies-Thumb.png",
          "active": true
        },
        "rank": 1
      },
      "date": "2022-12-04T18:03:43.181Z"
    }
  ],
  "counts": {
    "sessions": 1,
    "cities": 1,
    "countries": 1
  }
}

v1/api/activity-stats

Methods: GET
Description: Gets activity hit counts in descending order by the number of hits for the given date range.
Parameters:

ParameterRequired?Type
Description
Example
startDateNStringThe start of the date range in ISO string format. If this is not included, all-time hit counts will be returned.
2022-10-04T14:48:00.000Z
endDateNStringThe end of the date range in ISO string format. Required if startDate is provided.
2022-11-05T14:48:00.000Z
topNIntegerThe top number activities to return.
10

Responses
Success: 200 OK
Content:

{
  "stats": [
    {
      "hits": 81,
      "name": "Save The World",
      "type": "Game",
      "url": "",
      "imageUrl": "https://wonderville.org/wvAssets/Uploads/Save-the-World-Thumb.png",
      "rank": 1
    },
    {
      "hits": 24,
      "name": "Tree Cookies",
      "type": "Game",
      "url": "",
      "imageUrl": "https://wonderville.org/wvAssets/Uploads/Tree-Cookies-Thumb.png",
      "rank": 2
    }
  ]
}

v1/api/activity-filter-options

Methods: GET
Description: Gets a unique list of all activity categories and activity names recorded over time.
Parameters: None
Responses
Success: 200 OK
Content:

{
  "options": [
    {
      "name": "Game",
      "type": "Category"
    },
    {
      "name": "Video",
      "type": "Category"
    },
    {
      "name": "Airborne Experiment",
      "type": "Game"
    },
    {
      "name": "Waste No More",
      "type": "Video"
    }
  ]
}