Skip to content

Simple testing CRUD application targeting MongoDB.

License

Notifications You must be signed in to change notification settings

RiskIdent/gocrud

Repository files navigation

gocrud

REUSE status

Simple CRUD application that exposes an HTTP REST API to store data inside a MongoDB database.

Usage

Configuration

gocrud is configured via command-line flags or environment variables.

Flag Environment variable Default Description
--bind-address GOCRUD_BIND_ADDRESS 0.0.0.0:8080 Address to serve API on
--mongo-uri GOCRUD_MONGO_URI mongodb://localhost:27017 MongoDB URI to use
--mongo-db GOCRUD_MONGO_DB gocrud MongoDB database to use

MongoDB authentication

Authentication can be provided via the MongoDB URI. Example:

GOCRUD_MONGO_URI=mongodb://admin:password@localhost:27017

API

By default, gocrud exposes the following endpoints on port 8080:

Create pet

Creates a new pet, and returns the ID of the pet created.

POST /v1/pet
Accept: application/json
Content-Type: application/json

{
  "name": "string",
  "species": "string",
  "breed": "string"
}
Responses (click to expand)
  • Object successfully created
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
  "id": "string"
}
  • Invalid request body
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8

{
  "error": "string"
}
  • Failed to create object in database
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8

{
  "error": "string"
}

Get pet

Retrieves an existing pet.

GET /v1/pet/:id
Accept: application/json

Parameters:

  • :id (path): ID of the pet object, formatted as a 24-character long hexadecimal number.
Responses (click to expand)
  • Object successfully retrieved.
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
  "id": "string",
  "name": "Grande Hazelnut Mc.Muffin",
  "species": "dog",
  "breed": "Dobermann"
}
  • Invalid :id parameter format.
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8

{
  "error": "string"
}
  • No pet was found with the ID of :id
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8

{
  "error": "string"
}
  • Failed to retrieve object from database.
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8

{
  "error": "string"
}

Development

Prerequisites

  • Go 1.20 (or higher)
  • A way to run MongoDB locally, e.g via a container using Podman

Running locally

  1. Start up a local MongoDB instance, for example via Podman:

    podman run --rm -it -p 27017:27017 mongo
  2. Run gocrud locally, e.g:

    go run .
  3. To test out the webhooks, you can make use of our example webhook like so:

    $ curl localhost:8080/v1/pet --json @examples/pet.json
    {"id":"63d00f3a87cb268ed07657e6"}
    
    $ curl localhost:8080/v1/pet/63d00f3a87cb268ed07657e6
    {"id":"63d00f3a87cb268ed07657e6","name":"Grande Hazelnut Mc.Muffin","species":"dog","breed":"Dobermann"}

License

This repository complies with the REUSE recommendations.

Different licenses are used for different files. In general:

Please see each file's header or accompanied .license file for specifics.