A very basic RESTful API for a fictional online store that implements CRUD operations for store items.
An item is composed of
- ID,
- SKU,
- name,
- price,
- and category.
To build and run the app Go compiler 1.16 is required. Please refer to the official installation steps for instructions on setting it up.
To start the API server use the following command:
go run cmd/kekule/server.go localhost:8080
You can also launch the server inside a Docker container.
First build it with
docker build -t kekule .
then run it with
docker run -d -p 8080:8080 -e ADDRESS=0.0.0.0:8080 kekule
To launch the server in release mode simply set the GIN_MODE=release
environment variable.
When launching in the local environment
GIN_MODE=release go run cmd/kekule/server.go localhost:8080
When starting within a container
docker run -d -p 8080:8080 \
-e ADDRESS=0.0.0.0:8080 \
-e GIN_MODE=release \
kekule
The API supports basic CRUD operations.
Fetches a single item.
Parameters:
id
of typeinteger
: the ID of the item.
On success, returns a JSON-object like:
{
"result": "success",
"item": {
"id": 1,
"sku": "c2n1",
"name": "Item1",
"price": 20.0,
"category": 2
}
}
Fetches a list of items.
Parameters:
page
of typeinteger
, must be larger or equal to 1.limit
of typeinteger
: how many items to fetch at most, must be less than 100.lowest_price
of typedouble
: the lowest price an item can have in the the result set, must be positive.highest_price
of typedouble
: the highest price an item can have in the result set, must be positive.category
of typeinteger
: specifies the category for items in the result set, must be positive and valid.
On success, returns a JSON-object like:
{
"result": "success",
"items": [
{
"id": 1,
"sku": "c2n1",
"name": "Item1",
"price": 20.0,
"category": 2
},
// ...
]
}
Creates a new item.
Parameters:
name
of typestring
: the name of the item, length cannot exceed 64 characters.price
of typedouble
: the price of the item.category
of typeint
: the category of the item.
On success, returns a JSON-object like:
{
"result": "success",
"id": 12 // ID of the newly created item.
}
Updates an existing item.
Parameters:
id
of typeinteger
: the ID of the item.name
of typestring
: the new name of the item, length cannot exceed 64 characters, optional.price
of typedouble
: the new price of the item, optional.category
of typeint
: the new category of the item, optional.
On success, returns a JSON-object like:
{
"result": "success",
"description": "item updated"
}
Deletes an existing item.
Parameters:
id
of typeinteger
: the ID of the item to delete.
On success, returns a JSON-object like:
{
"result": "success",
"description": "item deleted"
}
Fetches a category.
id
of typeinteger
: the ID of the category to fetch.
On success, returns JSON-object like:
{
"result": "success",
"category": {
"id": 7,
"name": "category name"
}
}
All methods return a JSON-object of the following form if an error occurred when handling a request:
{
"result": "error message",
"description": "error details"
}