- Install Go (Golang): You can download and install Go from the official website: https://golang.org/dl/. Follow the installation instructions based on your operating system.
Before running the API, you need to install the Gorilla Mux package. Open a terminal or command prompt and execute the following command:
go get -u github.com/gorilla/mux
-
Copy the provided code into a file named
main.go
. -
Open a terminal or command prompt and navigate to the directory containing the
main.go
file. -
Build the API using the following command:
go build
This will create an executable file named myapi
(or myapi.exe
on Windows) in the same directory.
- Run the API server using the following command:
You can use go run
to build and run the API in one step:
go run main.go
The server will start and listen on port 8000.
To process a receipt, you can use tools like curl
, Postman
, or any other HTTP client.
curl -X POST -H "Content-Type: application/json" -d '{
"retailer": "Target",
"purchaseDate": "2022-01-01",
"purchaseTime": "13:01",
"items": [
{
"shortDescription": "Mountain Dew 12PK",
"price": "6.49"
},
{
"shortDescription": "Emils Cheese Pizza",
"price": "12.25"
},
{
"shortDescription": "Knorr Creamy Chicken",
"price": "1.26"
},
{
"shortDescription": "Doritos Nacho Cheese",
"price": "3.35"
},
{
"shortDescription": "Klarbrunn 12-PK 12 FL OZ",
"price": "12.00"
}
],
"total": "35.35"
}' http://localhost:8000/receipts/process
You will receive a response with the generated receipt ID.
To get the points for a processed receipt, use the ID received from the previous step.
curl http://localhost:8000/receipts/{RECEIPT_ID}/points
Replace {RECEIPT_ID}
with the actual ID received from the previous step.
You will receive a response with the calculated points for the receipt.
- This API is for demonstration purposes and does not use a persistent database. Receipt data is stored in memory and will be lost upon server restart.