Skip to content

aventhis/candy-server-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Candy API Server

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.

Technologies

  • Go
  • Swagger (go-swagger)
  • OpenSSL
  • TLS/SSL

Available Candies and Their Prices

Code Name Price (cents)
CE Cool Eskimo 10
AA Apricot Aardvark 15
NT Natural Tiger 17
DE Dazzling Elderberry 21
YR Yellow Rambutan 23

Features

  1. 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.
  2. 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.
  3. Code Generation:

    • Server code and models are automatically generated from the Swagger 2.0 specification using go-swagger.

Installation and Execution

Step 1. Clone the Repository

Clone the repository:

git clone git@github.com:aventhis/candy-server-go.git
cd candy-server-go/src

Step 2. Install Dependencies

Ensure Go is installed, then run:

go mod tidy

Step 3. Generate Server Code from Swagger Specification (if needed)

swagger generate server -f swagger.yaml -a candy-server

Step 4. Using Makefile

For 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-client

Step 5. Run the Server (Manual Option)

You 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.pem

Step 6. Run the Client (Manual Option)

Build 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 50

API

Methods and request formats are described in the Swagger specification. Main endpoint:

  • POST /buy_candy - handles candy purchases.

About

REST API сервер на Go для управления заказами конфет с использованием спецификации Swagger, поддержкой mTLS аутентификации и динамической генерацией ASCII-артов. Включает проверку входных данных и расчет сдачи на основе введенной суммы.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages