This project is the result of completing the "Day 04 - Go Boot Camp" task. The main goal is to develop an API server for candy vending machines. The task includes:
- Implementing an API that meets the requirements of the Swagger protocol.
- Supporting functionality for input validation, change calculation, and error handling.
- Adding mutual TLS authentication for security.
- Integrating an external ASCII art generator.
- Go
- Swagger (go-swagger)
- OpenSSL
- TLS/SSL
| Code | Name | Price (cents) |
|---|---|---|
| CE | Cool Eskimo | 10 |
| AA | Apricot Aardvark | 15 |
| NT | Natural Tiger | 17 |
| DE | Dazzling Elderberry | 21 |
| YR | Yellow Rambutan | 23 |
-
Candy Purchase:
- The client sends a request to the server with data about the amount of money, candy type, and quantity.
- The server calculates the change or returns an error:
- If the money is sufficient, the response includes "Thank you!" and the change.
- If the money is insufficient, the response contains an error indicating the amount needed.
- If the input is invalid (negative quantity or nonexistent candy type), an error is returned.
-
Security:
- Mutual TLS authentication is implemented using self-signed certificates.
- Both server and client use certificates signed by a local Certificate Authority (CA) created with Minica.
-
Code Generation:
- Server code and models are automatically generated from the Swagger 2.0 specification using go-swagger.
Clone the repository:
git clone git@github.com:aventhis/candy-server-go.git
cd candy-server-go/srcEnsure Go is installed, then run:
go mod tidyswagger generate server -f swagger.yaml -a candy-serverFor simplifying development and testing, the project includes a Makefile. Main commands:
- Build the server and client:
make all
- Run the server:
make start-server
- Run the client:
make start-client
- Generate certificates for the server and client:
make generate-certs
- Clean binaries and certificates:
make clean clean-certs
Example full flow:
make clean clean-certs
make generate-certs
make start-server
make start-clientYou can also run the server manually with the certificate paths:
go run cmd/candy-server-server/main.go --tls-port=3333 --tls-certificate=candy.tld/cert.pem --tls-key=candy.tld/key.pem --tls-ca=minica.pemBuild and run the client manually:
go build candy-client.go
./candy-client -k <candy_type> -c <count> -m <money_amount>Example:
./candy-client -k AA -c 2 -m 50Methods and request formats are described in the Swagger specification. Main endpoint:
POST /buy_candy- handles candy purchases.