https://github.com/mdcatapult/coshh-api
Go API complementing the COSHH UI web app
https://github.com/mdcatapult/coshh-api
Last synced: 2 months ago
JSON representation
Go API complementing the COSHH UI web app
- Host: GitHub
- URL: https://github.com/mdcatapult/coshh-api
- Owner: mdcatapult
- License: apache-2.0
- Created: 2024-06-27T10:15:37.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-09-16T10:58:04.000Z (9 months ago)
- Last Synced: 2025-02-06T13:51:36.682Z (4 months ago)
- Language: Go
- Homepage:
- Size: 93.4 MB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## COSHH API
This GOLang based application provides the backend API service to complement the [COSHH webapp](https://gitlab.mdcatapult.io/informatics/coshh/coshh-ui). It uses a Postgres Database to store information about chemicals, their hazardous properties, expiry dates and links to safety docs.
### Running
Copy the `.env_example` file to `.env`. Change the values to match your local setup. For example, point `COSHH_DATA_VOLUME` to where the
`assets` data files are stored on your local machine. The `COSHH_DATA_VOLUME` is mounted into the docker container and the files are read from there.```bash
make run
```This starts the API and a local Postgres instance.
By default, the app starts with no data. To populate the app, follow the [ETL guide](scripts/etl/README.md). You can also start with an empty db if you prefer. The Postgres docker container uses an [sql script](scripts/init.sql) to create the schema.
### Debugging
You can run it all using the docker-compose file (use within the `make run` task above) or start up your locally changed versions of the components using `docker compose up -d`. This will build your local version of the app and run it via docker.
You can also run and debug it in your IDE of choice by starting the server using `cmd/main.go`.
To run your local versions you need to tell the backend what database connection params to use and where the files detailing the different lab and project names are.```bash
export DBNAME=informatics \
export HOST=localhost \
export PASSWORD=postgres \
export PORT=5432 \
export API_PORT=8080 \
export USER=postgres \
export SCHEMA=coshh \
export LABS_CSV= \
export Auth0Audience="https://coshh-api-local.wopr.inf.mdc" \
export Auth0Domain="mdcatapult.eu.auth0.com"
export LDAP_USER="[email protected]"
export LDAP_PASSWORD=
````Auth0Audience` is the identifier used in the Auth0 setup page for the particular API within the chosen `Auth0Domain`.
LDAP username and password are used to get a list of users from the MDC LDAP server. The [email protected] user has
been created specifically for this purpose and has readonly access. The password is stored in 1Password.Start the database (also seeds the db with initial data):
```bash
docker-compose up -d db
```Start the API without docker:
```bash
cd cmd
go run main.go
```
ORStart the API using docker:
```bash
docker compose up -d server
```OR use your IDE and run the `cmd/main.go` file. Remember to set your env vars.
### Testing
`make test`
### Accessing the database locally from the command line
Ensure you set the schema, e.g.
```
psql -h localhost -U postgres -d informatics \\ password is postgres
SET schema 'coshh';
```#### View the output of audit_triggers/trigger functions
This audit trigger/functions provide transactions records of CRUD operations done on the chemical table(informatics.coshh).
Before running the commands below.
You must have inserted and updated data into the coshh schema tables, only then can you run the following commandssimply run this command
```
`SELECT * FROM audit_coshh_logs;` or `SELECT * FROM audit_coshh_log_views;````
NB: If none of the commands work add a coshh prefix for example `coshh.audit_coshh_logs`.### Testing Authenticated Routes
Get the Auth0 client token from the Auth0 web portal.
* Login in to the Auth0 web page.
* Switch to the correct Auth0 tenant.
* Go to `Applications` in the sidebar and select `APIs`.
* Open the correct API page from the list and click on the `Test` tab.
* Copy the access token from the `Response` box.
* Use curl to auth against the example `protected` route.```bash
curl --request GET \
--url http:/localhost:8080/protected \
--header 'authorization: Bearer INSERT AUTH0 TOKEN'
```
Successful auth results in `"You have successfully authenticated"`. Failure to auth results in `{"message":"Requires authentication"}`.### Gotchas
#### SQL
When writing any new sql queries always remember to commit the transaction!
#### Seed data
This is contained in /scripts/etl/init-003.sql and this script is run when the docker-compose file is run.
The data includes a NULL value for every column in the chemical table without a NON NULL constraint. This is to help identify
any errors which might arise when deploying the UI - the live database contains some pretty shaky historic data.
The data includes one chemical which is archived, two which are expired and one which expires within 30 days (again, to
facilitate testing of the UI).**Remember to run `docker-compose down` before running `make run` (or `docker-compose up`) if you want to re-seed the database.**
#### CI
There was a glitch in the publish API stage in CI in October 2022 (which has since resolved itself) which meant that in order to deploy the API the image
had to be built locally and pushed up to the registry manually. In the event this should happen again use this command:```docker build -t registry.mdcatapult.io/informatics/software-engineering/coshh/api: . && docker push registry.mdcatapult.io/informatics/software-engineering/coshh/api:```
N.B Mac M1 users may need to build the image for amd64 (as opposed to arm64) with `--platform linux/amd64`
### Licence
This project is licensed under the terms of the Apache 2 licence, which can be found in the repository as `LICENCE.txt`