mikeyglitz/pantry-api

By mikeyglitz

Updated about 2 years ago

Image
0

100

Pantry API

High-Level Architecture

Pantry API is a GraphQL application which is used to aid in managing inventory for a pantry. This application utilizes Postgres and Elasticsearch as its data backend. The application is designed to run in a Docker container.

Running

This application requires the following services to run:

In order to set up connections, check the configurations section below for environment variable configurations.

The docker image is built at https://hub.docker.com/r/mikeyglitz/pantry-api.

docker run -p 8080:8080 -e KEYCLOAK_CLIENT_ID=... mikeyglitz/pantry-api

Configuration

The configuration for this application is managed through environment variables

VariableDescription
SVC_HOSTThe hostname that the service will listen on
SVC_PORTThe port that the service will listen on
SVC_PATHThe path that exposes the graphql endpoint
SVC_PLAYGROUNDPATHThe path that exposes the GraphQL Playground endpoint
SVC_ISPLAYGROUNDENABLEDFlag to enable GraphQL Playground
SVC_LOG_LEVELThe level for which to display the logs
KEYCLOAK_CLIENT_IDThe Keycloak client ID for authentication
KEYCLOAK_CLIENT_SECRETThe Keycloak client secret used for authentication
KEYCLOAK_REALM_NAMEThe Keycloak realm name
KEYCLOAK_ENDPOINTThe address where the Keycloak server is listening on
ES_USERNAMEThe OpenSearch username
ES_PASSWORDThe OpenSearch password
ES_CERTIFICATE_PATHThe CA cert used to verify the OpenSearch SSL endpoints
ES_ADDRESSESA comma-separate list of OpenSearch server URLs
DB_PORTThe port that the Postgres database is listening on
DB_HOSTThe host where the Postgres database is listening on
DB_USERNAMEThe username used to login to Postgres
DB_PASSWORDThe password used to login to Postgres
DB_NAMEThe name of the database to connect to
OpenSearch Role

This application requires a user account and role to be created in OpenSearch with the following permissions:

Cluster-Level Permissions

  • cluster:monitor/health
  • cluster:monitor/main
  • indices:data/read/scroll/clear

Index-Level Permissions

Indices - ingredients, users, and pantries

  • create_index
  • indices:data/read/search
  • indices:data/write/delete/byquery
  • indices:admin/get
  • indices:admin/aliases/exists
  • indices:data/write/update/byquery
  • indices:data/read/get
  • indices:admin/create
  • indices:data/write/index
  • indices:admin/mapping/put
  • indices:data/read/scroll/clear
  • indices:data/write/bulk*

Endpoints

The API provided by this application exposes the following endpoints:

EndpointDescription
/actuator/*Exposes the actuator endpoints
/playgroundExposes the GraphQL Playground UI
/graphqlExposes the graphql API

The /playground and /graphql endpoints can be configured by the SVC_PLAYGROUNDPATH and SVC_PATH environment variables respectively

Project Structure

This project utilizes the Go standard project layout.

  • test - stores integration tests utilizing testcontainers
  • pkg - contains the root application server bootstrapping code
  • cmd - contains the main class -- the application entrypoint
  • internal - majority of the business logic is here
    • internal/config - configuration functions and interfaces
    • internal/database - database driver, models, and migration logic
    • internal/index - OpenSearch driver and interfaces
    • internal/dataloaders - dataloader interfaces
    • internal/handlers - gin rest handlers
    • internal/middleware - middleware used to enhance the request context (i.e. authentication)
    • internal/graph - GraphQL resolvers
    • internal/graph/services - database service layer - utilized primarily to supply data to GraphQL resolvers
    • internal/graph/schema - GraphQL schemas -- written in the GraphQL DSL

internal/graph/model, internal/graph/generated, and mocks all contain generated code

Roadmap

  • Implement configuration
  • Implement Elasticsearch driver
  • Implement ORM
  • Implement GraphQL schema
  • Separate GraphQL schema into separate files/resolvers
  • Break database/elasticsearch code into services and dataloaders
  • Validate auth token with Keycloak
  • Refactor to supply services and user using middleware
  • Create validation directive
  • Create user permissions directive

References

Docker Pull Command

docker pull mikeyglitz/pantry-api