/cservice-api

Primary LanguageGoMIT LicenseMIT

CI Codacy Badge Codacy Badge

Channel Services API

Warning

THIS IS A WORK IN PROGRESS. The API is not stable and may change at any time. DO NOT USE IN PRODUCTION.

Requirements

  • golang >= 1.22 (for compiling)
  • PostgreSQL >= 11.0 (for running)
  • Valkey (opensource redis)

Configuration

Copy config.yml.example to config.yml and edit it to your liking.

Generate JWT RSA key pair for access token and refresh token

openssl genrsa -out jwt.key 4096
openssl rsa -in jwt.key -pubout -out jwt.pub
openssl genrsa -out refresh_jwt.key 4096
openssl rsa -in refresh_jwt.key -pubout -out refresh_jwt.pub

Configure cservice-api with JWT RSA key

Add the following to config.yml:

jwt:
    signing_method: "RS256"
    signing_key: /path/to/jwt.key
    public_key: /path/to/jwt.pub
    refresh_signing_key: /path/to/refresh_jwt.key
    refresh_public_key: /path/to/refresh_jwt.pub

The JWKS can be downloaded from <site>/.well-known/jwks.json.

Building and running

Build

make build

Running the service:

bin/cservice-api -config </path/to/config.yml>

Development

Generate database repositories

This project uses sqlc to generate Go code from SQL queries.

The database schema is defined in db/migrations/*.sql. Do NOT modify existing migration files if a schema change is necessary. Instead, run the following command:

migrate create -ext sql -dir db/migrations <migration_name>

This will create two new migration files in db/migrations with the current timestamp for migrating up and down. Edit the files to add the necessary SQL statements.

To generate the Go code from the migrations in db/migrations and the SQL queries defined in db/query/*.sql, run:

make generate-sqlc

After this, you may have to update the service.go file in models so that it matches the interface defined in models/querier.go.

After changing the SQL queries or schema it may be necessary to update the database mocks for the unit tests by running:

make generate-mocks

Unit tests

To run the unit tests, run:

make test

Integration tests

The integration tests use dockertest. To run the integration tests, run:

make integration-test

Live reloading while developing

To run the service with live reloading, run:

make watch