gordon-cs / gordon-360-api

The 360° Gordon Experience
11 stars 6 forks source link

Gordon 360 API

The backend of Gordon 360, a Web API consumed by gordon-360-ui.

Table of Contents

The Code

Introduction

This project is an ASP.NET Core Web API written in C#. It generallly follows the Model View Controller (MVC) pattern: the heart of the code is in Controllers, which are organized according to the REST API they implement. The Models are auto-generated from the database objects using Entity Framework Core, and then ViewModels are written to abstract the database structure into the RESTful API structure. The View layer is provided by the frontend

Here is a breakdown of the significant parts of the project folder:

When the API receives an HTTP Request, it is routed to the Controller method with the matching Route attribute. For example, a HTTP Get request for https://360api.gordon.edu/api/activities will be routed to the ActivitesController.Get method. The Controller will make use of one or more Services (e.g. the ActivityService) to get data from the database as models/viewmodels. The controller will return that data, which will be serialized to JSON as the body of the HTTP Response.

Running the API locally

Preliminary setup

Building and running

API Maintenance

Continuous Integration

The backend uses GitHub Actions for Continuous Integration. Whenever changes are pushed to GitHub, the CI action defined in .github/workflows/ci.yml will be run by GitHub Actions. This action will checkout the latest version of whatever branch was pushed to and attempt to build it. If the build fails, the action will fail and no pull request on that branch will be able to be merged until a new build succeeds. This is the minimum in Continuous Integration. Ideally, we would also have unit/integration tests that run in the CI action. We currently have some tests defined in the pytest Python format, which can be found in Tests/ApiEndpoints. These are difficult to use in a Continuous Integration system because they require the API-to-be-tested to be running and locally accessible to the pytest module.

Continuous Deployment

The Gordon 360 API is hosted on the 360api.gordon.edu server. The built files are deployed at D:\Sites, under the names 360Api and 360ApiTrain for the master and develop branches respectively.

The backend is deployed automatically using GitHub Actions. Whenever changes are pushed to the develop or master branches, the CI workflow will run and ensure a successful build (see above section on Continuous Integration for details). If the changes build successfully, the output of that build will be saved as an artifact on the workflow run - build-Train for develop and build-Prod for master.

To detect and deploy successful builds, the scheduled task Deploy 360Api[Train] runs every 5 minutes on the API server. It calls the powershell script Deploy360BackEnd.ps1 (found at F:\Scripts\Deploy), polling GitHub's API for new builds. If it detects a new build for the relevant environment (Train for develop, Prod for master), it will backup the existing API and deploy the new one. Transcripts for these deployments can be found at F:\Scripts\Deploy\Transcripts.

The API server publishes its build timestamp and github SHA on the /Version endpoint, so checking this with Swagger (e.g., https://360apitrain.gordon.edu/swagger/) is a good way to see when the new version is deployed and serving.

Deploying Manually

If there are problems with continuous deployment, or a specific need arises to revert or push manually, then this older procedure can be used.

Additional Documentation

The docs folder contains further documentation. In particular, the Database and Endpoints documents may be useful to anyone trying to learn more about the project. However, both of these documents are known to be outdated, lacking important info, and in some cases fully incorrect. We hope to update/replace them sometime soon.