Skip to content

il-blood-donation-info/blood-donation-backend

Repository files navigation

Build Status

Blood Donation Backend Service

The project is used to collect and persist blood donation stations, and offers an API to manage stations availability. It also allows querying for the stations.

View OpenAPI definition using Swagger UI here.

Updating the API and generating code

In order to modify the API, edit the api/openapi.yaml file. Then, run the following commands to generate the code:

oapi-codegen -config pkg/api/api.cfg.yaml pkg/api/openapi.yaml

If oapi-codegen isn't available, install the code-gen dependency:

go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@latest

Setting development environment

Creating a Database

CREATE DATABASE bloodinfo;
CREATE USER mada WITH PASSWORD <your password>; # change this
GRANT ALL PRIVILEGES ON DATABASE bloodinfo TO mada;

Running the server

export DB_HOST=localhost
export DB_PORT=5432
export DB_USER=mada
export DB_NAME=bloodinfo
export DB_PASSWORD= # your password
go run cmd/cert/tls-self-signed-cert.go
go run cmd/server/main.go

Running using docker-compose

Building the image and running the containers

Edit or change the db.env file to your liking. Then, run the following command:

docker-compose --env-file db.env up --build

Note this generates a self-signed cert & key during the build, separate from anything you might have in cert.pem file. So once it built, you want to run this to get the same ./cert.pem:

docker run blood-donation-backend_blood-info /bin/cat cert.pem > cert.pem

Stopping the containers

docker-compose --env-file db.env down --remove-orphans --volumes --rmi local

Testing the API

Creating a user

curl --cacert ./cert.pem -X POST https://localhost:8443/users -H "Content-Type: application/json" -d '{"description": "User description",
    "email": "[email protected]",
    "first_name": "John",
    "id": 1,
    "last_name": "Doe",
    "phone": "1234567890",
    "role": "Admin"}'

Getting a user

curl --cacert ./cert.pem -s -X GET https://localhost:8443/users | jq
[
  {
    "description": "User description",
    "email": "[email protected]",
    "first_name": "John",
    "id": 1,
    "last_name": "Doe",
    "phone": "1234567890",
    "role": "Admin"
  }
]

Running Scrapper test against real DB

export DB_HOST=localhost
export DB_PORT=5432
export DB_USER_TEST=mada_test
export DB_NAME_TEST=bloodinfo_test
export DB_PASSWORD=mada

# Create the test database
TEST_DB_COMMANDS=$(cat <<EOF
CREATE DATABASE $DB_NAME_TEST;
CREATE USER $DB_USER_TEST WITH PASSWORD '$DB_PASSWORD';
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME_TEST TO $DB_USER_TEST;
EOF
)

# Use echo to pass the commands to psql
echo "$TEST_DB_COMMANDS" | psql -U postgres

go test ./pkg/scraper/... -v

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published