To-do Backend

Created for CS3219 Software Engineering Principles and Patterns Own Time Own Target (OTOT) Task B. Makes use of the Ports & Adapters architecture.

Frontend: https://github.com/ianyong/todo-frontend

Getting Started

  1. Install Go >= 1.17 by following the instructions here.

  2. Install PostgreSQL >= 12 by following the instructions here.

  3. Make a copy of .env.development as .env.development.local. For development on your local system, it is recommended that you connect to PostgreSQL via Unix-domain sockets so that there is no need for database server credentials, allowing you to leave DB_USER and DB_PASSWORD empty. This can be done by setting the DB_HOST to the following locations:

    • Linux: /var/run/postgresql
    • macOS: /tmp

    Note that if you are connecting via localhost, you might need to set DB_SSLMODE=disable.

  4. Create the database.

    $ make createdb
  5. Migrate the database.

    $ make migratedb
  6. Start the server. By default, the backend is accessible at http://localhost:8000/.

    $ make run

Linting

  1. Install golangci-lint by following the instructions here.
  2. Optionally, you can integrate the linter with your IDE if it is supported by following the instructions here. Otherwise, you will need to run make fmt and make lintfix to automatically format and fix any lint violations before you commit any changes, or add a pre-commit Git hook that does it for you automatically.

Note that gosec is a supported linter in golangci-lint. As such, there is no need to separately install gosec for local development.

Running Tests

  1. Make a copy of .env.test as .env.test.local. The configuration should be similar to .env.development.local as described above, but with a different database name.
  2. Create the test database.
    $ make createtestdb
  3. Migrate the test database.
    $ make migratetestdb
  4. Run the tests.
    $ make test

Deployment

Deployment makes use of the Serverless Framework. To deploy to AWS Lambda manually:

  1. Install Node >= 14 by following the instructions here.
  2. Install the Serverless Framework:
    $ npm install -g serverless
  3. Build the server for use with AWS Lambda. Note that the normal server has to be wrapped with a translation layer so that the server is able to understand API requests and responses that go through AWS Lambda.
    $ make buildlambda
  4. Export the environment variables (see any of the .env.* files) for the database connection that will be used by the AWS Lambda function.
  5. Migrate the database that will be used. Make sure that the correct environment variables are loaded from the step before.
    $ GO_ENV=production make migratedb
  6. Deploy the server.
    $ serverless deploy